AWS SDK DynamoDBを挿入、取得する方法

環境
AWS SDk
DynamoDB

操作方法
1.aws-sdk をインストールします
aws-sdk をインストールします。

2.AWS.DynamoDB Table作成 createTable
usersテーブル を作成します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const AWS = require('aws-sdk')
AWS.config.loadFromPath('./config.json')
const dynamoDB = new AWS.DynamoDB()
const params = {
TableName: 'users',
AttributeDefinitions: [
{ AttributeName: 'user_id', AttributeType: 'N' }, // number
{ AttributeName: 'created_at', AttributeType: 'S' }, // string
{ AttributeName: 'post_id', AttributeType: 'N' } // number
],
KeySchema: [
{ AttributeName: 'user_id', KeyType: 'HASH' }, // Partition key
{ AttributeName: 'created_at', KeyType: 'RANGE' } // Sort key
],
LocalSecondaryIndexes: [
{
IndexName: 'post_local_index',
Projection: {
ProjectionType: 'ALL' // 全て
},
KeySchema: [
{ AttributeName: 'user_id', KeyType: 'HASH' },
{ AttributeName: 'post_id', KeyType: 'RANGE' }
]
}
],
GlobalSecondaryIndexes: [
{
IndexName: 'post_global_index',
Projection: {
ProjectionType: 'ALL'
},
KeySchema: [
{ AttributeName: 'post_id', KeyType: 'HASH' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
}
dynamoDB.createTable(params, (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))
}
})
const AWS = require('aws-sdk') AWS.config.loadFromPath('./config.json') const dynamoDB = new AWS.DynamoDB() const params = { TableName: 'users', AttributeDefinitions: [ { AttributeName: 'user_id', AttributeType: 'N' }, // number { AttributeName: 'created_at', AttributeType: 'S' }, // string { AttributeName: 'post_id', AttributeType: 'N' } // number ], KeySchema: [ { AttributeName: 'user_id', KeyType: 'HASH' }, // Partition key { AttributeName: 'created_at', KeyType: 'RANGE' } // Sort key ], LocalSecondaryIndexes: [ { IndexName: 'post_local_index', Projection: { ProjectionType: 'ALL' // 全て }, KeySchema: [ { AttributeName: 'user_id', KeyType: 'HASH' }, { AttributeName: 'post_id', KeyType: 'RANGE' } ] } ], GlobalSecondaryIndexes: [ { IndexName: 'post_global_index', Projection: { ProjectionType: 'ALL' }, KeySchema: [ { AttributeName: 'post_id', KeyType: 'HASH' } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } } ], ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 10 } } dynamoDB.createTable(params, (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)) } })
const AWS = require('aws-sdk')
AWS.config.loadFromPath('./config.json')
const dynamoDB = new AWS.DynamoDB()

const params = {
  TableName: 'users',
  AttributeDefinitions: [
    { AttributeName: 'user_id', AttributeType: 'N' },     // number
    { AttributeName: 'created_at', AttributeType: 'S' },  // string
    { AttributeName: 'post_id', AttributeType: 'N' }      // number
  ],
  KeySchema: [
    { AttributeName: 'user_id', KeyType: 'HASH' },     // Partition key
    { AttributeName: 'created_at', KeyType: 'RANGE' }  // Sort key
  ],
  LocalSecondaryIndexes: [
    {
      IndexName: 'post_local_index',
      Projection: {
        ProjectionType: 'ALL' //  全て
      },
      KeySchema: [
        { AttributeName: 'user_id', KeyType: 'HASH' },
        { AttributeName: 'post_id', KeyType: 'RANGE' }
      ]
    }
  ],
  GlobalSecondaryIndexes: [
    {
      IndexName: 'post_global_index',
      Projection: {
        ProjectionType: 'ALL'
      },
      KeySchema: [
        { AttributeName: 'post_id', KeyType: 'HASH' }
      ],
      ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
      }
    }
  ],
  ProvisionedThroughput: {
    ReadCapacityUnits: 10,
    WriteCapacityUnits: 10
  }
}

dynamoDB.createTable(params, (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))
  }
})

3.Table削除 deleteTable
usersテーブル を削除します。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const AWS = require('aws-sdk')
AWS.config.loadFromPath('./config.json')
const dynamoDB = new AWS.DynamoDB()
const params = { TableName: 'users' }
dynamoDB.deleteTable(params, (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))
}
})
const AWS = require('aws-sdk') AWS.config.loadFromPath('./config.json') const dynamoDB = new AWS.DynamoDB() const params = { TableName: 'users' } dynamoDB.deleteTable(params, (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)) } })
const AWS = require('aws-sdk')
AWS.config.loadFromPath('./config.json')

const dynamoDB = new AWS.DynamoDB()
const params = { TableName: 'users' }
dynamoDB.deleteTable(params, (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))
  }
})

 

IT

Posted by arkgame