DynamoDB的基本操作(一)

一、创建表 

 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常见操作(二),这种方式比较繁琐,不推荐使用
posted @ 2019-12-29 11:53  w_CJiEYa  阅读(1916)  评论(0编辑  收藏  举报