Deleting an item from the DynamoDB table using the AWS SDK for Java
Let's understand how to delete an item from the DynamoDB table using the AWS SDK for Java.
Getting ready
To perform this operation, you can use the IDE of your choice.
How to do it…
Let's try to understand how to delete a stored item from the DynamoDB
table using Java:
Create an instance of the
DynamoDB
class and initialize it with the desired AWS region and credentials:AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider()); client.setRegion(Region.getRegion(Regions.US_EAST_1)); DynamoDB dynamoDB = new DynamoDB(client);
Get the table from which you wish to delete the item:
Table table = dynamoDB.getTable("productTable");
Invoke the
deleteItem
method from theTable
class by specifying the item's primary key. Here, in our case, we are using the composite hash and range keys, so we will have to mention both of them:DeleteItemOutcome outcome = table.deleteItem(new PrimaryKey("id", 10, "type", "phone...