171

DynamoDB JavaScript シェルを使用して単純なテーブルを作成しようとしていますが、次の例外が発生します。

{
  "message": "The number of attributes in key schema must match the number of attributes defined in attribute definitions.",
  "code": "ValidationException",
  "time": "2015-06-16T10:24:23.319Z",
  "statusCode": 400,
  "retryable": false
}

以下は、私が作成しようとしているテーブルです。

var params = {
  TableName: 'table_name',
  KeySchema: [
    {
      AttributeName: 'hash_key_attribute_name',
      KeyType: 'HASH'
    }
  ],
  AttributeDefinitions: [
    {
      AttributeName: 'hash_key_attribute_name',
      AttributeType: 'S'
    },
    {
      AttributeName: 'attribute_name_1',
      AttributeType: 'S'
    }
  ],
  ProvisionedThroughput: {
    ReadCapacityUnits: 1,
    WriteCapacityUnits: 1
  }
};
dynamodb.createTable(params, function(err, data) {
  if (err) print(err);
  else print(data);
});

ただし、2 番目の属性を に追加すると、KeySchema正常に動作します。作業テーブルの下:

var params = {
  TableName: 'table_name',
  KeySchema: [
    {
      AttributeName: 'hash_key_attribute_name',
      KeyType: 'HASH'
    },
    {
      AttributeName: 'attribute_name_1',
      KeyType: 'RANGE'
    }
  ],
  AttributeDefinitions: [
    {
      AttributeName: 'hash_key_attribute_name',
      AttributeType: 'S'
    },
    {
      AttributeName: 'attribute_name_1',
      AttributeType: 'S'
    }
  ],
  ProvisionedThroughput: {
    ReadCapacityUnits: 1,
    WriteCapacityUnits: 1
  }
};
dynamodb.createTable(params, function(err, data) {
  if (err) print(err);
  else print(data);
});

範囲をキー スキーマに追加したくありません。それを修正する方法はありますか?

4

4 に答える 4

358

TL;DR にキー以外の属性定義を含めないでくださいAttributeDefinitions

DynamoDB はスキーマレス (主要なスキーマを除く)

つまり、テーブルを作成するときにキー スキーマ (属性名と型) を指定する必要があります。非キー属性を指定する必要はありません。後で任意の属性のアイテムを配置できます (もちろんキーを含める必要があります)。

ドキュメント ページから、AttributeDefinitionsは次のように定義されます。

テーブルとインデックスのキー スキーマを記述する属性の配列。

テーブルを作成すると、AttributeDefinitionsフィールドはハッシュおよび/または範囲キーのみに使用されます。最初のケースでは、2 つの AttributeDefinitions を提供している間、ハッシュ キーのみ (番号 1) があります。これが例外の根本原因です。

于 2015-06-18T19:44:03.830 に答える
31

at で非キー属性を"AttributeDefinitions"使用する場合は、それをインデックスとして使用する必要があります。そうしないと、DynamoDB の動作に反します。リンクを参照 してください。

"AttributeDefinitions"したがって、インデックスまたは主キーとして使用しない場合は、非キー属性を入れる必要はありません。

    var params = {
            TableName: 'table_name',
            KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
                { // Required HASH type attribute
                    AttributeName: 'UserId',
                    KeyType: 'HASH',
                },
                { // Optional RANGE key type for HASH + RANGE tables
                    AttributeName: 'RemindTime', 
                    KeyType: 'RANGE', 
                }
            ],
            AttributeDefinitions: [ // The names and types of all primary and index key attributes only
                {
                    AttributeName: 'UserId',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'RemindTime',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'AlarmId',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                // ... more attributes ...
            ],
            ProvisionedThroughput: { // required provisioned throughput for the table
                ReadCapacityUnits: 1, 
                WriteCapacityUnits: 1, 
            },
            LocalSecondaryIndexes: [ // optional (list of LocalSecondaryIndex)
                { 
                    IndexName: 'index_UserId_AlarmId',
                    KeySchema: [ 
                        { // Required HASH type attribute - must match the table's HASH key attribute name
                            AttributeName: 'UserId',
                            KeyType: 'HASH',
                        },
                        { // alternate RANGE key attribute for the secondary index
                            AttributeName: 'AlarmId', 
                            KeyType: 'RANGE', 
                        }
                    ],
                    Projection: { // required
                        ProjectionType: 'ALL', // (ALL | KEYS_ONLY | INCLUDE)
                    },
                },
                // ... more local secondary indexes ...
            ],
        };
        dynamodb.createTable(params, function(err, data) {
            if (err) ppJson(err); // an error occurred
            else ppJson(data); // successful response
        });```
于 2016-08-29T13:30:32.437 に答える
1

私もこの問題を抱えていたので、他の人に役立つ場合に備えて、何がうまくいかなかったのかをここに投稿します。

CreateTableRequestの には、 の空の配列がありましたGlobalSecondaryIndexes

 CreateTableRequest createTableRequest = new CreateTableRequest
 {
   TableName = TableName,
   ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 2, WriteCapacityUnits = 2 },
   KeySchema = new List<KeySchemaElement>
   {
      new KeySchemaElement
      {
         AttributeName = "Field1",
         KeyType = KeyType.HASH
      },
      new KeySchemaElement
      {
         AttributeName = "Field2",
         KeyType = KeyType.RANGE
      }
   },
   AttributeDefinitions = new List<AttributeDefinition>()
   {
      new AttributeDefinition
      {
          AttributeName = "Field1", 
          AttributeType = ScalarAttributeType.S
      },
      new AttributeDefinition
      {
         AttributeName = "Field2",
         AttributeType = ScalarAttributeType.S
      }
   },
   //GlobalSecondaryIndexes = new List<GlobalSecondaryIndex>
   //{                            
   //}
 }; 

テーブル作成でこれらの行をコメントアウトすると、問題が解決しました。nullしたがって、リストは空ではなく、 でなければならないと思います。

于 2016-05-26T13:25:53.580 に答える