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

Querying a Global Secondary Index in dynamodb Local

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

configId是table的主键,我用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',
                }
            ]
        }
    ]
};

我正在查询这个 table 使用这个:

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

但我一直收到错误消息:

"ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type"

在文档中指定主 KeyType 可以是 HASHRANGE,并且您在 AttributeDefinitions 中设置了 AttributeType场地。我将 publisherId 作为 String 发送,不确定我在这里遗漏了什么。

问题出在我创建 table 的方式上还是我查询的方式上? 谢谢

尝试从这个ExpressionAttributeValues改变

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

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

{ ':pub_id': { S: '700' } }{ ':pub_id': '700'}

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

尝试用以下内容替换您的查询 JSON:

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

原来这取决于你是使用AWS.DynamoDB还是AWS.DynamoDB.DocumentClient

检查文档中的差异: AWS.DynamoDB.query 对比 AWS.DynamoDB.DocumentClient.query

在 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.

也许您还提到了 DynamoDB API Reference,它实际上没有对使用的 SDK 做出任何假设,但在示例中使用了纯 HTTP 请求。

因此,使用 AWS.DynamoDB.DocumentClient 您只需按照 @gior91 的建议为 ExpressionAttributeValues 提供一个简单的键值映射。

如上所述,如果您想抽象掉类型转换,您需要使用 DynamoDB Document Client。

var docClient = new AWS.DynamoDB.DocumentClient();

...那么你可以使用上面列出的对象符号来调用API。

{ ':pub_id': '700'}

运行 我自己解决了这个问题,我在某些地方使用 DynamoDB() 而在其他地方使用 docClient,暂时无法弄清楚,但这会解决它。

我发现其他答案很有帮助,但我 仍然 无法让 ValidationException 消失......最终我意识到我会一直在使用 DocumentClient.get 而不是 .query

(从技术上讲,这不是问题的答案,但将其张贴在此处而不是在评论中,以便让其他正在努力解决如此无用的错误消息的人更容易看到它。)