dynamo 中的无服务器错误获取 ValidationException:提供的关键元素与模式不匹配
Serverless error in dynamo get ValidationException: The provided key element does not match the schema
我正在使用无服务器创建和查询 DynamoDB 数据库 table,但是当不使用主键进行查询时,我收到此错误:
INFO Error in dynamo get ValidationException: The provided key element does not match the schema
我将另一个密钥设置为 GSI,但我仍然收到错误消息。
serverless.yml
...
resources:
Resources:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: VenueID
AttributeType: S
KeySchema:
- AttributeName: VenueID
KeyType: HASH
BillingMode: PAY_PER_REQUEST
Dynamo.js
const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();
const Dynamo = {
async get(TableName){
const params = {
TableName,
Key:{
"BeaconAddr": <string> // Hardcoded string
}
};
const data = await documentClient
.get(params)
.promise()
if (!data || !data.Item){
throw Error(`There was an error fetching the data for ID of ${ID} from ${TableName}`)
}
console.log(data);
return data;
},
async write(data, TableName){
if (!data.ID){
throw Error('no ID on the data');
}
const params = {
TableName,
Item: data
};
const res = await documentClient.put(params).promise();
if (!res) {
throw Error(`There was an error inserting ID of ${data.ID} in table ${TableName}`)
}
return data;
}
};
module.exports = Dynamo;
当键为“VenueID”时查询有效,但当我将其更改为“BeaconAddr”(这是一个 GSI)时出现错误。我做错了什么?
下图是table:
DynamoDB 中的 get
操作仅适用于 table 的主键。您的 table 定义(如 serverless.yml
所示)仅包含分区键(又名 HASH),因此使用 get
操作的唯一方法是通过键请求单个项目(您已将其命名为 VenueID
)。如果您的意图是让排序键(又名 RANGE)成为 SK
属性,而分区键成为 PK
属性,那么您的 table 定义需要如下所示:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
请注意,定义中没有提及 VenueID
或 BeaconAddr
。您可以将上面显示中的值放入这些字段中。要添加 GSI,如您所显示的,您需要具备以下条件:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSI-1-PK
AttributeType: S
- AttributeName: GSI-1-SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: GSI-1
KeySchema:
- AttributeName: "GSI-1-PK"
KeyType: HASH
- AttributeName: GSI-1-SK
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
请注意,此处有单独的列。进入这些列的数据由您来填充。
最后,当查询 GSI 时(您不能 get
在 GSI 上),您必须在查询中提供索引名称。
我正在使用无服务器创建和查询 DynamoDB 数据库 table,但是当不使用主键进行查询时,我收到此错误:
INFO Error in dynamo get ValidationException: The provided key element does not match the schema
我将另一个密钥设置为 GSI,但我仍然收到错误消息。
serverless.yml
...
resources:
Resources:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: VenueID
AttributeType: S
KeySchema:
- AttributeName: VenueID
KeyType: HASH
BillingMode: PAY_PER_REQUEST
Dynamo.js
const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();
const Dynamo = {
async get(TableName){
const params = {
TableName,
Key:{
"BeaconAddr": <string> // Hardcoded string
}
};
const data = await documentClient
.get(params)
.promise()
if (!data || !data.Item){
throw Error(`There was an error fetching the data for ID of ${ID} from ${TableName}`)
}
console.log(data);
return data;
},
async write(data, TableName){
if (!data.ID){
throw Error('no ID on the data');
}
const params = {
TableName,
Item: data
};
const res = await documentClient.put(params).promise();
if (!res) {
throw Error(`There was an error inserting ID of ${data.ID} in table ${TableName}`)
}
return data;
}
};
module.exports = Dynamo;
当键为“VenueID”时查询有效,但当我将其更改为“BeaconAddr”(这是一个 GSI)时出现错误。我做错了什么?
下图是table:
DynamoDB 中的 get
操作仅适用于 table 的主键。您的 table 定义(如 serverless.yml
所示)仅包含分区键(又名 HASH),因此使用 get
操作的唯一方法是通过键请求单个项目(您已将其命名为 VenueID
)。如果您的意图是让排序键(又名 RANGE)成为 SK
属性,而分区键成为 PK
属性,那么您的 table 定义需要如下所示:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
请注意,定义中没有提及 VenueID
或 BeaconAddr
。您可以将上面显示中的值放入这些字段中。要添加 GSI,如您所显示的,您需要具备以下条件:
BeaconTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: venue-table
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSI-1-PK
AttributeType: S
- AttributeName: GSI-1-SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: GSI-1
KeySchema:
- AttributeName: "GSI-1-PK"
KeyType: HASH
- AttributeName: GSI-1-SK
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
请注意,此处有单独的列。进入这些列的数据由您来填充。
最后,当查询 GSI 时(您不能 get
在 GSI 上),您必须在查询中提供索引名称。