Skip to content

Instantly share code, notes, and snippets.

@weavenet
Created June 27, 2013 20:44
Show Gist options
  • Save weavenet/5880251 to your computer and use it in GitHub Desktop.
Save weavenet/5880251 to your computer and use it in GitHub Desktop.
Dynamo Ruby Sample
#!/usr/bin/env ruby
require 'aws-sdk'
# create a table (10 read and 5 write capacity units)
dynamo_db = AWS::DynamoDB.new :region => 'us-west-2'
# get a table by name and specify its schema
table = dynamo_db.tables['dynamo-test']
table.hash_key = ['Customer ID', :number]
item = table.items.create('Customer ID' => 12345, 'foo' => 'bar')
# add attributes to an item
1.upto(10).each do |c|
start_time = Time.now
item.attributes.set 'updated_at' => Time.now.to_i
write_time = Time.now
item.attributes['updated_at'].to_i
end_time = Time.now
puts "Write: #{((end_time - start_time) * 1000).to_i} ms"
end
# read attributes to an item
1.upto(10).each do |c|
start_time = Time.now
item = table.items[12345]
item.attributes['updated_at']
end_time = Time.now
puts "Read: #{((end_time - start_time) * 1000).to_i} ms"
end
item.delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment