简体   繁体   English

在 dynamodb Local 中查询全局二级索引

[英]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可以是HASHRANGE ,并且您在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,所以,

  1. go to your aws console, select dynamodb service, go 到您的 aws 控制台,select dynamodb 服务,
  2. in the left, it's the dynamodb menu and click "Explore items".在左侧,它是 dynamodb 菜单,然后单击“Explore items”。
  3. And then, choose the table you are interested in.然后,选择您感兴趣的表。
  4. after selecting the table, in the right, the top will show "Scan/Query items".选择表格后,在右侧,顶部将显示“扫描/查询项目”。 Please expand it请展开
  5. choose "Query" tab and in the dropdown, select your index选择“查询”选项卡,然后在下拉列表中输入 select 您的索引
  6. then, input a value, and click the button "Run"然后,输入一个值,然后单击“运行”按钮
  7. ATTENTION: In the.network of chrome, the "Fetch/XHR" tab will show a http request.注意:在 chrome 的 .network 中,“Fetch/XHR”选项卡将显示 http 请求。 Please see the payload.请查看有效载荷。 It's the query you need.这是您需要的查询。 Of course, you can try to remove some fields you don't need, such as "Limit"当然,你可以尝试去掉一些你不需要的字段,比如“Limit”
  8. How to use the query param?如何使用查询参数? 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.

相关问题 查询带有全局二级索引错误的 DynamoDb - Querying DynamoDb with Global Secondary Index Error DynamoDB 使用不存在的 ExclusiveStartKey 查询全局二级索引返回意外结果 - DynamoDB querying Global Secondary Index with non-existant ExclusiveStartKey returns unexpected results 全局二级索引 DynamoDB 空值错误 - Global Secondary Index DynamoDB empty value error DynamoDB 全球二级索引“批量”检索 - DynamoDB Global Secondary Index "Batch" Retrieval 从 AWS 控制台按全局二级索引查询 - Query by global secondary index from AWS Console DynamoDB:从 cli 创建具有全局索引的表 - 参数验证失败 - DynamoDB: create table with global index from cli - parameter validation failed 按日期范围查询 DynamoDB 二级索引返回“不支持查询键条件” - Query DynamoDB secondary index by date range returns "Query key condition not supported" 在我的 DynamoDB 表上创建未使用的通用本地二级索引以备日后需要它们是否明智? - Is it sensible to create unused generic local secondary indexes on my DynamoDB tables in case I need them later? 使用 Python 查询 DynamoDB - Querying a DynamoDB with Python 本地二级索引的 PynamoDB 错误:ValueError('表 table_4 没有索引:key1_index',) - PynamoDB Error for Local Secondary Index: ValueError('Table table_4 has no index: key1_index',)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM