我正在使用AWS DynamoDB DocumentClient来查询我的DynamoDB表。在我的表中,我有一个名为“_id”的属性,它为每个条目保存一个唯一的ID。当我尝试根据此ID查询记录时,我最终收到一条错误消息:“无效的KeyConditionExpression:语法错误;令牌:\”_ \“,附近:\”_ id \“”。进行此查询的代码如下所示。
@Override public void configure() {
// BAD: To change the scope, you'll need to change three lines.
// If you don't change all three together, you'll get inconsistent behavior.
bind(A.class).to(AImpl.class).in(MyScope.INSTANCE);
bind(B.class).to(BImpl.class).in(MyScope.INSTANCE);
bindScope(AScoped.class, MyScope.INSTANCE);
}
@Override public void configure() {
// GOOD: To change the scope, you can change one line, and optionally
// extract that line to a separate Module.
bind(A.class).to(AImpl.class).in(AScoped.class);
bind(B.class).to(BImpl.class).in(AScoped.class);
bindScope(AScoped.class, MyScope.INSTANCE);
}
在构建DynamoDB的查询参数时,是否存在关于使用“_”的问题?我一直在寻找类似的错误,但一直找不到任何类似我的情景。
答案 0 :(得分:4)
您可以尝试使用ExpressionAttributeNames参数吗?
var params = {};
params.TableName = "recordDev";
params.IndexName = "_id";
params.KeyConditionExpression = '#id = :id';
params.ExpressionAttributeValues = {
':id': id
};
params.ExpressionAttributeNames: {
"#id":"_id"
};
答案 1 :(得分:2)
当你有" - "同样的问题也会发生。在财产名称。例如,我有" id-test"作为财产名称。我得到了同样的错误:
调用Query时发生错误(ValidationException) 操作:无效的KeyConditionExpression:语法错误;令牌:" - ", 附近:" teamid-testid"
@gkatzioura建议的解决方案也适用于这种情况。这是我使用Python boto3约定的实现:
params = {}
params['TableName'] = "myTable"
params['ProjectionExpression']="idStatus"
params['KeyConditionExpression'] = 'scanid = :val1 and #idtest = :val2'
params['ExpressionAttributeValues'] = {":val1":{"S": scanid}, ":val2": {"S": id_test}}
params['ExpressionAttributeNames'] = {"#idtestid": "id-test"}