I wanted to access Dynamo DB using Boto (Python SDK of AWS), but I couldn't find the Sample Code that was very helpful in the elementary part, so I put the created Code behind the leaflet. Put.
get_item
, query_2
, scan
and
Secret Access Key` have also been created.requests
requests
table is String and ʻuserID, Range Key is Number and
created_date`#!/usr/bin/env python
# coding: utf-8
import boto.dynamodb2
from boto.dynamodb2.table import Table
#AWS Keys for Dynamo
aws_access_key_id = 'AKIASJOE73SDGDTDA'
aws_secret_access_key = 'BuAHZv01Us0pZTbe87987JOIeuoeaM3MO'
aws_region = 'ap-northeast-1'
def main():
conn = boto.dynamodb2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
requests = Table('requests', connection=conn)
# show # of items
print requests.count()
# 'get_key_fields' example to show hash/range-key info for this table
print requests.get_key_fields()
# 'get_item' example to show all attribute of a item which has particular userID/created_date
item = requests.get_item(userID='USER-ID-0123-EDSMQNAK', created_date=143918255309)
for field, val in item.items():
print "%s: %s" % (field, val)
# 'query_2' example to query items with particular userID, and show all attributes for each item
# need to set 'userID__eq', not 'userID'
query_item = requests.query_2(userID__eq='USER-ID-0123-EDSMQNAK')
for item in query_item:
for field, val in item.items():
print '%s: %s' % (field, val)
print '--------------------'
# 'query_2' example to query items of userID (_eq) AND range key (__gt)
query_item = requests.query_2(userID__eq='USER-ID-0123-EDSMQNAK', created_date__gt=143918200000)
# 'scan' example to scan with key "last_access_day" >= "20150810"
query_item = requests.scan(last_access_day__gte=20150810)
if __name__ == '__main__':
main()
get_item
, query_2
, scan
get_item
specifies Key (hash or hash + range_key) and retrieves ** 1 ** best matching items **query_2
Is the Key(hash or hash+range_key)With the search condition, the item to be matchedMultipleTake it out. Only Key can be used as a search condition. range_For keyKeyConditionExpression
Operator(EQ|LE|LT|GE|GT|BEGINS_WITH|BETWEEN
)In greather,Range can be specified such as lessscan
is a so-called full-text search, and it is possible to search by attributes other than Key (but it consumes a large amount of read capacity unit).Recommended Posts