使用Python創建AWS DynamoDB年月表簡單示例

入參可配置表名前綴、後綴、讀寫容量。

運行環境: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章