在 DynamoDB 上,您必须仅定义Hash Key
以及可选的Sort Key
为您的餐桌。其余属性不必定义!您可以推送任何您想要的数据。
查看下面的示例,基于官方文档 http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.03.html.
我正在创建一个表Movies
与哈希:Year
并排序:Title
。
然后我正在创建一部具有更多属性的电影:
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var client = new AWS.DynamoDB();
var documentClient = new AWS.DynamoDB.DocumentClient();
var tableName = "Movies";
var params = {
TableName: tableName,
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
client.createTable(params, function(tableErr, tableData) {
if (tableErr) {
console.error("Error JSON:", JSON.stringify(tableErr, null, 2));
} else {
console.log("Created table successfully!");
}
// Adding Batman movie to our collection
var params = {
TableName: tableName,
Item: {
"year": 2005,
"title": "Batman Begins",
"info": {
"plot": "A young Bruce Wayne (Christian Bale) travels to the Far East.",
"rating": 0
}
}
};
console.log("Adding a new item...");
documentClient.put(params, function(err, data) {
if (err) {
console.error("Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item successfully!");
}
});
});