在非关键属性上查询 DynamoDB

Querying DynamoDB on non-key attributes(在非关键属性上查询 DynamoDB)
本文介绍了在非关键属性上查询 DynamoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否可以在 AWS 版本 2 中使用非关键属性过滤 DynamoDB 查询?亚马逊表示他们可以做到:http://amzn.to/1FVgQ9B.但是他们也提供 API 吗?我找到了 AWSDynamoDBQueryExpression,但我认为它只允许过滤范围键(没有足够的文档).我正在 iOS 和 AWS 版本 2 中寻找合适的 API.谢谢!

Is it possible to filter DynamoDB queries using non-key attributes in AWS version 2 ? Amazon says that they can do it: http://amzn.to/1FVgQ9B. But do they also give the API? I found AWSDynamoDBQueryExpression, but I think it only lets filtering on the range key (not enough documentation). I'm looking for the proper API in iOS and AWS version 2. Thanks!

推荐答案

我正在回答我自己的问题.这也是我在 AWS 支持论坛上发布的内容:

I'm answering my own question. This is what I posted on AWS support forum as well:

您无法使用高级 API -- AWSDynamoDBObjectMapper 来执行此操作.使用 AWSDynamoDBObjectMapper 时,需要向查询方法提供一个 AWSDynamoDBQueryExpression 对象来指定查询条件.AWSDynamoDBQueryExpression 没有为您提供在非关键属性上设置过滤器(条件)的选项.我想知道为什么不支持这个!但是,AWSDynamoDBScanExpression 允许您在使用扫描方法时指定非关键属性的条件.但是,当您真正的意思是查询时,您不想扫描.

You can't do this with the high level API -- AWSDynamoDBObjectMapper. When using AWSDynamoDBObjectMapper, you need to provide an AWSDynamoDBQueryExpression object to the query method to specify the query conditions. AWSDynamoDBQueryExpression doesn't give you the option to set filters(conditions) on non-key attributes. I wonder why this isn't supported! However, AWSDynamoDBScanExpression lets you specify conditions on non-key attributes when you use the scan method. But you don't want to scan when you actually mean a query.

幸运的是,您可以使用低级别 API 执行此操作,方法是直接在 AWSDynamoDB 上调用查询,提供 AWSDynamoDBQueryInput,让您可以指定许多低级别参数.AWSDynamoDBQueryInput 允许您使用 queryFilter 或 filterExpression 指定非键属性的过滤条件.不推荐使用 queryFilter,建议使用 filterExpression.以下是帮助我解决这个问题的两个文件:

Fortunately, you can do this using the low level API by directly calling query on AWSDynamoDB providing an AWSDynamoDBQueryInput which lets you specify a lot of low level parameters. AWSDynamoDBQueryInput lets you specify the filter conditions on non-key attributes using either queryFilter or filterExpression. queryFilter is deprecated, it's recommended to use filterExpression. Here are the two documents that helped me to figure this out:

http://docs.aws.amazon.com/amazondynamodb/最新/APIReference/API_Query.htmlhttp://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSDynamoDBQueryInput.html

这是 swift 中的代码示例.在此代码中,我基于作为非关键属性的已批准"字段进行过滤.recId 为主键:

Here's a code example in swift. In this code I'm filtering based on "approved" field that is a non-key attribute. recId is the primary key:

    func getApprovedRecords(recId: Int) {



     let dynamoDB = AWSDynamoDB.defaultDynamoDB()

        var startKey = nil

        var queryInput = AWSDynamoDBQueryInput()

        queryInput.tableName = TABLE_NAME

        queryInput.limit = QUERY_SIZE

        queryInput.exclusiveStartKey = startKey



        var recIdValue = AWSDynamoDBAttributeValue()

        recIdValue.N = String(recId)

        var recIdCondition = AWSDynamoDBCondition()

        recIdCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ

        recIdCondition.attributeValueList = [recIdValue]



        queryInput.keyConditions = [ "recId"" : recIdCondition]


        var oneValue = AWSDynamoDBAttributeValue()

        oneValue.N = "1"



        queryInput.expressionAttributeValues = [ ":one" : oneValue ]    

        queryInput.filterExpression = "approved = :one"

        dynamoDB.query(queryInput).continueWithBlock { (task: BFTask!) -> AnyObject! in

            if ((task.error) != nil) {

                NSLog("The request failed. Error: (task.error)")

            }

            if ((task.exception) != nil) {

                NSLog("The request failed. Exception: (task.exception)")

            }

            if ((task.result) != nil) {

                NSLog("The request  succeeded.")

                let results = task.result as! AWSDynamoDBQueryOutput

                for r in results.items {

                    // do whatever with the result

                }

            }

            return nil

        }

    }

这篇关于在非关键属性上查询 DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

iOS 6 rotations: supportedInterfaceOrientations doesn#180;t work?(iOS 6 旋转:supportedInterfaceOrientations 不起作用?)
CABasicAnimation rotate returns to original position(CABasicAnimation 旋转返回原始位置)
UITabBarController Rotation Issues in ios 6(ios 6 中的 UITabBarController 旋转问题)
iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))
How to rotate an image 90 degrees on iOS?(如何在 iOS 上将图像旋转 90 度?)
iOS 8 Rotation Methods Deprecation - Backwards Compatibility(iOS 8 旋转方法弃用 - 向后兼容性)