简体   繁体   English

如何从Android更新DynamoDB中的项目?

[英]How to update item in DynamoDB from Android?

The title describes the question that's been surrounding my pain for three days. 标题描述了困扰我三天的问题。 How to update an item stored in DynamoDB from an Android app? 如何从Android应用程序更新DynamoDB中存储的项目?

Below I left a list of links I already checked and implemented with unsuccessful results: 在下面,我留下了已经检查并实现失败的链接列表:

1 - https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-dynamodb-items.html Result: it is strictly for java ee, not android 1- https : //docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-dynamodb-items.html结果:严格来说,它仅适用于Java ee,不适用于android

2 - https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud-update Result: creates a new item even though the "unique-user-id" parameter is clearly unique 2- https : //docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud-update结果:创建一个新项目,即使“ unique-user-id”参数显然是唯一的

3 - https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-nosql-integrate-an-existing-table.html Result: same as above, creates a new one 3- https : //docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-nosql-integrate-an-existing-table.html结果:与上述相同,创建了一个新的

4 - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.CRUDExample1.html Result: retrieves but creates a new one item 4- https : //docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.CRUDExample1.html结果:检索但创建一个新项目

Thing I was doing kind of wrong: -Not including my sort (aka Range) key in my object to be send for the update. 我在做某种错误的事情:-在要发送更新的对象中不包括我的sort(又称Range)键。

For the rest: First, get your DynamoDBClient, I'd strongly suggest implementing a singleton pattern for calling this, I used the configuration.json in this case: 剩下的:首先,获得您的DynamoDBClient,我强烈建议实现一个单例模式来调用它,在这种情况下,我使用了configuration.json:

AWSMobileClient.getInstance().initialize(NewPatientActivity.this).execute();
    AWSCredentialsProvider credentialsProvider = AWSMobileClient.getInstance().getCredentialsProvider();
    AWSConfiguration configuration = AWSMobileClient.getInstance().getConfiguration();
    // Add code to instantiate a AmazonDynamoDBClient
    AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);

Then set an UpdateItemRequest like this: 然后像这样设置一个UpdateItemRequest:

//SET THE KEYS VALUES
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
            key.put("your-partition-key", new AttributeValue().withS(value));
            key.put("your-sort-key-if-exists", new AttributeValue().withS(value));
//SET VALUES TO REPLACE OLD ONES
 Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
            expressionAttributeValues.put(":val1",new AttributeValue().withN(value1));
            expressionAttributeValues.put(":val2", new AttributeValue().withS(value2));
/*
IF YOU WANT TO READ THE JUST-UPDATED ITEM, HAVE THIS READY
            ReturnValue returnValues = ReturnValue.ALL_NEW;

*/
//SET THE UPDATEITEMREQUEST
      UpdateItemRequest updateItemRequest = new UpdateItemRequest()
                    .withTableName("your-table-name")
                    //KEYS DEFINED ABOVE                        
                    .withKey(key)
                    //SET WHERE TO UPDATE
                    .withUpdateExpression("set attr1 = :val1, attr2 = :val2")
                .withExpressionAttributeValues(expressionAttributeValues)
                   //INDICATE TO RETURN UPDATED ITEM                     
                  .withReturnValues(returnValues);
   //GET THE RESULT OF YOUR UPDATE AND EXECUTE IT
     UpdateItemResult result = your-dynamo-client.updateItem(updateItemRequest);
    //convert it to a string                
     Gson gson = new Gson();
     String x = gson.toJson(result);
    //Check it out                
     Log.e("RESULT :",x);

For further information, I elaborated this gist. 有关更多信息,我详细阐述了这一要点。

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

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