简体   繁体   English

如何使用 Swift 将字典(地图对象)保存到 DynamoDB

[英]How do I save dictionary (map object) to DynamoDB using Swift

I'm trying to save a dictionary to my DynamoDB table field using low-level API.我正在尝试使用低级 API 将字典保存到我的 DynamoDB 表字段。 I couldn't figure out how to do it with object mapper.我无法弄清楚如何使用对象映射器来做到这一点。 There is no example for this in AWS iOS documentation and I've tried to research and implement Java / .NET examples of the same subject unsuccessfully. AWS iOS 文档中没有这方面的示例,我曾尝试研究和实施同一主题的 Java / .NET 示例,但未成功。

I want to update only the dictionary field in the row using updateExpression.我只想使用 updateExpression 更新行中的字典字段。

I stumbled to this question while searching for answer, but it didn't help: Best way to make Amazon AWS DynamoDB queries using Swift?我在寻找答案时偶然发现了这个问题,但它没有帮助: Best way to make Amazon AWS DynamoDB query using Swift?

Here's the function to update dynamoDB-table:这是更新 dynamoDB 表的函数:

func saveMapToDatabase(hashKey:Int, rangeKey:Double, myDict:[Int:Double]){
    let nsDict:NSDictionary = NSDictionary(dictionary: myDict)
    var dictAsAwsValue = AWSDynamoDBAttributeValue();dictAsAwsValue.M = nsDict as [NSObject : AnyObject]

    let updateInput:AWSDynamoDBUpdateItemInput = AWSDynamoDBUpdateItemInput()
    let hashKeyValue:AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue();hashKeyValue.N = String(hashKey)
    let rangeKeyValue:AWSDynamoDBAttributeValue = AWSDynamoDBAttributeValue(); rangeKeyValue.N = String(stringInterpolationSegment: rangeKey)

    updateInput.tableName = "my_table_name"
    updateInput.key = ["db_hash_key" :hashKeyValue, "db_range_key":rangeKeyValue]

    //How I usually do low-level update:
    //let valueUpdate:AWSDynamoDBAttributeValueUpdate = AWSDynamoDBAttributeValueUpdate()
    //valueUpdate.value = dictAsAwsValue
    //valueUpdate.action = AWSDynamoDBAttributeAction.Put
    //updateInput.attributeUpdates = ["db_dictionary_field":valueUpdate]

    //Using the recommended way: updateExpression
    updateInput.expressionAttributeValues = ["dictionary_value":dictAsAwsValue]
    updateInput.updateExpression = "SET db_dictionary_field = :dictionary_value"

    self.dynamoDB.updateItem(updateInput).continueWithBlock{(task:BFTask!)->AnyObject! in
        //do some debug stuff
        println(updateInput.aws_properties())
        println(task.description)

        return nil
    }
}

I solved it, the problem was that AWS requires dictionary keys to always be in the form of String, any other type is not allowed.我解决了它,问题是 AWS 要求字典键始终采用 String 的形式,不允许任何其他类型。

The working solution snippet:工作解决方案片段:

...
updateInput.tableName = "my_table_name"
updateInput.key = ["db_hash_key" :hashKeyValue, "db_range_key":rangeKeyValue]

let dictionaryInRightFormat:NSDictionary = ["stringKey":dictAsAwsValue]
updateInput.expressionAttributeValues = updateInput.updateExpression = "SET db_dictionary_field = :stringKey"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM