我用以下命令在dynamodb中创建了一个现有表
aws dynamodb create-table \
--region us-east-1 \
--table-name notifications \
--attribute-definitions AttributeName=CustomerId,AttributeType=S AttributeName=Timestamp,AttributeType=N AttributeName=MessageId,AttributeType=S \
--key-schema AttributeName=CustomerId,KeyType=HASH AttributeName=Timestamp,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--global-secondary-indexes '[
{
"IndexName": "MessageId",
"KeySchema": [
{
"AttributeName": "MessageId",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
]'
}
字符串
我想在它前面放置一个API Package 器,它允许我从CustomerId
提供的表中获取所有记录,因此我尝试使用v2 GO SDK中的查询
// GET /notifications/
func (api NotificationsApi) getNotifications(w http.ResponseWriter, r *http.Request) {
var err error
customerId := r.URL.Query().Get("customerId")
if customerId == "" {
api.errorResponse(w, "customerId query parameter required", http.StatusBadRequest)
return
}
span, ctx := tracer.StartSpanFromContext(r.Context(), "notification.get")
defer span.Finish(tracer.WithError(err))
keyCond := expression.Key("CustomerId").Equal(expression.Value(":val"))
expr, err := expression.NewBuilder().WithKeyCondition(keyCond).Build()
input := &dynamodb.QueryInput{
TableName: aws.String("notifications"),
KeyConditionExpression: expr.KeyCondition(),
ExpressionAttributeValues: map[string]dynamodbTypes.AttributeValue{
":val": &dynamodbTypes.AttributeValueMemberS{Value: customerId},
},
}
fmt.Println(*expr.KeyCondition())
output, err := api.dynamoClient.Query(ctx, input)
fmt.Println(output)
fmt.Println(err)
}
型
但是,我从dynamodb得到了400分。
operation error DynamoDB: Query, https response error StatusCode: 400, RequestID: *****, api error ValidationException: Invalid KeyConditionExpression: An expression attribute name used in the document path is not defined; attribute name: #0
型fmt.PrintLn(*expr.KeyCondition())
的输出是#0 = :0
在本地运行此查询将返回预期结果
awslocal dynamodb query \
--table-name notifications \
--key-condition-expression "CustomerId = :val" \
--expression-attribute-values '{":val":{"S":"localTesting"}}'
型
我也试过包含时间戳,但不认为这是必需的,因为我的终端命令没有它的作品。我不认为我解引用不当。我知道我的发电机会话是有效的,因为我可以张贴到我的 Package 器,并通过终端命令看到更新。
1条答案
按热度按时间p8h8hvxi1#
下面是一个可以用作模板的查询示例:
字符串
你可以在这里看到更多的GoV2示例