【发布时间】:2025-12-20 02:00:06
【问题描述】:
我正在尝试使用如下所示的 Node.js 脚本创建一个 DynamoDB 表。如果我删除 LocalSecondaryIndexes 块并删除删除后不再需要的两个属性定义,则代码可以正常工作并成功创建表。但是使用下面代码中显示的那个块,我从 DynamoDB 得到以下错误:
Unable to create table. Error JSON: {
"message": "Key Schema too big. Key Schema must at most consist of the hash and range key of a table",
"code": "ValidationException",
"time": "2019-02-13T19:45:34.482Z",
"statusCode": 400,
"retryable": false,
"retryDelay": 29.475438988642534
}
我该如何解决这个问题?
代码如下:
// Create the quizzes table in DynamoDB.
var AWS = require('aws-sdk');
AWS.config.update({
region: process.env.AWS_REGION,
endpoint: process.env.AWS_ENDPOINT
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName : "Quizzes",
KeySchema: [
{ AttributeName: "author_id", KeyType: "HASH"}, //Partition key
{ AttributeName: "quiz_id", KeyType: "RANGE" } //Sort key
],
// Secondary key allows us to get all the different versions of a
// a particular quiz, referenced by quiz name, for all the available
// languages the quiz supports.
LocalSecondaryIndexes: [
{
IndexName: "ForeignLanguageSupportIndex",
KeySchema: [
{ AttributeName: "author_id", KeyType: "HASH"}, //Partition key
{ AttributeName: "quiz_name", KeyType: "RANGE" }, //Sort key
{ AttributeName: "language_code", KeyType: "RANGE" }, //Sort key
{ AttributeName: "quiz_id", KeyType: "RANGE" } //Sort key
],
Projection: {
ProjectionType: "ALL"
}
}
],
AttributeDefinitions: [
{ AttributeName: "author_id", AttributeType: "S" },
{ AttributeName: "quiz_name", AttributeType: "S" },
{ AttributeName: "language_code", AttributeType: "S" },
{ AttributeName: "quiz_id", AttributeType: "S" }
],
// Using on-demand provisioning (pay as you go, no pre-allocation).
BillingMode: "PAY_PER_REQUEST"
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
【问题讨论】:
标签: javascript node.js indexing amazon-dynamodb