Created
February 6, 2022 01:22
-
-
Save jranson/464e9806b94361ad37b9511f75a8332b to your computer and use it in GitHub Desktop.
Load to DynamoDB from a CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import csv | |
import boto3 | |
dynamodb = boto3.resource('dynamodb') | |
def main(): | |
if len(sys.argv) != 3: | |
print("Usage: csv_to_dynamodb.py tableName filename") | |
tableName = sys.argv[1] | |
filename = sys.argv[2] | |
csvfile = open(filename) | |
batch_size = 100 | |
batch = [] | |
for row in csv.DictReader(csvfile): | |
deletes = [] | |
for key in row: | |
if row[key] == "null" or row[key] == "": | |
deletes.append(key) | |
for key in deletes: | |
print('delete', key) | |
del row[key] | |
if len(batch) >= batch_size: | |
write_to_dynamo(tableName, batch) | |
batch.clear() | |
batch.append(row) | |
if batch: | |
write_to_dynamo(tableName, batch) | |
return print("Done") | |
def write_to_dynamo(tableName, rows): | |
table = dynamodb.Table(tableName) | |
with table.batch_writer() as batch: | |
for row in rows: | |
batch.put_item(row) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment