Removing an attribute in DynamoDB

I am trying to find a better way to remove an attribute from an element in Dynamo DB. Below I tried, but I get an exception saying DELETE is not supported for either type N or S.

Exception in the thread "main" Status code: 400, AWS Service: AmazonDynamoDB, AWS Request ID: 09MRO4PVTJ8IK6OHLKSM551REJVV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error code: ValidationException, one or more of the following parameters were not received for the AWS error message: at .amazonaws.http.AmazonHttpClient.handleErrorResponse (AmazonHttpClient.java=44) at com.amazonaws.http.AmazonHttpClient.executeHelper (AmazonHttpClient.java:284) at com.amazonaws.http.AmazonHttp > com.amazonaws.services.dynamodb.AmazonDynamoDBClient.invoke (AmazonDynamoDBClient.java:675) at> com.amazonaws.services.dynamodb.AmazonDynamoDBClient.updateItem (AmazonDynamoDBClient.java opin71)

Key pk = new Key(new AttributeValue().withN(Long.toString(123))); AttributeValueUpdate avu = new AttributeValueUpdate(new AttributeValue().withN("555"), "DELETE"); Map<String, AttributeValueUpdate> m = new HashMap<String, AttributeValueUpdate>(); m.put(String.valueOf(555), avu); UpdateItemRequest uir = new UpdateItemRequest("users", pk, m); dynamoDB.updateItem(uir); 

One point of confusion is why the attribute value matters for deletion. I really want to remove the attribute name and any associated values, but could not find a suitable way to do this in the SDK.

Help will be appreciated.

+6
source share
2 answers

I could swear I already tried this, but by replacing the AttributeValue with a null value that it works:

 Key pk = new Key(new AttributeValue().withN(Long.toString(123))); AttributeValueUpdate avu = new AttributeValueUpdate(null, "DELETE"); Map<String, AttributeValueUpdate> m = new HashMap<String, AttributeValueUpdate>(); m.put(String.valueOf(555), avu); UpdateItemRequest uir = new UpdateItemRequest("users", pk, m); dynamoDB.updateItem(uir); 
+9
source

This also works.

 Table table = dynamoDB.getTable("users"); table.updateItem(new PrimaryKey("<MY HASH KEY NAME>", <MY HASH VALUE>), new AttributeUpdate("columnToRemove").delete()); 

or you can even use expressions in updating items.

REMOVE - removes one or more attributes from an element.

+4
source

Source: https://habr.com/ru/post/907649/


All Articles