Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I delete an item using DynamoDB Mapper without loading it first?

I am using DynamoDB mapper for deleting an item but have to make sure it exists before deleting it?

So I'm currently doing

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 a way to delete an item without loading it first? I want it to silently return if the item was not found

like image 682
Abhishek Iyer Avatar asked Jan 08 '16 20:01

Abhishek Iyer


People also ask

How do I remove items from DynamoDB?

With the DynamoDB API, you use the DeleteItem action to delete data from a table, one item at a time. You must specify the item's primary key values. In addition to DeleteItem , Amazon DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.

What does DynamoDB Mapper do?

The DynamoDBMapper class is the entry point to Amazon DynamoDB. It provides access to a DynamoDB endpoint and enables you to access your data in various tables. It also enables you to perform various create, read, update, and delete (CRUD) operations on items, and run queries and scans against tables.

Can DynamoDB Mapper query return null?

AWS DynamoDB Mapper query by GSI returns null for all non-key attributes.


1 Answers

Yes, you can!

Simply create an object with the ID 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);
....
like image 69
Federico José Sorenson Avatar answered Oct 03 '22 17:10

Federico José Sorenson