python3 で DynamoDB を使う
は、AWS 上の DynamoDB に対しての操作です。
LAN 内に、DynamoDB をインストールして、その DynamoDB に CRUD を行うサンプルです。
1)インストール
dynamodb_local_latest.tar.gz をダウンロードして解凍します。
そして、実行します。
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar
ポート 8000 を使います。
atami という端末にインストールした場合のサンプルです。
- Create
dynamo_create.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_create.py
#
# Oct/20/2017
# ---------------------------------------------------------------
import sys
import boto3
from decimal import *
# ---------------------------------------------------------------
def insert_proc (key,name,population,date_mod):
response = table.put_item ( \
Item={'key': key,'name': name,'population': population,'date_mod':date_mod}
)
# ---------------------------------------------------------------
def create_table_proc():
table = dynamodb.create_table(
TableName='cities',
KeySchema=[
{
'AttributeName': 'key',
'KeyType': 'HASH' #Partition key
},
],
AttributeDefinitions=[
{'AttributeName': 'key', 'AttributeType': 'S' },
],
ProvisionedThroughput={
'ReadCapacityUnits': 30,
'WriteCapacityUnits': 30
}
)
print("Table status:", table.table_status)
# ---------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
dynamodb = boto3.resource('dynamodb',endpoint_url="http://atami:8000")
create_table_proc()
table = dynamodb.Table('cities')
insert_proc ("t0921","宇都宮",Decimal ("47219"),"2003-2-12")
insert_proc ("t0922","小山",Decimal ("79814"),"2003-5-24")
insert_proc ("t0923","佐野",Decimal ("91845"),"2003-7-15")
insert_proc ("t0924","足利",Decimal ("59837"),"2003-6-9")
insert_proc ("t0925","日光",Decimal ("49871"),"2003-4-16")
insert_proc ("t0926","下野",Decimal ("39812"),"2003-10-8")
sys.stderr.write ("*** 終了 ***\n")
# ---------------------------------------------------------------
- Read
dynamo_read.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_read.py
#
# Oct/21/2017
# --------------------------------------------------------------------
import sys
import boto3
sys.stderr.write("*** 開始 ***\n")
dynamodb = boto3.resource('dynamodb',endpoint_url="http://atami:8000")
table = dynamodb.Table('cities')
response = table.scan()
for it in response['Items']:
str_out = it['key'] + '\t'
str_out += it['name'] + '\t'
str_out += str (it['population']) + '\t'
str_out += it['date_mod']
print(str_out)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
- Update
dynamo_update.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_update.py
#
# Oct/21/2017
# --------------------------------------------------------------------
import sys
import datetime
from decimal import *
import boto3
from boto3.dynamodb.conditions import Key
# --------------------------------------------------------------------
def display_data_proc(key_in):
response = table.query(
KeyConditionExpression=Key('key').eq(key_in)
)
for it in response['Items']:
str_out = it['key'] + '\t'
str_out += it['name'] + '\t'
str_out += str (it['population']) + '\t'
str_out += it['date_mod']
print(str_out)
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
key_in = sys.argv[1]
population_in = int(sys.argv[2])
print("%s\t%d" % (key_in, population_in))
dynamodb = boto3.resource('dynamodb',endpoint_url="http://atami:8000")
table = dynamodb.Table('cities')
#
#
display_data_proc(key_in)
#
response = table.update_item(Key={'key': key_in},
UpdateExpression="SET population = :updated",
ExpressionAttributeValues={':updated': Decimal (str(population_in))})
#
date_mod = '%s' % datetime.date.today()
response = table.update_item(Key={'key': key_in},
UpdateExpression="SET date_mod = :updated",
ExpressionAttributeValues={':updated': date_mod})
#
display_data_proc(key_in)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
- Delete
dynamo_delete.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# dynamo_delete.py
#
# Oct/21/2017
# --------------------------------------------------------------------
import sys
import boto3
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
key_in = sys.argv[1]
print("%s" % key_in)
dynamodb = boto3.resource('dynamodb',endpoint_url="http://atami:8000")
table = dynamodb.Table('cities')
response = table.delete_item(
Key={
'key': key_in,
}
)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------