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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33847477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:34:24  来源:igfitidea点击:

Querying a Global Secondary Index in dynamodb Local

node.jsamazon-dynamodbaws-sdk

提问by Stelios Savva

I am creating a table and GSI in DynamoDB, using these parameters, as per the documentation:

我正在 DynamoDB 中创建一个表和 GSI,使用这些参数,根据文档:

configIdis the primary key of the table, and I am using the publisherIdas the primary key for the GSI. (I've removed some unnecessary configuration parameters for brevity)

configId是表的主键,我将publisherId用作 GSI 的主键。(为简洁起见,我删除了一些不必要的配置参数)

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 KeyTypecan either be HASHor RANGE, and that you set the AttributeTypein the AttributeDefinitionsfield. I am sending the publisherIdas String, not sure what I am missing here.

在文档中,它指定主要KeyType可以是HASHRANGE,并且您AttributeTypeAttributeDefinitions字段中设置了。我正在发送publisherIdas String,不确定我在这里缺少什么。

Is the issue in the way I am creating the table, or the way I am querying? Thanks

问题是我创建表的方式还是我查询的方式?谢谢

回答by gior91

Try to change this

尝试改变这一点

{ 
 TableName: 'Configs',
 IndexName: 'publisher_index',
 KeyConditionExpression: 'publisherId = :pub_id',
 ExpressionAttributeValues: { ':pub_id': { S: '700' } } 
}

in this

在这

{ 
 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. The official documentation in misleading.

我遇到了同样的问题,为此我花了 2 天时间。官方文档中的误导。

回答by TheWebweiser

Turns out it depends on whether you use AWS.DynamoDBor AWS.DynamoDB.DocumentClient.

事实证明,这取决于您是使用AWS.DynamoDB还是AWS.DynamoDB.DocumentClient.

Check the difference in the documentation: AWS.DynamoDB.queryvs. AWS.DynamoDB.DocumentClient.query

检查文档中的差异: AWS.DynamoDB.queryvs. 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. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

...

Supply the same parameters as AWS.DynamoDB.query() with AttributeValues substituted by native JavaScript types.

文档客户端通过抽象出属性值的概念来简化对 Amazon DynamoDB 中项目的处理。此抽象对作为输入参数提供的原生 JavaScript 类型进行注释,并将带注释的响应数据转换为原生 JavaScript 类型。

...

提供与 AWS.DynamoDB.query() 相同的参数,其中 AttributeValues 替换为原生 JavaScript 类型。

Maybe you where also referring to the DynamoDB API Referencewhich 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.DocumentClientyou would just provide a simple key-value map for ExpressionAttributeValuesas suggested by @gior91.

因此,使用AWS.DynamoDB.DocumentClient您只需提供一个简单的键值映射,ExpressionAttributeValues就像@gior91 所建议的那样。

回答by AntonusMaximus

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,有一段时间无法弄清楚,但这会解决它。

回答by Hatim Khouzaimi

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' } }
}