当前位置: 代码迷 >> JavaScript >> DynamoDB使用LocalSecondaryIndexes出现“关键架构太大”错误?
  详细解决方案

DynamoDB使用LocalSecondaryIndexes出现“关键架构太大”错误?

热度:93   发布时间:2023-06-13 11:48:51.0

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

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

如何创建表格?

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

// 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: {
        ...
    }
}

如何执行查询?

以下是获取所需数据的 。

要获取某个作者的所有调查,您可以仅使用哈希键查询表或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-")

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

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

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