Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created September 28, 2013 16:56
Show Gist options
  • Save mryoshio/6744061 to your computer and use it in GitHub Desktop.
Save mryoshio/6744061 to your computer and use it in GitHub Desktop.
sample ruby script to batch write onto DynamoDB
require 'aws'
require 'aws-sdk'
ACCESS_KEY_ID='access key'
SECRET_ACCESS_KEY='secret access key'
TABLE='table name'
MAX_ITEMS=100
AWS.config({
access_key_id: ACCESS_KEY_ID,
secret_access_key: SECRET_ACCESS_KEY,
dynamo_db_endpoint: 'dynamodb.ap-northeast-1.amazonaws.com'
})
batch = AWS::DynamoDB::BatchWrite.new
items = []
MAX_ITEMS.times do |item_id|
items.push({
item_id: item_id.to_s,
selling_date: (Time.now - 60* 60 * 24 * Random.rand(100)).strftime('%Y/%m/%d'),
total_amount: Random.rand(10000)
})
if items.size == 25
batch.put(TABLE, items.dup)
batch.process!
items.clear
end
end
batch.process!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment