使用Python创建AWS DynamoDB年月表简单示例

1 篇文章 0 订阅
1 篇文章 0 订阅

入参可配置表名前缀、后缀、读写容量。

运行环境: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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值