2014年10月20日月曜日

Node.js から DynamoDB を操作

AWS-SDKをインストール
npm install aws-sdk --save
設定ファイルを作成 aws_config.json
{
     "accessKeyId":"xxxxxxxx",
     "secretAccessKey":"xxxxxxxx",
     "region":"ap-northeast-1"
}
DynamoDB接続
 var aws = require('aws-sdk');
 aws.config.loadFromPath('./aws_config.json');
 var dynamodb = new aws.DynamoDB({ endpoint: new aws.Endpoint('http://localhost:8000') });
createTable
   var params = {
      TableName: 'TEST_01', /* required */
      AttributeDefinitions: [ /* required */
         {
            AttributeName: 'ID', /* required */
            AttributeType: 'S' /* required */
         },
         {
            AttributeName: 'TIME_STAMP', /* required */
            AttributeType: 'S' /* required */
         },
         /* more items */
      ],
      KeySchema: [ /* required */
         {
            AttributeName: 'ID', /* required */
            KeyType: 'HASH' /* required */
         },
         {
            AttributeName: 'TIME_STAMP', /* required */
            KeyType: 'RANGE' /* required */
         },
         /* more items */
      ],
      ProvisionedThroughput: { /* required */
         ReadCapacityUnits: 1, /* required */
         WriteCapacityUnits: 1 /* required */
      },
   };
   dynamodb.createTable(params, function(err, data) {
    if (err){ console.log(err); }// an error occurred
    else { console.log(data);} // successful response
    });
putItem
   console.log("putItem");
   dynamodb.putItem({
    TableName:"TEST_01",
    Item:
    {
     ID: {S:"id1"},
     TIME_STAMP: {S:"2013-10-01 00:00:00"},
     TEXT:   {S:"text"},
     NAME:   {S:"name"}
    }
   },
   function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
getItem
   dynamodb.getItem(
   {
    TableName:"TEST_01",
    Key: {
     ID: {S:"id1"},
     TIME_STAMP: {S:"2013-10-01 00:00:00"},
    }
   },function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
query
   dynamodb.query(
   {
    TableName:"TEST_01",
    KeyConditions : 
    {
     "ID" : 
     {
      AttributeValueList:[
       {S:"id1"}
      ],
      ComparisonOperator:'EQ',
     },
    }
   },function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
   dynamodb.query(
   {
    TableName:"TEST_01",
    KeyConditions : 
    {
     "ID" : 
     {
      AttributeValueList:[
       {S:"id1"}
      ],
      ComparisonOperator:'EQ',
     },
     "TIME_STAMP" : 
     {
      AttributeValueList:[
       {S:"2013-10-01 00:00:00"}
      ],
      ComparisonOperator:'LE',
     }
    }
   },function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
batchGetItem
   var params = {
    "RequestItems":
        {"TEST_01": 
            {"Keys": 
                [
                 {"ID": {"S":"id1"}, "TIME_STAMP":{"S":"2013-10-01 00:00:00"}},
                 {"ID": {"S":"id2"}, "TIME_STAMP":{"S":"2013-10-02 00:00:00"}},
                 {"ID": {"S":"id3"}, "TIME_STAMP":{"S":"2013-10-01 00:00:00"}}
                ],
             "AttributesToGet":["ID", "TIME_STAMP", "NAME"]
         }
        }
     };
   dynamodb.batchGetItem(params, function(err, data) {
    if (err){ console.log(err); }
    else { console.log(data.Responses.TEST_01);}
    callback(err, 6);
   });
deleteItem
   var params = {
      TableName: 'TEST_01', /* required */
    Key: {
     ID: {S:"id1"},
     TIME_STAMP: {S:"2013-10-01 00:00:00"},
    }
   };
   dynamodb.deleteItem(params, function(err, data) {
    if (err){ console.log(err); }
    else { console.log(data);}
   });
describeTable
   dynamodb.describeTable(
   {
    TableName:"TEST_01",
   },function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
deleteTable
   dynamodb.deleteTable(
   {
    TableName:"TEST_01",
   },function(err,data){
    if (err){ console.log(err); }
    else { console.log(data);}
   });
listTables
   dynamodb.listTables(function (err, data)
   {
    if (err){ console.log(err); }
    else { console.log(data);}
    callback(err, 8);
   });
比較演算子 ComparisonOperator
EQ =
LE <=
LT <
GE >=
GT >
BEGINS_WITH 前方一致
BETWEEN 範囲

0 件のコメント:

コメントを投稿