Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CouchDB Talk, Codestock 2012

CouchDB Talk, Codestock 2012

digitalbush

June 19, 2012
Tweet

Other Decks in Programming

Transcript

  1. CouchDB • Document Database • Open Source • Written in

    Erlang • RESTful HTTP API • JSON • Query via Map/Reduce • Bi-directional replication
  2. Right Tool, Right Job. • This is not a SQL

    hate fest. • CouchDB is not a silver bullet. • Use the tool that works best for the problem you are trying to solve.
  3. NoSQL Describes a broad range of database technologies which are

    non-relational. Focused towards solving specific problems which are not suited for relational storage.
  4. Documents • Just a collection of key value pairs. •

    Represented as JSON. • No need to store things separately which naturally go together. • Can also have attachments. • Retrieved via key.
  5. JSON Document { “Name”:”Document”, “Description”:”Collection of key/value pairs”, “IsAwesome”:true, “Value-Types”:[“Number”,”String”,”Boolean”,”Array”,”Object”],

    “Volume”: 11, “Has-The-Following-Default-Properties”:{ “_id”: “uniquely identifies the document”, “_rev”: “version of the document” } }
  6. Example Order #7 Burger French Fries Coke Bun Type Meat

    Type Toppings Lettuce Tomato Mustard Onion Pickles
  7. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table
  8. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table OrderItems Table
  9. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Letuce Tomato Mustard Onion Pickle Order Table Don’t forget the foreign key! OrderItems Table
  10. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table Don’t forget the foreign key! OrderItems Table BurgerItem Table
  11. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table Don’t forget the foreign key! OrderItems Table BurgerItem Table Another Foreign Key (One to One)
  12. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table Don’t forget the foreign key! OrderItems Table BurgerItem Table Another Foreign Key (One to One) Toppings Table
  13. Relational Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Order Table Don’t forget the foreign key! OrderItems Table BurgerItem Table Another Foreign Key (One to One) Toppings Table MOAR Foreign Key!
  14. Document Database Order #7 Burger French Fries Coke Bun Type

    Meat Type Toppings Lettuce Tomato Mustard Onion Pickles Store in one document
  15. Document Database { “_id”: 7, “Items”: [ { “Type”: “Burger”,

    “BunType”: “Sesame Seed”, “MeatType”: “1/4 lb”, “Toppings”: [“Lettuce”,”Tomato”,”Mustard”,”Onion”,”Pickles”] }, {“Type”: “French Fries”}, {“Type”: “Coke”} ] } Represented as JSON
  16. API • Create • POST http://{host}/{database} • PUT http://{host}/{database}/{id} •

    Read • GET http://{host}/{database}/{id} • Update • PUT http://{host}/{database}/{id} • Delete • DELETE http://{host}/{database}/{id}?rev={rev}
  17. That Was Fun? My demos show it the “hard” way.

    You have other options: • RestSharp (http://restsharp.org/) • New HttpClient in .NET 4.5 (or NuGet) • Relax (https://github.com/arobson/Relax) • LoveSeat (https://github.com/soitgoes/LoveSeat)
  18. Map/Reduce • Used to create views on the data. •

    Roughly equivalent to a materialized view(snapshot) with indexing (sort of). • Useful for querying/some reporting.
  19. Map • Takes data in and transforms it into something

    else. • Output is key/value pairs. • Keys can be complex types. • Like a select in LINQ.
  20. Reduce • Takes in a set of intermediate values and

    combines them into a single value. • Reduce needs to be able to accept results from the map function AND the reduce function itself. • Like an .Aggregate() in LINQ
  21. Map/Reduce in LINQ var test=new [] {"1","2","3”}; var mapped =

    test.Select(Int32.Parse); var reduced = mapped.Aggregate((sum,i)=>sum+=i);
  22. View API • Read • GET http://{host}/{database}/_design/{doc}/_view/{name} • Map Only

    • ?reduce=false • Key • Specific - ?key={key} • Range - ?startkey={lower bound}&endkey={upper bound} • Grouping • ?group_level={n} • Include Documents • ?include_docs=true