First, create a table
. 1 var the AWS = the require ( "aws-SDK" ); 2 AWS.config.update ({ . 3 Region: "US-2-West", // used which area aws service 4 endpoint: "http: // localhost : 8000 " // dynamodb position . 5 }); . 6 var dynamodb = new new AWS.DynamoDB (); . 7 . 8 var the params = { . 9 the TableName:" Movies ", // table 10 KeySchema: [ // primary key 11 {AttributeName:" year ", KeyType:" HASH "}, // partition key partitioning key 12 {AttributeName: "title", KeyType: "RANGE"} //Sort key 排序键 13 ], 14 AttributeDefinitions: [//主键数据类型 15 {AttributeName: "year", AttributeType: "N"},//N Number 16 {AttributeName: "title", AttributeType: "S"} //S String 17 ], 18 ProvisionedThroughput: { //DynamoDB吞吐量配置 19 ReadCapacityUnits: 10, 20 WriteCapacityUnits: 10 21 } 22 }; 23 dynamodb.createTable(params, function (err, data) { 24 if (err) { 25 console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); 26 } else { 27 console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); 28 } 29 });
Second, the increase in entries
. 1 var docClient = new new AWS.DynamoDB.DocumentClient (); 2 var Table = "Movies" ; . 3 var year =, 2015 ; . 4 var title = "of The New Big Movie" ; . 5 var the params = { . 6 the TableName: Table, // table to operate . 7 Item: { . 8 "year": year, // primary key - inter-partition . 9 "title": title, // primary key - sort key 10 "info": { // other properties 11 "plot": "Nothing happens at all.", "rating": 0 12 } 13 } 14 }; 15 console.log("Adding a new item..."); 16 docClient.put(params, function (err, data) { 17 if (err) { 18 console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2)); 19 } else { 20 console.log("Added item:", JSON.stringify(data, null, 2)); 21 } 22 });
Third, access entries
. 1 var docClient = new new AWS.DynamoDB.DocumentClient () 2 var Table = "Movies"; // table must fill . 3 var year =, 2015; // partition key to be queried . 4 var title = "of The New Big Movie"; // sort key to be queried is provided if the table creating a composite primary key, then the partition key sort key must exist . 5 var the params = {the TableName: table, key: { "year": year, "title" : title}}; . 6 docClient.get (the params, function (ERR, Data) { . 7 IF (ERR) { . 8 console.error ( "Unable to Read Error the JSON Item:.", the JSON.stringify (ERR, null ,2)); 9 } else { 10 console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 11 } 12 });
Fourth, the updated item
. 1 var Table = "Movies" ; 2 var year =, 2015 ; . 3 var title = "of The New Big Movie"; // the Update The Item, UNCONDITIONALLY, . 4 var the params = { . 5 the TableName: Table, Key: { . 6 "year" : year, // partition key . 7 "title": title // sort key interface must update the primary key . 8 }, UpdateExpression: "SET = info.rating: R & lt, = info.plot: P, = info.actors: A" , // want to change the value of the expression 9 ExpressionAttributeValues: { // value you want to change the assignment 10 ":r": 5.5, ":p": "Everything happens all at once.", ":a": ["Larry", "Moe", "Curly"] 11 }, ReturnValues: "UPDATED_NEW"//返回更新值,即下中data 12 }; 13 console.log("Updating the item..."); 14 docClient.update(params, function (err, data) { 15 console.log(data); 16 if (err) { 17 console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2)); 18 } else { 19 console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2)); 20 } 21 });
Fifth, delete items
. 1 var Table = "Movies" ; 2 var year =, 2015 ; . 3 var title = "of The New Big Movie" ; . 4 var the params = { . 5 the TableName: Table, // wait table delete operation . 6 Key: { . 7 "year" : year, // partition key . 8 "title": title // sort key . 9 }, 10 ExpressionAttributeNames: { "U #": "the User" }, . 11 conditionExpression: "= #U: Val", // deletion condition expression formula 12 ExpressionAttributeValues: { 13 ":val": 5.0 //条件值 14 } 15 }; 16 console.log("Attempting a conditional delete..."); 17 docClient.delete(params, function (err, data) { 18 if (err) { 19 console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2)); 20 } else { 21 console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2)); 22 } 23 });
Sixth, batch query
1 var params = { 2 RequestItems: { 3 "GsdSubDevices": { 4 Keys: [ 5 { 6 "Mac": "haha" 7 }, 8 { 9 "Mac": "111" 10 }, 11 { 12 "Mac": "222" 13 } 14 ] 15 } 16 } 17 }; 18 console.log("requestArr===>" + JSON.stringify(requestArr)); 19 docClient.batchGet(params, function (err, data) { 20 if (err) { 21 console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); 22 } else { 23 24 } 25 });
When using more than the recommended operation database operation, the operation DynamoDB database in two ways, see DynamoBD Another common operation (b), this approach is more complicated, not recommended