Search code examples
dynamo-local

AWS DynamoDB SampleData: Create Local Tables


AWS Create Example Tables

Here is the link to the AWS Create Example Tables https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.CreateTables.html. I would like to create a script for the localhost as the follows. I didn't figure out how to create the Reply Table. Could someone help?

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

The data loading

This part should be fine for that's copied from Step 2: Load Data into Tables at link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.LoadData.html

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

Solution

  • Here is the code that worked.

    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