aws-cli dynamodb创建具有多个二级索引的表

时间:2018-09-19 09:15:58

标签: amazon-dynamodb aws-cli

我试图用2个本地二级索引创建一个dynamodb表。我执行了以下操作,仅应用了后者index(index-2)。正确的方法是什么?

aws dynamodb create-table \
 --table-name test_table_name \
 --attribute-definitions \
     AttributeName=type,AttributeType=S \
     ...
 --key-schema \
     AttributeName=type,KeyType=HASH \
     AttributeName=id,KeyType=RANGE \
 --provisioned-throughput \
     ReadCapacityUnits=5,WriteCapacityUnits=5 \
 --local-secondary-indexes \
     'IndexName=index-1,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk1,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[A,B,C,D]}' \
 --local-secondary-indexes \
     'IndexName=index-2,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk2,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[B,D,F,H]}' \
 --region us-east-1

1 个答案:

答案 0 :(得分:2)

您只需要指定单个--local-secondary-indexes,例如

之前

 --local-secondary-indexes \
     'IndexName=index-1,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk1,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[A,B,C,D]}' \
 --local-secondary-indexes \
     'IndexName=index-2,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk2,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[B,D,F,H]}' \

之后

 --local-secondary-indexes \
     'IndexName=index-1,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk1,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[A,B,C,D]}' \
     'IndexName=index-2,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk2,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[B,D,F,H]}' \

最终

aws dynamodb create-table \
 --table-name test_table_name \
 --attribute-definitions \
     AttributeName=type,AttributeType=S \
     ...
 --key-schema \
     AttributeName=type,KeyType=HASH \
     AttributeName=id,KeyType=RANGE \
 --provisioned-throughput \
     ReadCapacityUnits=5,WriteCapacityUnits=5 \
 --local-secondary-indexes \
     'IndexName=index-1,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk1,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[A,B,C,D]}' \
     'IndexName=index-2,KeySchema=[{AttributeName=type,KeyType=HASH},{AttributeName=sk2,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[B,D,F,H]}' \
 --region us-east-1