我正在使用无服务器框架开发AWS服务。到目前为止,它有一个表和几个全局二级索引。我试图查询全局二级索引。
查询:
{
"TableName": "messages-table-dev",
"IndexName": "roomIndex",
"KeyConditionExpression": "room = :room",
"ExpressionAttributeValues": {
":room": {
"S": "everyone"
}
}
}
它提供了一个例外 - 使用Node.js DynamoDB客户端,而不是Node.js DocumentClient
ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type
at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:48:27)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
message: 'One or more parameter values were invalid: Condition parameter type does not match schema type',
code: 'ValidationException',
time: 2018-06-05T05:11:08.899Z,
requestId: '72OROVKI35I3QDO2IJNQH6SIRVVV4KQNSO5AEMVJF66Q9ASUAAJG',
statusCode: 400,
retryable: false,
retryDelay: 35.265782751506215
我从文档中了解到KeyConditionExpression必须在分区索引上进行相等匹配。在这种情况下,room
字段是roomIndex
全局二级索引的分区索引。根据文档,您使用ExpressionAttributeValues数组将值替换为表达式,并且据我所知,这是正确构造的。
异常消息让我想到查询中提供的值与要查询的列的类型之间的类型不匹配。这是正确的解释吗?但情况并非如此 - 正如您在下面所见,room
列被定义为String的S
。因此,这是一个字符串比较,应该没问题。
再次 - 我没有使用DocumentClient。我已经看到很多答案建议通过使用该客户端来简化查询。我没有使用它。
该表在无服务器serverless.yml
中定义如下:
MessagesDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: messageId
AttributeType: S
- AttributeName: room
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: messageId
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: roomIndex
KeySchema:
- AttributeName: room
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
- IndexName: userIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
该表有一个简单的主键,然后是两个全局索引,每个索引都有简单的主键。
查询代码:
const params = {
TableName: MESSAGES_TABLE,
IndexName: "roomIndex",
KeyConditionExpression: "room = :room",
ExpressionAttributeValues: {
":room": { S: `${req.params.room}` }
},
};
console.log(`QUERY ROOM ${JSON.stringify(params)}`);
dynamoDb.query(params, (error, result) => {
if (error) {
console.log(error);
res.status(400).json({ error: 'Could not get messages' });
} else {
res.json(result.Items);
}
});
答案 0 :(得分:1)
我不得不承认有点尴尬。我在问题中多次说我的代码没有使用DocumentClient但是使用的是DynamoDB类。
使用DocumentClient,此查询确实有效:
{
"TableName": "messages-table-dev",
"IndexName": "roomIndex",
"KeyConditionExpression": "room = :room",
"ExpressionAttributeValues": {
":room": "everyone"
}
}
那是因为DocumentClient不需要这些数据类型,而是推断出数据类型。
事实证明我的代码实际上是在使用DocumentClient,而且只要我从查询中删除了数据类型,HTTP请求就会按预期工作。而且,当转到DynamoDB客户端并添加数据类型时,这也有效。
如果我更仔细地查看了应用程序代码,那么这个问题就永远不会发布了。
答案 1 :(得分:0)
{
"TableName": "messages-table-dev",
"IndexName": "roomIndex",
"KeyConditionExpression": "room = :room",
"ExpressionAttributeValues": {
":room": "everyone"
}
}
查询dynamoDb时无需输入数据类型