我在DynamoDB中有一个带有复合键的表
user_id(分区密钥)
时间戳(排序键)
我想检索一组项目,我有一组要检索的user_id,但我还需要时间戳是每个user_id的最大值。
例如:
user_id
|
时间戳
|
attr
|
1.
|
1614910613
|
值1
|
1.
|
1614902527
|
值2
|
2.
|
1614910683
|
值2
|
2.
|
1614881311
|
值2
|
3.
|
1614902527
|
值2
|
我想检索user_id 1和2的行,以及每个用户的最大时间戳。在这种情况下,我的结果集应该是:
user_id
|
时间戳
|
attr
|
1.
|
1614910613
|
值1
|
2.
|
1614910683
|
值2
|
我现在可以逐一执行以下Key表达式:
cond := expression.KeyAnd(
expression.Key("user_id").Equal(expression.Value(userId)),
expression.Key("timestamp").LessThanEqual(expression.Value(int32(time.Now().Unix()))),
)
expr, err:= expression.NewBuilder().WithKeyCondition(cond).Build()
if err!=nil {
panic(err)
}
input := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
KeyConditionExpression: expr.KeyCondition(),
TableName: aws.String("td.notes"),
Limit: aws.Int64(1),
ScanIndexForward: aws.Bool(false),
}
我的问题是,我不知道如何为user_id键传递一组值,而不是单个值。我看了BatchGetItem,我知道我可以做这样的事情:
mapOfAttrKeys := []map[string]*dynamodb.AttributeValue{}
for _, place := range userIDs {
mapOfAttrKeys = append(mapOfAttrKeys, map[string]*dynamodb.AttributeValue{
"user_id": &dynamodb.AttributeValue{
S: aws.String(place),
},
"timestamp": &dynamodb.AttributeValue{
N: aws.String(timestamp),
},
})
}
input := &dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
tableName: &dynamodb.KeysAndAttributes{
Keys: mapOfAttrKeys,
},
},
}
但我不能在时间戳中加入“小于”的条件。
谢谢!