-
-
Save ryanfb/8e98e570967c4f9829398d03bd2ae91c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
require 'csv' | |
require 'pp' | |
BATCH_LIMIT = 25 | |
def do_batch_write(dynamodb, request_items) | |
begin | |
resp = dynamodb.batch_write_item(request_items: request_items) | |
unless resp.unprocessed_items.empty? | |
PP.pp resp.unprocessed_items | |
end | |
rescue Exception => e | |
PP.pp request_items | |
raise | |
end | |
end | |
dynamodb = Aws::DynamoDB::Client.new | |
table_name = ARGV[0] | |
num_items = 0 | |
total_items = 0 | |
request_items = { table_name => [] } | |
CSV.parse(File.read(ARGV[1]), headers: true).each do |row| | |
request_items[table_name] << { | |
put_request: { | |
item: row.to_hash.reject{|k,v| v == ''} | |
} | |
} | |
num_items += 1 | |
total_items += 1 | |
if num_items == BATCH_LIMIT | |
do_batch_write(dynamodb, request_items) | |
$stderr.puts "Items written: #{total_items}" | |
num_items = 0 | |
request_items = { table_name => [] } | |
end | |
end | |
if num_items > 0 | |
do_batch_write(dynamodb, request_items) | |
$stderr.puts "Items written: #{total_items}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment