1
1

More than 1 year has passed since last update.

Node.js で DynamoDB のデータを読む

Last updated at Posted at 2020-03-02

参考ページ
Amazon DynamoDB Step 3.2: Read an Item
Node.js からDynamoDBを操作するサンプル

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

sudo npm install aws-sdk -g

Movies というテーブルから、 Scott という項目を読む

get_item.js
#! /usr/bin/node
//
//
var AWS = require("aws-sdk");

var dynamodb = new AWS.DynamoDB({region: 'ap-northeast-1'})

const table = "Movies"
const user = "Scott"

const params = {
    TableName: table,
    Key:{
        "user":  {"S": user},
    }
}

dynamodb.getItem(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2))
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2))
    }
})

テーブルの一覧

list_tables.js
#! /usr/bin/node
//
//
var AWS = require("aws-sdk")


var dynamodb = new AWS.DynamoDB({region: 'ap-northeast-1'})

var params = {
    Limit: 100
}

dynamodb.listTables(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2))
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2))
    }
})

テーブルの説明

describe_table.js
#! /usr/bin/node
//
//
var AWS = require("aws-sdk")


var dynamodb = new AWS.DynamoDB({region: 'ap-northeast-1'})

var table = "Movies"
var params = {TableName: table}

dynamodb.describeTable(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2))
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2))
    }
})
1
1
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
1
1