Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto DynamoDB2 conditional put_item

I am trying to have put_item to check if there is item with the same HashKey before actually adding the new item.

According to boto DynamoDB2 document, it is possible to do it with "Conditional Put".

I tried following command but no luck.

connection.put_item('table',item={'locationId':'a1', 'timestamp': time.time()}, expected={'locationID':False})

The error message is as following.

boto.exception.JSONResponseError: JSONResponseError: 400 Bad Request
{u'Message': u'Expected null', u'__type': u'com.amazon.coral.service#SerializationException'}

Does anyone have a conditional put with DynamoDBv2?

Thanks to all in advance.

like image 632
nicklee Avatar asked Aug 08 '13 03:08

nicklee


1 Answers

You need to write it like this for the syntax to work. It's extremely bad that this isn't documented anywhere. I just had the exact same problem and had to try 50 different things while digging through the Java SDK documentation for it to work. Note that you need to give a value if you want to go with 'Exists': True instead of False.

connection.put_item(
    'table',
    item={
        'locationId':{'S': 'a1'},
        'timestamp': {'N': str(time.time())
    },
    expected={
        'locationID': {'Exists': False}
    }
    #expected={
    #    'locationID': {'Exists': True, 'Value': {'N': '0'}}
    #}
)

Hope it helps!

Edit: the question that helped me with the syntax enough to make it work and code in the boto integration tests

like image 170
moodh Avatar answered Sep 29 '22 23:09

moodh