2016-07-01 4 views
2

ダイナモローカルにデータを挿入したい。しかし、私は1つのキー属性と複数の非キー属性しか持っていません。Dynamodb-非キーアトリビュートを追加する

{ 
    'id':'99876983ydbhdu3739', 
    'category':'Spa', 
    'latitude':'33.498', 
    'longitude':'32.332', 
    'name':'Studio' 
} 

このような値は複数あります。これは1つのレコードです。挿入するレコードの例です。私がしようとしていますものです、以下:

table = dynamodb.create_table(
    TableName='Trial', 
    KeySchema=[ 
     { 
      'AttributeName': 'facebook_id', 
      'KeyType': 'HASH' #Sort key 
     }, 
     { 
      'AttributeName': 'latitude', 
      'KeyType': 'RANGE' #Sort key 
     }, 



    ], 
    AttributeDefinitions=[ 
     { 
      'AttributeName':'id', 
      'AttributeType':'S' 
     }, 
     { 
      'AttributeName': 'category', 
      'AttributeType': 'S' 
     }, 
     { 
      'AttributeName': 'latitude', 
      'AttributeType': 'S' 
     }, 
     { 
      'AttributeName': 'longitude', 
      'AttributeType': 'S' 
     }, 
     { 
      'AttributeName': 'name', 
      'AttributeType':'S' 
     } 



    ], 
    ProvisionedThroughput={ 
     'ReadCapacityUnits': 10, 
     'WriteCapacityUnits': 10 
    } 
) 

私は次のエラーを取得する:

An error occurred (ValidationException) when calling the CreateTable operation: The number of attributes in key schema must match the number of attributesdefined in attribute definitions. 

任意のヘルプ?

答えて

6

テーブルの作成中にHASHおよびRANGEキー属性のみを使用してテーブルを作成することができます。 DynamoDBはキーと値のペアテーブルであるため、DynamoDBは他のすべての属性を定義するとは限りません。以下のコードを試してください。テーブルを作成できるはずです。

アイテムを挿入するときに、要件に応じて任意の属性を含めることができます。

表を作成します -

var AWS = require("aws-sdk"); 

AWS.config.update({ 
    region : "us-west-2", 
    endpoint : "http://localhost:8000" 
}); 

var dynamodb = new AWS.DynamoDB(); 

var params = { 
    TableName : "Trail", 
    KeySchema : [ { 
     AttributeName : "facebook_id", 
     KeyType : "HASH" 
    }, //Partition key 
    { 
     AttributeName : "latitude", 
     KeyType : "RANGE" 
    } //Sort key 
    ], 
    AttributeDefinitions : [ { 
     AttributeName : "facebook_id", 
     AttributeType : "N" 
    }, { 
     AttributeName : "latitude", 
     AttributeType : "S" 
    } ], 
    ProvisionedThroughput : { 
     ReadCapacityUnits : 10, 
     WriteCapacityUnits : 10 
    } 
}; 

dynamodb.createTable(params, function(err, data) { 
    if (err) { 
     if (err.code === "ResourceInUseException" 
       && err.message === "Cannot create preexisting table") { 
      console.log("message ====>" + err.message); 
     } else { 
      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)); 
    } 
}); 

は、アイテムの作成: -

var AWS = require("aws-sdk"); 

AWS.config.update({ 
    region : "us-west-2", 
    endpoint : "http://localhost:8000" 
}); 

var docClient = new AWS.DynamoDB.DocumentClient(); 

var table = "Trail"; 

var params = { 
    TableName : table, 
    Item : { 
     "facebook_id" : 1, 
     "latitude" : 'lat', 
     "longitude" : 'long', 
     "name" : 'facebook', 
     "category" : 'social_media' 
    } 
}; 

console.log("Adding a new item..."); 
docClient.put(params, function(err, data) { 
    if (err) { 
     console.error("Unable to add item. Error JSON:", JSON.stringify(err, 
       null, 2)); 
    } else { 
     console.log("Added item:", JSON.stringify(data, null, 2)); 
    } 
}); 
関連する問題