Using Amazon Simple Db With Rails

4,819 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,819
On SlideShare
0
From Embeds
0
Number of Embeds
863
Actions
Shares
0
Downloads
0
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Using Amazon Simple Db With Rails

  1. 1. Using Amazon SimpleDB with Rails http://webonrails.com
  2. 2. 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 ● Ability to store, process and query data sets http://webonrails.com
  3. 3. Amazon SimpleDB ● HTTP-Interface ● Pricing: Similar to other AWS http://webonrails.com
  4. 4. Before we move Further http://webonrails.com
  5. 5. Let, have a look at Amazon SimpleDB Common Terms http://webonrails.com
  6. 6. Amazon SimpleDB Common Terms ● Domain: storage container ~ table ● Item: ~ table rows accessed by ID ~ primary key ● Attribute: ~ table columns; every item may have a different set of upto 256 attributes http://webonrails.com
  7. 7. Amazon SimpleDB Common Terms ● Value: each Attribute may havemultiple Values, always varchar(1024)[] http://webonrails.com
  8. 8. SimpleDB Domain A Attributes Items Values (multiple) http://webonrails.com
  9. 9. PUT (item, 123), (description, sweater), (color, blue), (color, red) PUT (item, 456), (description, dress shirt), (color, white), (color, blue) http://webonrails.com
  10. 10. 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 ● Beware: Eventual Consistency Approach! http://webonrails.com
  11. 11. Benefits over traditional databases ● No need to pre-define data types ● 'Size' can be 9 or XL or 1.5 ● Add attributes anytime ● Attributes specifically for some items ● Index all data http://webonrails.com
  12. 12. API Summary ● CreateDomain — Create a domain(100). ● DeleteDomain — Delete a domain. ● ListDomains — List all domains. ● DomainMetadata — Retrieve information about the domain. http://webonrails.com
  13. 13. API Summary ● PutAttributes — Add or update an item and its attributes, or add attribute-value pairs to items that exist already. ● GetAttributes — Retrieve an item and all or a subset of its attributes and values. ● DeleteAttributes — Delete an item, an attribute, or an attribute value. http://webonrails.com
  14. 14. API Summary ● Query — Query the dataset using a query expression which specifies value tests on one or more attributes. ● Supported value tests are: =, !=, <, > <=, >=, starts-with. [“price” < “12.00”] INTERSECTION [“color” = “green”]. ● Order results using the SORT operator http://webonrails.com
  15. 15. API Summary ● QueryWithAttributes — Enables developers to retrieve all or a subset of the information associated with items returned as a response to a particular query. http://webonrails.com
  16. 16. SimpleDB vs S3 S3 SimpleDB ● Stores raw data ● Stores indexed data ● Uses higher dense ● Uses less dense storage drives storage drives http://webonrails.com
  17. 17. SimpleDB Limits (Cont...) ● Domain size 10 GB per domain ● Domain size 250,000,000 attribute name- value pairs ● Domain name 3-255 characters ● Domains per account 100 ● Attribute name-value pairs/item 256 ● Attribute name length 1024 bytes ● Attribute value length 1024 bytes ● Item name length 1024 bytes ● All UTF-8 characters that are valid in XML documents are allowed in name/value. http://webonrails.com
  18. 18. SimpleDB Limits ● Attributes per PutAttributes operation 100 ● Maximum items in query response 250 ● Maximum query execution time 5 seconds ● Maximum comparisons per predicate 10 ● Maximum predicates per query 10 expression ● Maximum response size for 1MB QueryWithAttributes http://webonrails.com
  19. 19. Working with Numerical Data ● SimpleDB is a schema-less data store ● Everything is stored as a UTF-8 string value http://webonrails.com
  20. 20. Working with Numerical Data ● Ensure that every number is positive http://webonrails.com
  21. 21. Working with Numerical Data ● Ensure that every number is positive What about Negative Numbers? http://webonrails.com
  22. 22. Negative Numbers Offsets ● Choose an offset ● That should be arger than the smallest expected negative number in your data set ● Ex: ● if the smallest expected number in your data set is -12, choosing offset = 100 might be safe http://webonrails.com
  23. 23. Negative Numbers Offsets The following is a sample original data set. 14.58, -12.7, 20, 65, -23 If you apply an offset of 100 the following is the resulting data set: 114.58, 87.3, 120, 165, 77 http://webonrails.com
  24. 24. Zero Padding ● Since simpleDB uses lexicographical comparisons “10” comes before “2” ● If we zero pad the numbers to five digits, quot;00002quot; comes before quot;00010quot; http://webonrails.com
  25. 25. Zero Padding ● Since simpleDB uses lexicographical comparisons “10” comes before “2” ● If we zero pad the numbers to five digits, quot;00002quot; comes before quot;00010quot; http://webonrails.com
  26. 26. Dates ● ISO 8601 format http://webonrails.com
  27. 27. Query dataset ['attribute1' = 'value1'] intersection not ['attribute2' = 'value2'] union ['attribute3' = 'value3'] http://webonrails.com
  28. 28. SimpleDB with Rails AWS SDB Proxy Plugin http://webonrails.com
  29. 29. http://webonrails.com
  30. 30. SimpleDB with Rails ● Install aws-sdb gem ● gem install aws-sdb ● Install aws_sdb_proxy plugin: ● script/plugin install git://github.com/bansalakhil/aws_sdb_proxy.git *Plugin originaly written by martin.rehfeld@glnetworks.de http://webonrails.com
  31. 31. AWS SDB Proxy Plugin ● Configure config/aws_sdb_proxy.yml ● development: aws_access_key_id: <aws key> aws_secret_access_key: <aws secret key> port: 8888 ● Create new domain on Amazon SimpleDB ● rake aws_sdb:create_domain DOMAIN=MyDataStore http://webonrails.com
  32. 32. AWS SDB Proxy Plugin ● Start AWS SDB proxy ● rake aws_sdb:start_proxy_in_foreground http://webonrails.com
  33. 33. Mapping RESTful URLs http://webonrails.com
  34. 34. Using AWS SDB Proxy Plugin ● Create demo ActiveResource model class Post < ActiveResource::Base self.site = quot;http://localhost:8888quot; self.prefix = quot;/MyDataStore/quot; end http://webonrails.com
  35. 35. Lets try at script/console >> Article.create(:title => quot;This is my First articlequot;) => #<Article:0xb7005d04 @attributes={quot;updated_atquot;=>Sat Dec 13 15:53:30 UTC 2008, quot;titlequot;=>quot;This is my First articlequot;, quot;idquot;=>601834...98, quot;created_atquot;=>Sat Dec 13 15:53:30 UTC 2008}, @prefix_options={}> >> a = Article.create(:title => quot;This is my Second articlequot;) => #<Article:0xb6fe1f08 @attributes={quot;updated_atquot;=>Sat Dec 13 15:53:52 UTC 2008, quot;titlequot;=>quot;This is my Second articlequot;, quot;idquot;=>112...9, quot;created_atquot;=>Sat Dec 13 15:53:52 UTC 2008}, @prefix_options={}> >> a.body = quot;Nothingquot; >> a.save => true http://webonrails.com
  36. 36. Lets try at script/console >> Article.find(:all).size => 2 >> Article.find(:all, :from => :query, :params => quot;['title' starts-with 'This is' ]quot;) => [#<Article:0xb6f73080 @attributes={quot;updated_atquot;=>Sat Dec 13 15:53:30 UTC 2008, quot;titlequot;=>quot;This is my First articlequot;, quot;idquot;=>60...98, quot;created_atquot;=>Sat Dec 13 15:53:30 UTC 2008}, @prefix_options={}>, #<Article:0xb6f7306c @attributes={quot;updated_atquot;=>Sat Dec 13 15:54:14 UTC 2008, quot;bodyquot;=>quot;Nothingquot;, quot;titlequot;=>quot;This is my Second articlequot;, quot;idquot;=>11...79, quot;created_atquot;=>Sat Dec 13 15:53:52 UTC 2008}, @prefix_options={}>] http://webonrails.com
  37. 37. References ● http://aws.amazon.com/simpledb/ ● http://inside.glnetworks.de http://webonrails.com
  38. 38. Thanks Akhil Bansal http://webonrails.com http://webonrails.com

×