[英]Querying a Global Secondary Index in dynamodb Local
I am creating a table and GSI in DynamoDB, using these parameters, as per the documentation:我根据文档使用这些参数在 DynamoDB 中创建表和 GSI:
configId
is the primary key of the table, and I am using the publisherId
as the primary key for the GSI. configId
是表的主键,我使用publisherId
作为 GSI 的主键。 (I've removed some unnecessary configuration parameters for brevity) (为简洁起见,我删除了一些不必要的配置参数)
var params = {
TableName: 'Configs',
KeySchema: [
{
AttributeName: 'configId',
KeyType: 'HASH',
}
],
AttributeDefinitions: [
{
AttributeName: 'configId',
AttributeType: 'S',
},
{
AttributeName: 'publisherId',
AttributeType: 'S',
}
],
GlobalSecondaryIndexes: [
{
IndexName: 'publisher_index',
KeySchema: [
{
AttributeName: 'publisherId',
KeyType: 'HASH',
}
]
}
]
};
I am querying this table using this:我正在使用此查询此表:
{ TableName: 'Configs',
IndexName: 'publisher_index',
KeyConditionExpression: 'publisherId = :pub_id',
ExpressionAttributeValues: { ':pub_id': { S: '700' } } }
but I keep getting the error:但我不断收到错误:
"ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type"
“ValidationException:一个或多个参数值无效:条件参数类型与架构类型不匹配”
In the docs it specifies that the primary KeyType
can either be HASH
or RANGE
, and that you set the AttributeType
in the AttributeDefinitions
field.在文档中,它指定主要
KeyType
可以是HASH
或RANGE
,并且您在AttributeDefinitions
字段中设置了AttributeType
。 I am sending the publisherId
as String
, not sure what I am missing here.我将
publisherId
作为String
发送,不确定我在这里遗漏了什么。
Is the issue in the way I am creating the table, or the way I am querying?问题出在我创建表的方式上,还是我查询的方式上? Thanks
谢谢
Try to change ExpressionAttributeValues
from this尝试从此更改
ExpressionAttributeValues
{
TableName: 'Configs',
IndexName: 'publisher_index',
KeyConditionExpression: 'publisherId = :pub_id',
ExpressionAttributeValues: { ':pub_id': { S: '700' } }
}
to到
{
TableName: 'Configs',
IndexName: 'publisher_index',
KeyConditionExpression: 'publisherId = :pub_id',
ExpressionAttributeValues: { ':pub_id': '700'}
}
From { ':pub_id': { S: '700' } }
to { ':pub_id': '700'}
.从
{ ':pub_id': { S: '700' } }
到{ ':pub_id': '700'}
。
I had the same problem and I spent 2 days for this.我遇到了同样的问题,为此我花了 2 天时间。 The official documentation in misleading.
官方文档中的误导。
Turns out it depends on whether you use AWS.DynamoDB
or AWS.DynamoDB.DocumentClient
.事实证明,这取决于您是使用
AWS.DynamoDB
还是AWS.DynamoDB.DocumentClient
。
Check the difference in the documentation: AWS.DynamoDB.query vs. AWS.DynamoDB.DocumentClient.query检查文档中的差异: AWS.DynamoDB.query vs. AWS.DynamoDB.DocumentClient.query
In the DocumentClient the doc clearly states:在 DocumentClient 文档中明确指出:
The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values.
文档客户端通过抽象出属性值的概念来简化对 Amazon DynamoDB 中项目的处理。 This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.
此抽象对作为输入参数提供的原生 JavaScript 类型进行注释,并将带注释的响应数据转换为原生 JavaScript 类型。
...
...
Supply the same parameters as AWS.DynamoDB.query() with AttributeValues substituted by native JavaScript types.
提供与 AWS.DynamoDB.query() 相同的参数,其中 AttributeValues 替换为原生 JavaScript 类型。
Maybe you where also referring to the DynamoDB API Reference which in fact makes no assumptions about the used SDK but uses plain HTTP Requests in the examples.也许您还参考了DynamoDB API 参考,它实际上没有对使用的 SDK 做任何假设,而是在示例中使用纯 HTTP 请求。
Thus using the AWS.DynamoDB.DocumentClient
you would just provide a simple key-value map for ExpressionAttributeValues
as suggested by @gior91.因此,使用
AWS.DynamoDB.DocumentClient
您只需按照 @gior91 的建议为ExpressionAttributeValues
提供一个简单的键值映射。
As stated above, you need to use the DynamoDB Document Client if you want to abtract away the type casting.如上所述,如果您想消除类型转换,您需要使用 DynamoDB 文档客户端。
var docClient = new AWS.DynamoDB.DocumentClient();
...then you can just use the above listed object notation to call the API. ...然后您可以使用上面列出的对象表示法来调用 API。
{ ':pub_id': '700'}
Ran into this issue myself, I was using DynamoDB() in some places and the docClient in others, couldn't figure it out for awhile, but that'll solve it.我自己遇到了这个问题,我在某些地方使用了 DynamoDB(),在其他地方使用了 docClient,暂时无法弄清楚,但这会解决它。
I found the other answers very helpful, but I still couldn't get the ValidationException
s to go away... eventually I realized that I'd been using the DocumentClient
's .get
instead of .query
🤦我发现其他答案非常有帮助,但我仍然无法让
ValidationException
s消失......最终我意识到我一直在使用DocumentClient
的.get
而不是.query
🤦
(Technically not an answer to the question, but posting this here rather than in a comment to make it more visible to anyone else struggling with such a marvellously unhelpful error message.) (从技术上讲,这不是问题的答案,而是将其发布在此处而不是在评论中,以使其他人在遇到如此无益的错误消息时更容易看到它。)
Maybe I am late, but I'd like to share a method that will guarantee a correct query param.也许我来晚了,但我想分享一个保证正确查询参数的方法。
So,所以,
await documentClient.query(params).promise();
If you are still confused, see the screenshot below:如果你还一头雾水,请看下面的截图:
This is technically not an answer to the question asked, but I am posting this here to make it visible to anyone else dealing with the validation error.从技术上讲,这不是所问问题的答案,但我将其张贴在这里是为了让处理验证错误的任何其他人都能看到它。
I was getting the "query condition missed key schema element id" ValidationError.我收到“查询条件缺少关键架构元素 ID”ValidationError。
The problem was that my QueryCommand was missing "IndexName" field.问题是我的 QueryCommand 缺少“IndexName”字段。 After adding it, the command worked.
添加后,该命令起作用了。
What helped me figure it out was the documentation - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property帮助我弄明白的是文档 - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property
Try replacing your query JSON with this:尝试用这个替换你的查询 JSON:
{
TableName: 'Configs',
IndexName: 'publisher_index',
KeyConditionExpression: '#publisherId = :pub_id',
ExpressionAttributeNames: { '#publisherId': 'publisherId' },
ExpressionAttributeValues: { ':pub_id': { 'S': '700' } }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.