最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用LocalSecondaryIndexes和DynamoDB获取“密钥模式太大”错误?

运维笔记admin10浏览0评论

使用LocalSecondaryIndexes和DynamoDB获取“密钥模式太大”错误?

使用LocalSecondaryIndexes和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));
    }
});
回答如下:

每个表/索引必须具有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-")
发布评论

评论列表(0)

  1. 暂无评论