- 最新
- 最多得票
- 最多評論
In DynamoDB you must always provide the Partition Key when reading items, unless using Scan
operation which will read every item in the table and is best to be avoided if possible.
For your use-case you would need to create a Global Secondary Index (GSI) and specify ami_name
as the partition key. Optionally you can use environment
as your sort key.
Now you would target the GSI and use a Query
operation to read the data (with partition key only):
out, err := svc.Query(context.TODO(), &dynamodb.QueryInput{
TableName: aws.String(tableName),
IndexName: aws.String(indexName),
KeyConditionExpression: aws.String("ami_name = :ami_name"),
FilterExpression: aws.String("environment = :environment"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":ami_name": &types.AttributeValueMemberS{Value: ami_name},
":environment": &types.AttributeValueMemberS{Value: environment},
},
ExpressionAttributeNames: map[string]string{
"ami_name": ami_name,
},
}
If you choose to use environment
as sort-key on your index then you would include that in your KeyConditionExpression
and remove the FilterExpression
.
Yes you can use the pagination method which uses 'Scan' operation, however this operation will read every item in the table and is best to be avoided if possible due to two reasons :
- Increase in cost.
- Time consuming.
Therefore you must always provide the Partition Key when reading items.