1
1

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 3 years have passed since last update.

DynamoDBへのテストデータ作成用pythonスクリプト

Last updated at Posted at 2021-01-27

はじめに

DynamoDBへ大量のテストデータを作りたいときのPythonスクリプトが欲しいなという時の参考にしていただければと思います。

実装

第一引数に、対象となるDynamoDBのテーブル名、第二引数にテストデータ数を整数で渡してあげると、動作します。

sample.py
import json
from argparse import ArgumentParser, FileType
 
import boto3
from tqdm import tqdm
 
def main():
    p = ArgumentParser(description='jsonをDynamoDBにインポート')
    p.add_argument('table', help='テーブル名')
    p.add_argument('total', type=int, help='テストデータ数')
    args = p.parse_args()
    session = boto3.Session(profile_name="default")
 
    dynamodb = session.resource('dynamodb')
    table = dynamodb.Table(args.table)
    total = args.total
 
    with table.batch_writer() as batch:
        for i in tqdm(range(total)):
            batch.put_item(
                Item={
                "ID": str(i),
                "TEST_DETAIL": "TESTDATA"
               }
            )
 
if __name__ == "__main__":
    main()
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?