1) 您需要使用query 来获取分区键的所有排序键。请参考以下链接。
Query API
Query sample code
2) 使用 AWS CLI 命令创建 GSI。
本地 DynamoDB:-
您可能需要删除端点 url 并包含适当的区域 --region us-east-1。另外,请相应地更改表名。
aws dynamodb update-table --table-name Movies --attribute-definitions file://create_gsi_attributes.json --global-secondary-index-updates file://create_gsi.json --endpoint-url http://localhost:8000
create_gsi_attributes.json:-
请将属性名称(和类型)更改为conversation_id 和user_id
[{
"AttributeName": "title",
"AttributeType": "S"
},
{
"AttributeName": "yearkey",
"AttributeType": "N"
}]
create_gsi.json:-
请将关键架构属性名称更改为conversation_id 和user_id
[{
"Create": {
"IndexName": "Movies_Gsi",
"KeySchema": [{
"AttributeName": "title",
"KeyType": "HASH"
},
{
"AttributeName": "yearkey",
"KeyType": "RANGE"
}],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 100,
"WriteCapacityUnits": 100
}
}
}]
编辑:-
命令:-
aws dynamodb update-table --table-name message_participants_tbl --attribute-definitions file://create_gsi_attributes_conversation.json --global-secondary-index-updates file://create_gsi_conversation.json --endpoint-url http://localhost:8000
create_gsi_conversation.json:-
[{
"Create": {
"IndexName": "message_participants_tbl_gsi",
"KeySchema": [{
"AttributeName": "conversation_id",
"KeyType": "HASH"
},
{
"AttributeName": "user_id",
"KeyType": "RANGE"
}],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 100,
"WriteCapacityUnits": 100
}
}
}]
create_gsi_attributes_conversation.json:-
[{
"AttributeName": "user_id",
"AttributeType": "S"
},
{
"AttributeName": "conversation_id",
"AttributeType": "S"
}]