LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

DynamoDBのテスト用のデータをただ作るだけのPython

Last updated at Posted at 2020-08-26

概要

↓を作るPythonスクリプト。
パーティションキーとして、id: id00~01、
ソートキーとして、id毎に sid: sid000~sid099
1項目ごとに、data1,data2,data3を、dataX-{id}-{sid}

実装(テーブル定義)

    """
    テーブル定義
    パーティションキー(String):id
    ソートキー(String):sid
    属性1(String):data1
    属性2(String):data2
    属性3(String):data3
    """
    import boto3

    client = boto3.client('dynamodb')
    client.create_table(
        TableName='test-table',
        AttributeDefinitions=[
            {
                'AttributeName': 'id',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'sid',
                'AttributeType': 'S'
            },
        ],
        KeySchema=[
            {
                'AttributeName': 'id',
                'KeyType': 'HASH'
            },
            {
                'AttributeName': 'sid',
                'KeyType': 'RANGE'
            },
        ],
        BillingMode='PROVISIONED',
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5
        },
    )

実装(テスト用データ)

def register_items():
    """
    テーブル定義
    パーティションキー(String):id
    ソートキー(String):sid
    属性1(String):data1
    属性2(String):data2
    属性3(String):data3
    """
    import boto3

    client = boto3.client('dynamodb')

    for id in range(10):
        _id = "{:0=2}".format(id)

        for sid in range(100):
            _sid = "{:0=3}".format(sid)

            client.put_item(
                TableName='test-table',
                Item={
                    'id': {
                        'S': f'id{_id}',
                    },
                    'sid': {
                        'S': f'sid{_sid}',
                    },
                    'data1': {
                        'S': f'data1-{_id}-{_sid}',
                    },
                    'data2': {
                        'S': f'data2-{_id}-{_sid}',
                    },
                    'data3': {
                        'S': f'data3-{_id}-{_sid}',
                    },
                },
            )

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