DynamoDB 查询输入:如何检索键值不为空的所有项目
DynamoDB Query Input: How to retrieve all items where key value is not null
我有以下架构:
resources:
Resources:
ChatDynamoDbTable:
Type: "AWS::DynamoDB::Table"
DeletionPolicy: Retain
Properties:
TableName: "chats-admin-${opt:stage, self:provider.stage}"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: admin_type
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
-
IndexName: user_id-admin_type-index
KeySchema:
-
AttributeName: user_id
KeyType: HASH
-
AttributeName: admin_type
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
我需要获取 admin_type
与我提供的密钥匹配的所有项目。
我尝试了以下但失败了,因为我没有指定 user_id
的值:
"error_message":"ValidationException: Query condition missed key
schema element: user_id
input := &dynamodb.QueryInput{
TableName: aws.String(table),
IndexName: aws.String("user_id-admin_type-index"),
KeyConditionExpression: aws.String("#admin_type = :admin_type"),
ExpressionAttributeNames: map[string]*string{
"#admin_type": aws.String("admin_type"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":admin_type": {
S: aws.String(a.AdminType),
},
},
}
如何修改以上内容以获取 admin_type
与我的密钥匹配的所有项目? (基本上所有匹配 admin_type
的项都无需担心 user_id
的值。)
DynamoDB API 提供了两种获取数据的基本方法; query
和 scan
.
query
操作要求您指定主键。当你有复合主键(分区键+排序键)时,你可以:
- 仅指定分区键(您将取回分区中的所有内容)
- 同时指定分区键和排序键(您将返回一个项目)
query
操作没有仅指定排序键的选项。
要仅通过排序键 获取项目,您需要使用 scan
操作。或者,您可以创建一个二级索引,并将主键设置为您的排序键(在您的例子中,admin_type
)。这将允许您按 admin_type
获取项目,而不管 user_id
.
我有以下架构:
resources:
Resources:
ChatDynamoDbTable:
Type: "AWS::DynamoDB::Table"
DeletionPolicy: Retain
Properties:
TableName: "chats-admin-${opt:stage, self:provider.stage}"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: user_id
AttributeType: S
- AttributeName: admin_type
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
-
IndexName: user_id-admin_type-index
KeySchema:
-
AttributeName: user_id
KeyType: HASH
-
AttributeName: admin_type
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
我需要获取 admin_type
与我提供的密钥匹配的所有项目。
我尝试了以下但失败了,因为我没有指定 user_id
的值:
"error_message":"ValidationException: Query condition missed key schema element: user_id
input := &dynamodb.QueryInput{
TableName: aws.String(table),
IndexName: aws.String("user_id-admin_type-index"),
KeyConditionExpression: aws.String("#admin_type = :admin_type"),
ExpressionAttributeNames: map[string]*string{
"#admin_type": aws.String("admin_type"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":admin_type": {
S: aws.String(a.AdminType),
},
},
}
如何修改以上内容以获取 admin_type
与我的密钥匹配的所有项目? (基本上所有匹配 admin_type
的项都无需担心 user_id
的值。)
DynamoDB API 提供了两种获取数据的基本方法; query
和 scan
.
query
操作要求您指定主键。当你有复合主键(分区键+排序键)时,你可以:
- 仅指定分区键(您将取回分区中的所有内容)
- 同时指定分区键和排序键(您将返回一个项目)
query
操作没有仅指定排序键的选项。
要仅通过排序键 获取项目,您需要使用 scan
操作。或者,您可以创建一个二级索引,并将主键设置为您的排序键(在您的例子中,admin_type
)。这将允许您按 admin_type
获取项目,而不管 user_id
.