DynamoDB使用LocalSecondaryIndexes获得“关键架构太大”错误?

时间:2019-02-13 19:59:35

标签: javascript node.js indexing amazon-dynamodb

我正在尝试使用下面显示的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));
    }
});

2 个答案:

答案 0 :(得分:2)

每个表/索引必须具有1个哈希键和0或1个范围键。如果需要使用多个属性进行查询,则可以创建多个索引,或者,如果数据按原样是分层的,则可以将多个数据组合到排序键中。 (有关官方示例,请参见此AWS blog post。另请参见Best Practices for Using Sort Keys to Organize Data。)

如何创建表格?

您可以像这样创建所需的索引:

// 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_language", KeyType: "RANGE" },  //Sort key

            ],
            Projection: {
                ProjectionType: "ALL"
            }
        }
    ],
    AttributeDefinitions: [
        { AttributeName: "author_id", AttributeType: "S" },
        { AttributeName: "quiz_name_language", 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));
    }
});

那我的数据是什么样的?

您读/写的对象看起来像这样:

{
    author_id: "author1234",
    quiz_name: "DynamoDBExperienceSurvey",
    language_code: "en-us",
    quiz_name_language: "DynamoDBExperienceSurvey/en-us",
    quiz_id: "55dc0736-2fdf-11e9-b210-d663bd873d93",
    quiz_data: {
        ...
    }
}

如何执行查询?

这里是key condition expressions,用于获取您需要的数据。

要获取某个作者的所有调查问卷,您可以仅使用哈希键查询表或LSI。

author_id = "theAuthorId" 

要获得基于名称的测验的所有语言变体,您的关键条件是

author_id = "theAuthorId" AND begins_with(quiz_name_language, "theQuizName/")

在这种情况下,重要的是,在测验名称的末尾包括/(或您使用的任何定界符),否则“ theQuizName”也将返回“ theQuizName2”,“ theQuizName3”的结果等

奖金:您还可以使用语言代码的第一部分来查询特定语言的所有区域化变体。

author_id = "theAuthorId" AND begins_with(quiz_name_language, "theQuizName/en-")

答案 1 :(得分:1)

每个表,本地二级索引(LSI)或全局二级索引(GSI)只能有1个哈希键和1个排序键。

您需要将quiz_name,language_code和quiz_id连接到一个字符串中,或​​者创建多个LSI。

选择取决于查询LSI的方式。