Is it possible to delete an item using DynamoDB Mapper without loading it first?

I use mapper DynamoDB to delete an item, but before you delete it, you need to make sure that it exists?

So i am doing now

public void delete(final String hashKey, final Long rangeKey) { final Object obj = mapper.load(Object.class, hashKey, rangeKey); if (obj != null) { mapper.delete(obj); } } 

If there is a way to delete an item without first loading it? I want it to silently return if the item was not found.

+6
source share
2 answers

Yes, you can!

Just create an object with the identifier you want to delete, and pass it as an object to the delete method:

 ... MyObject object = new MyObject(); object.setHashKey(hashKey); object.setRangeKey(rangeKey); mapper.delete(object); .... 
+4
source

You can also do this with an instance of com.amazonaws.services.dynamodbv2.document.Table :

 table.deleteItem("hashKeyAttributeName", hashKey, "rangeKeyAttribureName", rangeKey); 

To create an instance of a table, you can use the following:

 BasicAWSCredentials credentials = new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey); // set access and secret keys AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withRegion("us-west-1") // set aws region .build(); Table table = new DynamoDB(amazonDynamoDB).getTable(tableName); 
+2
source

All Articles