首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用LocalSecondaryIndexes获得“键模式太大”的错误DynamoDB?

用LocalSecondaryIndexes获得“键模式太大”的错误DynamoDB?
EN

Stack Overflow用户
提问于 2019-02-13 19:59:35
回答 2查看 4.6K关注 0票数 0

我正在尝试使用下面所示的DynamoDB脚本创建一个Node.js表。如果删除LocalSecondaryIndexes块并删除删除后不再需要的两个属性定义,代码就可以正常工作,并成功地创建表。但是,使用这个块(如下面的代码所示),我从DynamoDB获得以下错误:

代码语言:javascript
复制
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
}

我怎样才能解决这个问题?

下面是代码:

代码语言:javascript
复制
// 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));
    }
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-13 22:51:26

每个表/索引必须有一个散列键和0或1个范围键。如果需要使用多个属性进行查询,则可以创建多个索引,或者,如果数据像您的数据一样是分层的,则可以将多个数据块组合到排序键中。(官方示例请参见此AWS博客文章。也请参阅使用排序键组织数据的最佳实践。)

如何创建表?

您可以创建如下所需的索引:

代码语言:javascript
复制
// 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));
    }
});

那么我的数据是什么样子的?

您所读/写的对象如下所示:

代码语言:javascript
复制
{
    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。

代码语言:javascript
复制
author_id = "theAuthorId" 

要根据名称获取测试的所有语言变体,您的关键条件是

代码语言:javascript
复制
author_id = "theAuthorId" AND begins_with(quiz_name_language, "theQuizName/")

在本例中,重要的是在测试名称的末尾包含/ (或您使用的任何分隔符),否则"theQuizName“也将返回"theQuizName2”、"theQuizName3“等的结果。

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

代码语言:javascript
复制
author_id = "theAuthorId" AND begins_with(quiz_name_language, "theQuizName/en-")
票数 2
EN

Stack Overflow用户

发布于 2019-02-13 20:17:56

只能有一个散列键和一个排序key...per表、本地辅助索引(LSI)或全局辅助索引(GSI)。

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

选择取决于您需要如何查询LSI。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54678479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档