B. León
B. León

Reputation: 549

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?

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

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

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

4 - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.CRUDExample1.html Result: retrieves but creates a new one item

Upvotes: 2

Views: 971

Answers (1)

B. León
B. León

Reputation: 549

Thing I was doing kind of wrong: -Not including my sort (a.k.a. Range) key in my object to be send for the update.

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:

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:

//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.

Upvotes: 0

Related Questions