AWS DynamoDB SampleData:创建本地表

时间:2019-06-17 14:13:11

标签: dynamo-local

AWS创建示例表

这里是AWS Create Example Tables https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.CreateTables.html的链接。我想为本地主机创建一个脚本,如下所示。我不知道如何create the Reply Table。有人可以帮忙吗?

export LOCAL="--endpoint-url http://localhost:8000"

aws dynamodb create-table \
    $LOCAL \
    --table-name ProductCatalog \
    --attribute-definitions \
        AttributeName=Id,AttributeType=N
    --key-schema \
        AttributeName=Id,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5

aws dynamodb create-table \
    $LOCAL \
    --table-name Forum \
    --attribute-definitions \
        AttributeName=Name,AttributeType=S
    --key-schema \
        AttributeName=Name,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5


aws dynamodb create-table \
    $LOCAL \
    --table-name Thread \
    --attribute-definitions \
        AttributeName=ForumName,AttributeType=S \
        AttributeName=Subject,AttributeType=S \
    --key-schema \
        AttributeName=ForumName,KeyType=HASH \
        AttributeName=Subject,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5


aws dynamodb create-table \
    $LOCAL \
    --table-name Reply \
    --attribute-definitions \
        AttributeName=Id,AttributeType=S
        AttributeName=ReplyDateTime,AttributeType=S
    --key-schema \
        AttributeName=Id,KeyType=HASH \
        AttributeName=ReplyDateTime,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5

数据加载

这部分应该很好,因为它是从链接https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.LoadData.htmlStep 2: Load Data into Tables复制而来的

aws dynamodb batch-write-item --request-items file://ProductCatalog.json
aws dynamodb batch-write-item --request-items file://Forum.json
aws dynamodb batch-write-item --request-items file://Thread.json
aws dynamodb batch-write-item --request-items file://Reply.json

1 个答案:

答案 0 :(得分:0)

这是起作用的代码。

export LOCAL="--endpoint-url http://localhost:8000"

aws dynamodb delete-table $LOCAL \
    --table-name ProductCatalog

aws dynamodb create-table \
    $LOCAL \
    --table-name ProductCatalog \
    --attribute-definitions \
        AttributeName=Id,AttributeType=N \
    --key-schema \
        AttributeName=Id,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5

aws dynamodb delete-table $LOCAL \
    --table-name Forum

aws dynamodb create-table \
    $LOCAL \
    --table-name Forum \
    --attribute-definitions \
        AttributeName=Name,AttributeType=S \
    --key-schema \
        AttributeName=Name,KeyType=HASH \
    --provisioned-throughput \
        ReadCapacityUnits=10,WriteCapacityUnits=5

aws dynamodb delete-table $LOCAL \
    --table-name Thread

# https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html
aws dynamodb create-table \
    $LOCAL \
    --table-name Thread \
    --attribute-definitions \
        AttributeName=ForumName,AttributeType=S \
        AttributeName=Subject,AttributeType=S \
        AttributeName=LastPostDateTime,AttributeType=S \
    --key-schema \
        AttributeName=ForumName,KeyType=HASH \
        AttributeName=Subject,KeyType=RANGE \
    --global-secondary-indexes \
       IndexName=LastPostIndex,KeySchema=["\
       {AttributeName=ForumName,KeyType=HASH}","\
       {AttributeName=LastPostDateTime,KeyType=RANGE}"],Projection="\
       {ProjectionType=ALL}",ProvisionedThroughput="\
       {ReadCapacityUnits=5,WriteCapacityUnits=5}" \
    --provisioned-throughput \
        ReadCapacityUnits=5,WriteCapacityUnits=5 \
    --tags \
        Key=Owner,Value=BlueTeam

aws dynamodb delete-table $LOCAL \
            --table-name Reply

aws dynamodb create-table $LOCAL \
    --table-name Reply \
    --attribute-definitions \
        AttributeName=Id,AttributeType=S \
        AttributeName=ReplyDateTime,AttributeType=S \
        AttributeName=PostedBy,AttributeType=S \
        AttributeName=Message,AttributeType=S \
    --key-schema AttributeName=Id,KeyType=HASH \
        AttributeName=ReplyDateTime,KeyType=RANGE \
    --global-secondary-indexes \
        IndexName=PostedBy-Message-Index,KeySchema=["\
        {AttributeName=PostedBy,KeyType=HASH}","\
        {AttributeName=Message,KeyType=RANGE}"],Projection="{ProjectionType=INCLUDE \
        ,NonKeyAttributes=["ReplyDateTime"]}",ProvisionedThroughput="\
        {ReadCapacityUnits=10,WriteCapacityUnits=10}" \
    --provisioned-throughput \
        ReadCapacityUnits=5,WriteCapacityUnits=4


# data loading

aws dynamodb batch-write-item $LOCAL --request-items file://ProductCatalog.json
aws dynamodb batch-write-item $LOCAL --request-items file://Forum.json
aws dynamodb batch-write-item $LOCAL --request-items file://Thread.json
aws dynamodb batch-write-item $LOCAL --request-items file://Reply.json