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'
)

 

 

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