LoginSignup
23
12

More than 3 years have passed since last update.

DynamoDB基礎知識とboto3での簡単な使用方法

Posted at

記事について

DynamoDBを使用するために学習した内容まとめ用

DynamoDB

NoSQLデータベースサービス

DB構造、用語

image.png

  • table
    RDSでのtable

  • item
    RDSでのrecord

  • attribute
    RDSでのcolumn
    item間でattributeは一致しなくてよい
    以下Partition Key、Sort Keyとなるattribute以外はtable生成時に設定不要

  • Primary Key
    DynamoDBでItemを一意に決定するためのキー
    主キーとも言うらしい

  • Partition Key
    table生成時に設定(必須)
    単体でPrimary Keyとして使用可能

  • Sort Key
    table生成時に設定
    Partition Key + Sort KeyでPrimary Keyとして使用可能
    Partition Keyの値が同じでSort Keyの値が異なる場合に、Partition Key + Sort KeyでItemを一意に決定

Primary Key

table生成時にPrimary Keyを以下2パターンから選択

  1. Partition Key
  2. Partition Key + Sort Key

1の場合、Partition Keyが同じデータは登録不可
2の場合、Partition Key、Sort Keyが共に同じデータは登録不可

Index

Key(Partition Key、Sort Key)として設定した以外のattributeで絞込検索(クエリ)を行うために、
Partition Key、Sort Keyを別のattributeで設定したもの
ローカルセカンダリインデックス(LSI)と、グローバルセカンダリインデックス(GSI)の2種類あり

  • ローカルセカンダリインデックス(LSI)

    以下のようにSort Keyのみ別のattributeに変更したもの
    6.png
    ※Primary KeyをPartition Key + Sort Keyとしている場合のみ作成可能
    ※LSIを設定する場合、table作成時に設定する必要あり、設定方法は後述

  • グローバルセカンダリインデックス(GSI)

    以下のようにPartition Key、Sort Keyを別のattributeに変更したもの
    7.png

LSI設定方法

以下のようにテーブル設定欄のデフォルト設定の使用のチェックを外して、インデックスの追加をクリック
3.png
LSIとして設定する場合は、パーティションキーは、tableのパーティションキーを指定
ソートキーに絞込検索に使用するattributeを設定、ローカルセカンダリインデックスとしての作成にチェック
4.png

GSI設定方法

以下のようにインデックスタブからインデックスの作成をクリック
8.png
image.png

データ取得パターン

  • getitem
    Primary Keyを使用した検索
    取得できるItemが一意に決定される場合のみ

  • query
    Primary Key = Partition Key + Sort Key設定時にPartition Keyでの絞込検索
    LSI、GSIでの絞込検索

  • scan(全レコード取得)

Key設定と、データ取得可/不可

Key設定 scan get item query
Primary Key (Partition Key) ×
Primary Key (Partition Key + Sort Key)
LSI - -
GSI - -

boto3を使用したデータ取得例

テーブル定義

【テーブル】

  • Primary Key
    Partition Key(SerialNumber) + Sort Key(BuildingId) image.png

【GSI】

  • Partition Key(Maker) image.png

データ取得コード

import boto3
import json
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb", region_name='ap-northeast-1')
table = dynamodb.Table("EdgeTable")

# Primary Key(SerialNumber + BuildingId)を使用した検索
def get_item(SerialNumber, BuildingId):
  response = table.get_item(
    Key={
      'SerialNumber': SerialNumber,
      'BuildingId': BuildingId
    }
  )
  return response['Item']

# Partition Key(SerialNumber)での絞込検索
def query_SerialNumber(SerialNumber):
  response = table.query(
    KeyConditionExpression=Key('SerialNumber').eq(SerialNumber)
  )
  return response['Items']

# Partition Key + Sort Key(SerialNumber + BuildingId)での絞込検索
def query_SerialNumber_BuildingId(SerialNumber, BuildingId):
  response = table.query(
    KeyConditionExpression=
      Key('SerialNumber').eq(SerialNumber) & Key('BuildingId').eq(BuildingId)
  )
  return response['Items']

# GSI(Maker-index)のPartition Key(Maker)での絞込検索
def query_Maker_index(Maker):
  response = table.query(
    IndexName='Maker-index',
    KeyConditionExpression=Key('Maker').eq(Maker)
  )
  return response['Items']


response = get_item('abdc-000002', 'zyxw-00987')
# response = {'BuildingId': 'zyxw-00987', 'DeviceType': '2', 'Maker': 'CCC', 'ProductionDate': '2019/10/2', 'SerialNumber': 'abdc-000002'}

response = query_SerialNumber('abcd-000001')
# response[0] = {'BuildingId': 'zyxw-12345', 'DeviceType': '1', 'LastMaintenanceDate': '2019/10/5', 'Maker': 'AAA', 'ProductionDate': '2015/10/1', 'SerialNumber': 'abcd-000001'}
# response[1] = {'BuildingId': 'zyxw-12399', 'DeviceType': '5', 'Maker': 'BBB', 'ProductionDate': '2018/1/5', 'SerialNumber': 'abcd-000001'}

response = query_SerialNumber_BuildingId('abcd-000001', 'zyxw-12399')
# response[0] = [{'BuildingId': 'zyxw-12399', 'DeviceType': '5', 'Maker': 'BBB', 'ProductionDate': '2018/1/5', 'SerialNumber': 'abcd-000001'}]

response = query_Maker_index('AAA')
# response[0] = {'BuildingId': 'zyxw-12378', 'DeviceType': '3', 'LastMaintenanceDate': '2017/5/7', 'Maker': 'AAA', 'ProductionDate': '2012/4/5', 'SerialNumber': 'abcd-000003'}
# response[1] = {'BuildingId': 'zyxw-12345', 'DeviceType': '1', 'LastMaintenanceDate': '2019/10/5', 'Maker': 'AAA', 'ProductionDate': '2015/10/1', 'SerialNumber': 'abcd-000001'}

データ追加

import boto3
import json
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb", region_name='ap-northeast-1')
table = dynamodb.Table("EdgeTable")

def put_item():
  table.put_item(
      Item={
          'SerialNumber': 'abcd-000001',
          'BuildingId': 'zyxw-12399',
          'DeviceType': '5'
    }
  )

参考

https://qiita.com/UpAllNight/items/a15367ca883ad4588c05
https://d1.awsstatic.com/webinars/jp/pdf/services/20181225_AWS-BlackBelt_DynamoDB.pdf

23
12
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
23
12