Ans*_*rma 5 amazon-web-services amazon-dynamodb dynamo-local
我想将数据插入到dynamodb local中.但是,我只有一个键属性和多个非键属性.
{
'id':'99876983ydbhdu3739',
'category':'Spa',
'latitude':'33.498',
'longitude':'32.332',
'name':'Studio'
}
Run Code Online (Sandbox Code Playgroud)
我有多个这样的价值观.这是一条记录,是我想插入的一个例子.以下是我的尝试:
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
}
)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
调用CreateTable操作时发生错误(ValidationException):密钥模式中的属性数必须与属性定义中定义的属性数相匹配.
您只需在创建表时创建具有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));
}
});
Run Code Online (Sandbox Code Playgroud)
创建项目: -
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));
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3324 次 |
| 最近记录: |