Basically, you store objects (representable in JSON), matched to a key. Your key can be a string or an int (and actually, DynamoDB allows your key to be composed of two fields, a hash and a range, but here we will only do the hash). Your object is just a dictionary mapping fields to values.
The best python library for DynamoDB (and for most other aws services) is boto. The easiest way to install boto is with pip; just do pip install boto or sudo pip install boto.
Amazon offers a free tier for DynamoDB, which should be enough for playing and small applications (the DB has to be less than 100M in size, and you get 5KB in reads and 1KB in writes per second), so head down to aws.amazon.com and create your account, if you don't have it; after making your account, go to security credentials and find out your access key id and your secret access key. You may want to store your credentials in a file called .boto in your home folder, as explained here. After that, you can connect to your dynamodb as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
conn=boto.connect_dynamodb() # if set in ~/.boto | |
# or | |
conn = boto.connect_dynamodb(aws_access_key_id='...',aws_secret_access_key='...') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myschema=conn.create_schema(hash_key_name='username',hash_key_proto_value='S'); | |
table=conn.create_table(name='users', schema=myschema, read_units=1, write_units=1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user_data={'name':'Orlando Karam', 'password':'abc123', 'email':'ok@ok.com'}; | |
user=table.new_item(hash_key='okaram', attrs=user_data); | |
user.put(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it=table.get_item("okaram") | |
# or select only some fields | |
it=table.get_item("okaram", attributes_to_get=['email']) |
Most of the times you will be searching for a given key (or at least will know a part of the key), but if not, you can use the scan method (notice this method goes through EVERY object on the table, and so may be slow and consume a lot of I/O). You can pass a list of conditions (as of now, the documentation is incorrect, stating you specify the conditions with strings; you need to use elements coming from boto.dynamodb.condition). A scan would look like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users=table.scan(scan_filter={'username':CONTAINS('o')}) | |
# and later we cab iterate like | |
for user in users: | |
print user; |
- Amazon's DynamoDB page
- boto's github
- Boto's DynamoDB tutorial
- Boto's DynamoDB API reference
users=table.scan(scan_filter={'username':CONTAINS('o')})
ReplyDeleteit doesn't return any data for me it returns "" as result and i have data with that content in my table. Kindly help me..
The problem is with your imports...You can do it this way:
Deleterecords = table.scan(scan_filter={'username':boto.dynamodb.condition.CONTAINS('Mik')})
And don't forget to import "import boto.dynamodb.condition"
Are you sure you have data that has lowercase o ? maybe you have it only uppercase ? did you use 'username' in lowercase ?
ReplyDeleteActually, I see a typo in my code ... user_data has a name attribute, not username ... try users=table.scan(scan_filter={'name':CONTAINS('o')})