LoginSignup
0
1

More than 5 years have passed since last update.

DynamoDBにboto3を使ってデータインポートする

Posted at

DynamoDBにデータをインポートするにはS3にインポート用のファイルを置いて、pipelineを使う、という方法があるけど、boto3を使ってやってみた

インポート用のファイルはこんな感じ
pipeline使う時とはファイルのフォーマットが異なるので要注意

import_dynamo.txt
{"foo":2541657,"bar":1523767752,"hoge":90192}
{"foo":2720851,"bar":1523768049,"hoge":148858}
{"foo":1260648,"bar":1523768077,"hoge":342488}
{"foo":2149901,"bar":1523767818,"hoge":277628}
{"foo":453283,"bar":1523768020,"hoge":365116}
{"foo":516232,"bar":1523767766,"hoge":109706}
{"foo":596116,"bar":1523767872,"hoge":47268}

Pythonのコードはざっとこんな感じ
自分がやった場合はインポートのファイルがtxtで改行コードが入ってたので、line.strip()を使って改行コードを省いています
print(datetime.datetime.now()) は実行時間を計測したかったので、入れてるけど、なくてもおk

put_item.py
import boto3
import json
import datetime

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('hogehoge_table')

with open("import_dynamo.txt", 'r') as f:
    line = f.readline()
    print(datetime.datetime.now())
    while line:
        table.put_item(Item=json.loads(line.strip()))
        line = f.readline()

    print(datetime.datetime.now())

260万件の処理でだいたい12時間程度かかったので、速度的にはpipeline使った方が速いと思う

0
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
0
1