简体   繁体   English

如何部分更新DynamoDB表?

[英]How to Partially Update a DynamoDB Table?

We're having a lot of trouble doing a partial update in DynamoDB. 我们在DynamoDB中进行部分更新时遇到了很多麻烦。

Trying to use best practices in NoSQL by using aggregates instead of entities so our data looks a bit like this: 尝试使用聚合而不是实体来使用NoSQL中的最佳实践,因此我们的数据看起来有点像这样:

[
{
    "userLogin": {
        "ipAddress": "123.222.121.1234",
        "lastLogin": "2017-10-16T17:38:59.818Z",
        "userAgent": "bob",
        "geoLocation": "lat-121 long-312"
    },
    "addresses": {
        "billingAddress": {
            "streetName": "york street",
            "province": "ontario",
            "city": "toronto",
            "streetNumber": "18",
            "postalCode": "m5a2v7"
        },
        "businessAddress": {
            "streetName": "york street",
            "province": "ontario",
            "city": "toronto",
            "streetNumber": "18",
            "postalCode": "m5a2v7"
        },
        "mailingAddress": {
            "streetName": "york street",
            "province": "ontario",
            "city": "toronto",
            "streetNumber": "18",
            "postalCode": "m5a2v7"
        },
    },
    "paymentTransaction": {
        "orderId": 5,
        "amount": 75,
        "transactionTimestamp": "2017-10-16T17:38:59.818Z"
    },
    "userId": "00uc4sxyi7gfQoFum0h7",
    "phoneNumbers": {
        "workNumber": "647-123-1234",
        "homeNumber": "647-321-4321"
    },
    "userProfile": {
        "role": "admin",
        "verifiedTimeStamp": "2017-10-16T17:38:59.818Z",
        "termsConditionTimeStamp": "2017-10-16T17:38:59.818Z",
        "verified": "TRUE",
        "createdTimeStamp": "2017-10-16T17:38:59.818Z",
        "termsConditionVersion": "1.0",
        "email": "kyle.truong@test.io"
    }
}

] ]

All we want to do is make a request with a body like this: 我们要做的就是用这样的身体发出请求:

PUT /api/user-profiles/00uc4sxyi7gfQoFum0h7
body: {
    "userLogin": { "lastLogin": "2017-12-16T17:38:59.818Z" }
}

and have it update the one attribute in the User table. 并让它更新User表中的one属性。

The updateItem API seems to require defining the attributes you want to update before updating them, but we want it be more flexible and dynamic based on the body of the request. updateItem API似乎需要在更新之前定义要更新的属性,但我们希望它基于请求的主体更灵活和动态。

This thread seems to say it's not possible: 这个帖子好像说不可能:

How to update multiple items in a DynamoDB table at once 如何一次更新DynamoDB表中的多个项目

If so, what's the best way to go about updating just a partial attribute object within an item in DynamoDB? 如果是这样,那么在DynamoDB中只更新项目中的部分属性对象的最佳方法是什么?

In dynamoDB if your userLogin is a map type then you can update the value of lastLoginkey. 在dynamoDB中,如果userLogin是地图类型,则可以更新lastLoginkey的值。 In Java below code may help. 在Java下面的代码可能有所帮助。 Here column name would be userLogin and newKey would be lastLogin. 这里的列名是userLogin,newKey是lastLogin。 Full code snippet can be found here 完整的代码片段可以在这里找到

UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey,primaryKeyValue).withReturnValues(ReturnValue.ALL_NEW).
        withUpdateExpression("set #columnName." + newKey + " = :columnValue").
        withNameMap(new NameMap().with("#columnName", updateColumn)).
        withValueMap(new ValueMap().with(":columnValue", newValue)).withConditionExpression("attribute_exists("+ updateColumn +")");

table.updateItem(updateItemSpec);

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

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