Created
September 28, 2013 16:56
-
-
Save mryoshio/6744061 to your computer and use it in GitHub Desktop.
sample ruby script to batch write onto DynamoDB
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
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