Skip to content

Instantly share code, notes, and snippets.

@ryanfb
Last active December 4, 2019 15:58
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 ryanfb/8e98e570967c4f9829398d03bd2ae91c to your computer and use it in GitHub Desktop.
Save ryanfb/8e98e570967c4f9829398d03bd2ae91c to your computer and use it in GitHub Desktop.
#!/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