AWS DynamoDB(二)--- 创建一个DynamoDB

创建一个DynamoDB的四种方法:

1. 在DynamoDB页面,创建一个

2. 通过cloudformation创建一个

创建的模板 JSON代码如下:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "PetTable": {
            "Type": "AWS::DynamoDB::Table",
            "Properties": {
                "AttributeDefinitions" : [
                    {
                        "AttributeName": "pet_species",
                        "AttributeType": "S"
                    },
                    {
                        "AttributeName": "pet_id",
                        "AttributeType": "N"
                    }
                ],
                "KeySchema" : [
                    {
                        "AttributeName": "pet_species",
                        "KeyType": "HASH"
                    },
                    {
                        "AttributeName": "pet_id",
                        "KeyType": "RANGE"
                    }
                ],
                "TableName": "PetInventory",
                "BillingMode": "PAY_PER_REQUEST"
            }
        }
    }
}

3. 通过awscli

以下命令可以创建:

aws dynamodb\
    create-table\
        --table-name PetInventory\
        --attribute-definitions\
            AttributeName=pet_species,AttributeType=S\
            AttributeName=pet_id,AttributeType=S\
        --key-schema\
            AttributeName=pet_species,KeyType=HASH\
            AttributeName=pet_id,KeyType=RANGE\
        --billing-mode PAY_PER_REQUEST

4. Python创建

脚本如下:

import boto3
ddb = boto3.client('dynamodb')
createResponse = ddb.create_table(
    AttributeDefinitions=[
        {
            'AttributeName':'pet_species',
            'AttributeType': 'S',
        }, 
        {
            'AttributeName':'pet_id',
            'AttributeType':'N'
        }
    ], 
    KeySchema=[
        {
            'AttributeName':'pet_species',
            'KeyType':'HASH'
        },
        {
            'AttributeName':'pet_id',
            'KeyType':'RANGE'
        },
    ],
    BillingMode = 'PAY_PER_REQUEST',
    TableName='PetInventory'
)
发布了140 篇原创文章 · 获赞 80 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/daiqinge/article/details/103368103