一、創建表
1 var AWS = require("aws-sdk"); 2 AWS.config.update({ 3 region: "us-west-2", //使用哪個區域的aws服務 4 endpoint: "http://localhost:8000" //dynamodb位置 5 }); 6 var dynamodb = new AWS.DynamoDB(); 7 8 var params = { 9 TableName: "Movies",//表名 10 KeySchema: [ //主鍵 11 {AttributeName: "year", KeyType: "HASH"}, //Partition 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 });
二、增加項
1 var docClient = new AWS.DynamoDB.DocumentClient(); 2 var table = "Movies"; 3 var year = 2015; 4 var title = "The Big New Movie"; 5 var params = { 6 TableName: table,//要操作的表名 7 Item: { 8 "year": year,//主鍵-分區間 9 "title": title,//主鍵-排序鍵 10 "info": { //其他屬性 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 });
三、獲取項
1 var docClient = new AWS.DynamoDB.DocumentClient() 2 var table = "Movies";//表名必須填 3 var year = 2015; //待查詢的分區鍵 4 var title = "The Big New Movie";//待查詢的排序鍵 如果表建立時設置為復合主鍵的話,分區鍵排序鍵必須都存在 5 var params = {TableName: table, Key: {"year": year, "title": title}}; 6 docClient.get(params, function (err, data) { 7 if (err) { 8 console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); 9 } else { 10 console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 11 } 12 });
四、更新項
1 var table = "Movies"; 2 var year = 2015; 3 var title = "The Big New Movie"; // Update the item, unconditionally, 4 var params = { 5 TableName: table, Key: { 6 "year": year,//分區鍵 7 "title": title//排序鍵 主鍵在update接口必須 8 }, UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a",//想要改變值的表達式 9 ExpressionAttributeValues: { //為想要改變的值賦值 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 });
五、刪除項
1 var table = "Movies"; 2 var year = 2015; 3 var title = "The Big New Movie"; 4 var params = { 5 TableName: table,//待刪除操作的表 6 Key: { 7 "year": year,//分區鍵 8 "title": title//排序鍵 9 }, 10 ExpressionAttributeNames:{"#u":"User"}, 11 ConditionExpression: "#u = :val",//刪除條件表達式 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 });
六、批量查詢
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 });
推薦操作數據庫時使用以上操作,操作DynamoDB數據庫有兩種方式,另一種見DynamoBD常見操作(二),這種方式比較繁瑣,不推薦使用
