Skip to content

Instantly share code, notes, and snippets.

@noblejasper
Created April 20, 2012 10:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noblejasper/2427656 to your computer and use it in GitHub Desktop.
Save noblejasper/2427656 to your computer and use it in GitHub Desktop.
pythonからbotoを使ってAmazonDynamoDBを叩いてみた。
>>> import boto.dynamodb
>>> boto.dynamodb.regions()
[RegionInfo:us-east-1, RegionInfo:ap-northeast-1, RegionInfo:eu-west-1]
>>> boto.dynamodb.regions()[1]
RegionInfo:ap-northeast-1
>>> type(boto.dynamodb.regions()[1])
<class 'boto.regioninfo.RegionInfo'>
>>> region_info = boto.dynamodb.regions()[1]
>>> conn = region_info.connect(aws_access_key_id='', aws_secret_access_key='')
>>> conn
<boto.dynamodb.layer2.Layer2 object at 0x101dbb510>
>>> conn.list_tables()
[u'nobjas_test']
>>> table = conn.get_table('nobjas_test')
>>> table
Table(nobjas_test)
>>> import datetime
>>> item = dict(test_key='1', name="noblejasper", age=26, birthday=datetime.date(1985,6,11))
>>> item
{'test_key': '1', 'age': 26, 'birthday': datetime.date(1985, 6, 11), 'name': 'noblejasper'}
>>> item = table.new_item(hash_key="noblejasper", attrs=item)
>>> item
{u'test_key': 'noblejasper', 'age': 26, 'birthday': datetime.date(1985, 6, 11), 'name': 'noblejasper'}
>>> item.put()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/item.py", line 187, in put
return self.table.layer2.put_item(self, expected_value, return_values)
File "/Library/Python/2.6/site-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/layer2.py", line 470, in put_item
self.dynamize_item(item),
File "/Library/Python/2.6/site-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/layer2.py", line 132, in dynamize_item
d[attr_name] = dynamize_value(item[attr_name])
File "/Library/Python/2.6/site-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/types.py", line 84, in dynamize_value
dynamodb_type = get_dynamodb_type(val)
File "/Library/Python/2.6/site-packages/boto-2.3.0-py2.6.egg/boto/dynamodb/types.py", line 63, in get_dynamodb_type
raise TypeError(msg)
TypeError: Unsupported type "<type 'datetime.date'>" for value "1985-06-11"
>>> item['birthday']
datetime.date(1985, 6, 11)
>>> item['birthday'] = '1985-06-11'
>>> item.put()
{u'ConsumedCapacityUnits': 1.0}
>>> table.get_item(hash_key='noblejasper')
{u'test_key': 'noblejasper', u'age': 26, u'birthday': u'1985-06-11', u'name': u'noblejasper'}
>>> table.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment