Skip to content

Instantly share code, notes, and snippets.

@yoshi0309
Created December 15, 2014 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshi0309/962595770c3ac9e85639 to your computer and use it in GitHub Desktop.
Save yoshi0309/962595770c3ac9e85639 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import boto
import sys
import csv
"""
Import CSV data to your DynamoDB.
usage : importCSV2DDB.py -f <csv filepath>
prerequirements:
- you need to setup boto before you run. see also http://boto.readthedocs.org/en/latest/boto_config_tut.html
- you need to create table before you run.
- the table name which you want to load data to must be same name with CSV file name (e.g. tablename 'rating' for 'rating.csv')
- the table Primary Key's attribute name must be 'id'. this script does not support 'Hash and Range' type.
- WARN!: This script is NOT consitered about your DynamoDB's provisiond throughput.
"""
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', dest='file', help='csv file path.')
options, args = parser.parse_args(sys.argv[1:])
if not options.file: # if filename is not given
parser.error('Filename not given')
filepath = options.file
tablename = filepath[filepath.rindex('/')+1:filepath.rindex('.')]
conn = boto.connect_dynamodb()
table = conn.get_table(tablename)
i = 0
header = []
for row in csv.reader(open(filepath)):
# 1行目はヘッダーとして扱う
if i == 0:
i = i+1
header = row
continue
item_data = {}
n = 0
for fname in header:
# 先頭のカラムは id なのでスキップ
if fname == 'id':
continue
n = n + 1
# DynamoDB の仕様上、空の値は入れられない。
val = row[n]
if val == None or val == '':
continue
item_data[fname] = row[n]
item = table.new_item(
hash_key=row[0],
attrs=item_data
)
item.put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment