入参可配置表名前缀、后缀、读写容量。
运行环境:python3
import boto3
import json
#加载AWS环境变量
def load_json(path):
try:
with open(path) as json_file:
data = json.load(json_file)
except Exception as e:
print('ERROR: no such file like ' + path)
exit(-1)
else:
return data
def __init__(path):
conf = load_json(path)
dynamo_db = boto3.resource('dynamodb',region_name=conf['region_name'],aws_access_key_id=conf['aws_access_key_id'], aws_secret_access_key=conf['aws_secret_access_key'])
return dynamo_db
#构建表元素
def create_table_biao_YYYYMM(tableprefix,quarter,read,write,iread,iwrite,dynamodb):
tablename = tableprefix+'_biao_'+quarter
table = dynamodb.create_table(
TableName=tablename,
KeySchema=[
{
'AttributeName': 'acc_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'meeid_userid',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'acc_id',
'AttributeType': 'S'
},
{
'AttributeName': 'time',
'AttributeType': 'S'
},
{
'AttributeName': 'meeid_userid',
'AttributeType': 'S'
},
{
'AttributeName': 'status',
'AttributeType': 'N'
}
],
LocalSecondaryIndexes=[
{
'IndexName': 'lidx_biao_time',
'Projection': {
'ProjectionType': 'ALL'
},
'KeySchema': [
{
'AttributeName': 'acc_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'time',
'KeyType': 'RANGE'
}
],
},
{
'IndexName': 'lidx_biao_status',
'Projection': {
'ProjectionType': 'INCLUDE',
'NonKeyAttributes': [
'bind'
],
},
'KeySchema': [
{
'AttributeName': 'acc_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'status',
'KeyType': 'RANGE'
}
],
},
],
GlobalSecondaryIndexes=[
{
'IndexName': 'gidx_biao_status',
'KeySchema': [
{
'AttributeName': 'status',
'KeyType': 'HASH'
},
],
'Projection': {
'ProjectionType': 'INCLUDE',
'NonKeyAttributes': [
'jointime'
],
},
'ProvisionedThroughput': {
'ReadCapacityUnits': iread,
'WriteCapacityUnits': iwrite
}
},
],
ProvisionedThroughput={
'ReadCapacityUnits': read,
'WriteCapacityUnits': write
}
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName=tablename)
response = table.meta.client.describe_table(TableName=tablename)
print(response)
##begin###
dynamodb_db=__init__('./devaws.json')
#prefix_biao_201901
create_table_biao_YYYYMM('prefix','201901',10,10,dynamodb_db)
##end###
AWS环境变量配置文件示例:
{
"region_name":"us-east-1",
"aws_access_key_id":"AKXXXXXXXXXXXXXXXXXX",
"aws_secret_access_key":"VVYYYYYYYYYYYYYYYYYYYYYYYYY"
}
更多表字段配置参考:aws . dynamodb