Skip to content

Instantly share code, notes, and snippets.

@ericlathrop
Created March 24, 2014 17:06
Show Gist options
  • Save ericlathrop/9744568 to your computer and use it in GitHub Desktop.
Save ericlathrop/9744568 to your computer and use it in GitHub Desktop.
DynamoDB Tutorial for Ruby
# Port of the DynamoDB Tutorial to Ruby
# http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LoadData_PHP.html
require 'aws-sdk'
def upload_sample_products(dynamo_db)
products = dynamo_db.tables['ProductCatalog']
products.hash_key = { :Id => :string }
product = {
'Id' => 101,
'Title' => 'Book 101 Title',
'ISBN' => '111-1111111111',
'Authors' => [ 'Author 1' ],
'Price' => -2,
'Dimensions' => '8.5 x 11.0 x 0.5',
'PageCount' => 500,
'InPublication' => true.to_s,
'ProductCategory' => 'Book'
}
products.items.create(product)
product = {
'Id' => 102,
'Title' => 'Book 102 Title',
'ISBN' => '222-2222222222',
'Authors' => [ 'Author 1', 'Author 2' ],
'Price' => 20,
'Dimensions' => '8.5 x 11.0 x 0.8',
'PageCount' => 600,
'InPublication' => true.to_s,
'ProductCategory' => 'Book'
}
products.items.create(product)
product = {
'Id' => 103,
'Title' => 'Book 103 Title',
'ISBN' => '333-3333333333',
'Authors' => [ 'Author 1', 'Author 2', 'Author 3' ],
'Price' => 2000,
'Dimensions' => '8.5 x 11.0 x 1.5',
'PageCount' => 700,
'InPublication' => false.to_s,
'ProductCategory' => 'Book'
}
products.items.create(product)
product = {
'Id' => 201,
'Title' => '18-Bike 201',
'Description' => '201 description',
'BicycleType' => 'Road',
'Brand' => 'Brand-Company A',
'Price' => 100,
'Gender' => 'M',
'Color' => [ 'Red', 'Black' ],
'ProductCategory' => 'Bike'
}
products.items.create(product)
product = {
'Id' => 202,
'Title' => '21-Bike 202Brand-Company A',
'Description' => '202 description',
'BicycleType' => 'Road',
# 'Brand' => '',
'Price' => 200,
'Gender' => 'M',
'Color' => [ 'Green', 'Black' ],
'ProductCategory' => 'Bicycle'
}
products.items.create(product)
product = {
'Id' => 203,
'Title' => '19-Bike 203',
'Description' => '203 description',
'BicycleType' => 'Road',
'Brand' => 'Brand-Company B',
'Price' => 300,
'Gender' => 'W',
'Color' => [ 'Red', 'Green', 'Black' ],
'ProductCategory' => 'Bike'
}
products.items.create(product)
product = {
'Id' => 204,
'Title' => '18-Bike 204',
'Description' => '204 description',
'BicycleType' => 'Mountain',
'Brand' => 'Brand-Company B',
'Price' => 400,
'Gender' => 'W',
'Color' => [ 'Red' ],
'ProductCategory' => 'Bike'
}
products.items.create(product)
product = {
'Id' => 205,
'Title' => '18-Title 205',
'Description' => '205 description',
'BicycleType' => 'Hybrid',
'Brand' => 'Brand-Company C',
'Price' => 500,
'Gender' => 'B',
'Color' => [ 'Red', 'Black' ],
'ProductCategory' => 'Bike'
}
products.items.create(product)
end
def upload_sample_forums(dynamo_db)
forums = dynamo_db.tables['Forum']
forums.hash_key = { :Name => :string }
forum = {
'Name' => 'Amazon DynamoDB',
'Category' => 'Amazon Web Services',
'Threads' => 2,
'Messages' => 4,
'Views' => 1000
}
forums.items.create(forum)
forum = {
'Name' => 'Amazon S3',
'Category' => 'Amazon Web Services',
'Threads' => 1
}
forums.items.create(forum)
end
def upload_sample_threads(dynamo_db)
threads = dynamo_db.tables['Thread']
threads.hash_key = { :ForumName => :string }
threads.range_key = { :Subject => :string }
thread = {
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 1',
'Message' => 'DynamoDB thread 1 message text',
'LastPostedBy' => 'User A',
'LastPostedDateTime' => (Time.now.utc - (60 * 60 * 24 * 14)).to_s, # 14 days ago
'Views' => 0,
'Replies' => 0,
'Answered' => false.to_s,
'Tags' => [ 'index', 'primarykey', 'table' ]
}
threads.items.create(thread)
thread = {
'ForumName' => 'Amazon DynamoDB',
'Subject' => 'DynamoDB Thread 2',
'Message' => 'DynamoDB thread 2 message text',
'LastPostedBy' => 'User A',
'LastPostedDateTime' => (Time.now.utc - (60 * 60 * 24 * 21)).to_s, # 21 days ago
'Views' => 0,
'Replies' => 0,
'Answered' => false.to_s,
'Tags' => [ 'index', 'primarykey', 'rangekey' ]
}
threads.items.create(thread)
thread = {
'ForumName' => 'Amazon S3',
'Subject' => 'S3 Thread 1',
'Message' => 'S3 thread 3 message text',
'LastPostedBy' => 'User A',
'LastPostedDateTime' => (Time.now.utc - (60 * 60 * 24 * 7)).to_s, # 7 days ago
'Views' => 0,
'Replies' => 0,
'Answered' => false.to_s,
'Tags' => [ 'largeobjects', 'multipart upload' ]
}
threads.items.create(thread)
end
def upload_sample_replies(dynamo_db)
replies = dynamo_db.tables['Reply']
replies.hash_key = { :Id => :string }
replies.range_key = { :ReplyDateTime => :string }
reply = {
'Id' => 'Amazon DynamoDB#DynamoDB Thread 1',
'ReplyDateTime' => (Time.now.utc - (60 * 60 * 24 * 21)).to_s, # 21 days ago
'Message' => 'DynamoDB Thread 1 Reply 1 text',
'PostedBy' => 'User A'
}
replies.items.create(reply)
reply = {
'Id' => 'Amazon DynamoDB#DynamoDB Thread 1',
'ReplyDateTime' => (Time.now.utc - (60 * 60 * 24 * 14)).to_s, # 14 days ago
'Message' => 'DynamoDB Thread 1 Reply 2 text',
'PostedBy' => 'User B'
}
replies.items.create(reply)
reply = {
'Id' => 'Amazon DynamoDB#DynamoDB Thread 1',
'ReplyDateTime' => (Time.now.utc - (60 * 60 * 24 * 7)).to_s, # 14 days ago
'Message' => 'DynamoDB Thread 1 Reply 3 text',
'PostedBy' => 'User B'
}
replies.items.create(reply)
reply = {
'Id' => 'Amazon DynamoDB#DynamoDB Thread 2',
'ReplyDateTime' => (Time.now.utc - (60 * 60 * 24 * 7)).to_s, # 14 days ago
'Message' => 'DynamoDB Thread 2 Reply 1 text',
'PostedBy' => 'User A'
}
replies.items.create(reply)
reply = {
'Id' => 'Amazon DynamoDB#DynamoDB Thread 2',
'ReplyDateTime' => (Time.now.utc - (60 * 60 * 24 * 1)).to_s, # 14 days ago
'Message' => 'DynamoDB Thread 2 Reply 2 text',
'PostedBy' => 'User A'
}
replies.items.create(reply)
end
def list_replies(dynamo_db)
replies = dynamo_db.tables['Reply']
replies.hash_key = { :Id => :string }
replies.range_key = { :ReplyDateTime => :string }
replies.items.query(:hash_value => 'Amazon DynamoDB#DynamoDB Thread 1').each do |item|
puts "#{item.attributes[:ReplyDateTime]} #{item.attributes[:PostedBy]}: #{item.attributes[:Message]}"
end
end
dynamo_db = AWS::DynamoDB.new({ :region => 'us-west-2' })
upload_sample_products(dynamo_db)
upload_sample_forums(dynamo_db)
upload_sample_threads(dynamo_db)
upload_sample_replies(dynamo_db)
list_replies(dynamo_db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment