Using Amazon SimpleDB with Ruby on Rails
-
Upload
martin-rehfeld -
Category
Technology
-
view
12.576 -
download
1
description
Transcript of Using Amazon SimpleDB with Ruby on Rails
Using Amazon SimpleDB with Rails
Martin Rehfeld@ RUG-B 07-Feb-2008
Remember?AWS Rails Scenario
Static Content
SQL Database
Web Server
Application Server
Code
memcached,backgrounDRb,
...
EC2 S3
}HOW TO GET A PERSISTENT DB?
Amazon SimpleDB• Database web service advertised as
• Simple, Flexible, Scalable, Fast,Reliable, Inexpensive
• No RDBMS: no SQL, no joins, no schema,no referential integrity, no transactions
• HTTP-Interface
• Pay per use:
• $0.14 per Machine Hour consumed
• $0.10-0.18 per GB - data transfer
• $1.50 per GB-month storage
limitedbeta
Amazon SimpleDBCommon Terms
• Domain:storage container ~ table
• Item:~ table rows accessed byID ~ primary key
• Attribute:~ table columns; every item may have a different set of up to 256 attributes
• Value:each Attribute may have multiple Values, always varchar(1024)[]
SimpleDB
...my domain B
my domain A
}}
(multiple) values
item
s
attributes
SimpleDB @
(amazon_sdb)rich API*
*) defect release 0.6.5 when evaluated, but newer release available (not re-tested yet)
no codeno codeno codeno codeno codeno code
no code
simply working!
Amazon SimpleDB API
• Domain level
• CREATE, LIST, DELETE
• Item level
• GET, PUT, DELETE attributes with values
• QUERY* for unordered item IDs by attribute values within one domain*) lexicographical =, !=, <, > <=, >=, STARTS-WITH, AND, OR, NOT, INTERSECTION / UNION
• Beware: Eventual Consistency Approach!
Almost RESTful
Mapping RESTful URLs
HTTP/REST
GET /domain?query
GET /domain/itemID
POST /domain/itemID
PUT /domain/itemID
DELETE /domain/itemID
SimpleDB
QUERY
GET ATTRIBUTES
PUT ATTRIBUTES
PUT ATTRIBUTES(replace)
DELETE ATTRIBUTES
Mapping RESTful URLs
HTTP/REST
GET /domain/resource?query
GET /domain/resource/itemID
POST /domain/resource/itemID
PUT /domain/resource/itemID
DELETE /domain/resource/itemID
SimpleDB
QUERY
GET ATTRIBUTES
PUT ATTRIBUTES
PUT ATTRIBUTES(replace)
DELETE ATTRIBUTES
Emulating multiple resources in one domain via _resource attribute
A Bridge to Rails
SimpleDB
ActiveResource
HTTP/REST
HTTP/SDB API
„Rails XML“
„AWS XML“?AWS
SDB ProxyServer*
*) http://inside.glnetworks.de/2008/01/20/bridging-rails-to-amazon-simpledb-using-activeresource/
AWS SDB Proxy Plugin I• Install aws-sdb gem
gem install aws-sdb
• Install aws_sdb_proxy plugin:script/plugin install http://rug-b.rubyforge.org/svn/aws_sdb_proxy
• Configure config/aws_sdb_proxy.ymldevelopment: aws_access_key_id: <your amazon aws key> aws_secret_access_key: <your amazon aws secret key> port: 8888
• Create new domain on Amazon SimpleDBrake aws_sdb:create_domain DOMAIN=ActiveResourceStore
• Start AWS SDB Proxyrake aws_sdb:start_proxy_in_foreground
AWS SDB Proxy Plugin II• Create demo ActiveResource model
class Post < ActiveResource::Base self.site = "http://localhost:8888" self.prefix = "/ActiveResourceStore/"end
• Testdrive in script/console>> p = Post.create(:title => 'My first SimpleDB post')=> #<Post:0x198ceec @prefix_options={}, @attributes={...}>
>> p.body = 'Content is king'=> "Content is king"
>> p.save=> true
>> Post.find(:first, :params => { :title => 'My first SimpleDB post' })=> #<Post:0x18efef8 @prefix_options={}, @attributes={...}>
Vision: Fully Virtualized Scalable Stack...
• Building blocks for AWS-only web app deployment:
• Static assets/content in S3
• As many app servers as needed on EC2
• Structured data persisted in SimpleDB
• All client/server interactions via SQSNote: XmlHttpRequest cross-site restrictions must be examined