4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.js で DynamoDB を使う(テーブル cities)

Last updated at Posted at 2017-10-07

前提ソフトのインストール

sudo npm install aws-sdk

cities という名前のテーブルを作成

dynamo_create_table.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_create_table.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
var AWS = require("aws-sdk")

AWS.config.update({
  region: "ap-northeast-1"
})

var dynamodb = new AWS.DynamoDB()

var params = {
    TableName : "cities",
    KeySchema: [       
        { AttributeName: "key", KeyType: "HASH"},
    ],
    AttributeDefinitions: [       
        { AttributeName: "key", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
}

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2))
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2))
    }
})

// ---------------------------------------------------------------

データの挿入

dynamo_insert.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_insert.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
function insert_proc (docClient,key,name,population,date_mod)
{
var params = {
        TableName: "cities",
        Item: {
            "key":  key,
            "name": name,
            "population":  population,
		"date_mod": date_mod
		}
}

docClient.put(params, function(err, data)
	{
	if (err) {
		console.error("Unable to add cities")
	} else {
		console.log("PutItem succeeded:")
		}
	})

}

// ---------------------------------------------------------------
var AWS = require("aws-sdk")

AWS.config.update({
  region: "ap-northeast-1"
})

var docClient = new AWS.DynamoDB.DocumentClient()

console.log("Importing into DynamoDB. Please wait.")

insert_proc (docClient,"t0921","宇都宮",37814,"2003-9-15")
insert_proc (docClient,"t0922","小山",28167,"2003-8-22")
insert_proc (docClient,"t0923","佐野",97125,"2003-7-9")
insert_proc (docClient,"t0924","足利",57819,"2003-6-11")

// ---------------------------------------------------------------

データのスキャン

dynamo_scan.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_scan.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
function scan_proc (docClient)
{
	var params = {
		TableName: "cities"
		}

	docClient.scan(params, function(err, data)
		{
		if (err) {
			console.error("Unable to get cities",JSON.stringify(err, null, 2))
		
			console.error("key = " + key)
			}
		 else {
			var str_out = ""
			data.Items.forEach(function(city) {
			str_out += city.key + "\t"
				 + city.name + "\t"
				 + city.population + "\t"
				 + city.date_mod + "\n"
				})

			console.log(str_out)
			}
		})

}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

var AWS = require("aws-sdk")

AWS.config.update({
  region: "ap-northeast-1"
})

var docClient = new AWS.DynamoDB.DocumentClient()

scan_proc (docClient)

console.error ("*** 終了 ***")

// ---------------------------------------------------------------

キーを指定してデータの取得

dynamo_get.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_get.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
function get_proc (docClient,key)
{
	var params = {
		TableName: "cities",
		Key: {"key":  key}
		}

	docClient.get(params, function(err, data)
		{
		if (err) {
			console.error("Unable to get cities",JSON.stringify(err, null, 2))
		
			console.error("key = " + key)
			}
		 else {
			const str_out = data.Item.key + "\t"
				 + data.Item.name + "\t"
				 + data.Item.population + "\t"
				 + data.Item.date_mod

			console.log(str_out)
			}
		})

}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

var AWS = require("aws-sdk")

AWS.config.update({
  region: "ap-northeast-1"
})

var docClient = new AWS.DynamoDB.DocumentClient()

get_proc (docClient,"t0923")

console.error ("*** 終了 ***")

// ---------------------------------------------------------------

キーを指定してデータの削除

dynamo_delete.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_delete.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
function delete_proc (docClient,key)
{
	var params = {
		TableName: "cities",
		Key: {"key":  key}
		}

	docClient.delete(params, function(err, data)
		{
		if (err) {
			console.error("Unable to delete cities",JSON.stringify(err, null, 2))
		
			console.error("key = " + key)
			}
		 else {
			console.log(data)
			}
		})

}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

var AWS = require("aws-sdk")

AWS.config.update({
  region: "ap-northeast-1"
})

var docClient = new AWS.DynamoDB.DocumentClient()

delete_proc (docClient,"t0922")

console.error ("*** 終了 ***")

// ---------------------------------------------------------------

テーブルの削除

dynamo_delete_table.js
# ! /usr/bin/node
// ---------------------------------------------------------------
//	dynamo_delete_table.js
//
//					Oct/7/2017
//
// ---------------------------------------------------------------
var AWS = require("aws-sdk");

AWS.config.update({
  region: "ap-northeast-1"
})

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "cities"
    }

dynamodb.deleteTable(params, function(err, data) {
    if (err) {
        console.error("Unable to delete table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Deleted table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

// ---------------------------------------------------------------

AWS のマニュアルにある例を改造した例
Node.js で DynamoDB を使う (テーブル Movies)

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?