Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query AWS DynamoDB in python?

Tags:

I'm fairly new to NoSQL and using AWS DynamoDB. I'm calling it from AWS Lambda using python 2.7 I'm trying to retrieve a value from an order_number field.
This is what my table looks like(only have one record.):
enter image description here

primary partition key: subscription_id enter image description here


and my secondary global index: order_number
enter image description here

Is my setup correct? If so given the order_number how do I retrieve the record using python?
I can't figure out the syntax to do it.

I've tried

response = table.get_item( Key = {'order_number': myordernumber} ) 

But i get:
An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema: ClientError

like image 930
MalcolmInTheCenter Avatar asked Dec 09 '15 05:12

MalcolmInTheCenter


People also ask

Can you run SQL queries on DynamoDB?

You now can use a SQL-compatible query language to query, insert, update, and delete table data in Amazon DynamoDB. You now can use PartiQL (a SQL-compatible query language)—in addition to already-available DynamoDB operations—to query, insert, update, and delete table data in Amazon DynamoDB.


2 Answers

DynamoDB does not automatically index all of the fields of your object. By default you can define a hash key (subscription_id in your case) and, optionally, a range key and those will be indexed. So, you could do this:

response = table.get_item(Key={'subscription_id': mysubid}) 

and it will work as expected. However, if you want to retrieve an item based on order_number you would have to use a scan operation which looks through all items in your table to find the one(s) with the correct value. This is a very expensive operation. Or you could create a Global Secondary Index in your table that uses order_number as the primary key. If you did that and called the new index order_number-index you could then query for objects that match a specific order number like this:

from boto3.dynamodb.conditions import Key, Attr  response = table.query(     IndexName='order_number-index',     KeyConditionExpression=Key('order_number').eq(myordernumber)) 

DynamoDB is an very fast, scalable, and efficient database but it does require a lot of thought about what fields you might want to search on and how to do that efficiently.

The good news is that now you can add GSI's to an existing table. Previously you would have had to delete your table and start all over again.

like image 105
garnaat Avatar answered Sep 21 '22 05:09

garnaat


Make sure you've imported this:

from boto3.dynamodb.conditions import Key, Attr 

If you don't have it, you'll get the error for sure. It's in the documentation examples.

Thanks @altoids for the comment above as this is the correct answer for me. I wanted to bring attention to it with a "formal" answer.

like image 24
user3303554 Avatar answered Sep 17 '22 05:09

user3303554