Share
Embed
Couchbase Cluster: The Ruby SDK
Sergey Avseyev
September 13, 2013
Transcript
23 slides
-
Presenta6on Summary • Couchbase ruby client overview • Basic opera6ons
• Working with JSON • View opera6ons • Rails integra6on Friday, September 13, 13 -
About the Library • Open source: Apache 2.0 License •
Current version is 1.3.2 • Built atop of libcouchbase as C extension to ruby • Works with ruby 1.8.7+ on UNIX-‐like plaUorms and Windows Friday, September 13, 13 -
Making connec6on • Connec6on arguments passed using a Hash options
= { :bucket => "test", :password => "secret", :node_list => ["example.com", "example.org"] } • Basic form Couchbase::Bucket.new(options) • A bit more handy Couchbase.connect(options) Friday, September 13, 13 -
Thread-‐local Singleton • Rather long living threads can has their
own copies of connec6ons Couchbase.connection_options = { :password => "secret", :node_list => ["example.com", "example.org"] } default = Couchbase.bucket # use named slots to keep several connections users = Couchbase.bucket("users") Friday, September 13, 13 -
Basic Opera6ons: SET • Single set conn.set("john", {"balance" => 100.0})
#=> 1953098181688492032 •Set mul6ple values conn.set("john" => {"balance" => 100.0}, "bill" => {"balance" => 200.0}) #=> {"john"=>2206872111421718528, "bill"=>4374793481558818816} Friday, September 13, 13 -
Basic Opera6ons: GET • Single get conn.get("john") #=> {"balance"=>100.0} •
Get mul6ple values conn.get("john", "bill") #=> [{"balance"=>100.0}, {"balance"=>200.0}] • Get with meta info conn.get("john", "bill", :extended => true) #=> {"john"=>[{"balance"=>100.0}, 0, 2206872111421718528], "bill"=>[{"balance"=>200.0}, 0, 4374793481558818816]} Friday, September 13, 13 -
Basic Opera6ons: Batching • Run command asynchronously conn.run do conn.get("john")
{|ret| puts ret.value} conn.get("bill") {|ret| puts ret.value} end Friday, September 13, 13 -
JSON by default • It uses Couchbase::Transcoder::Document by default doc
= {:team => [ "matt", "sergey", "trond", "mark", "michael", "mark"]} conn.set("sdk", doc) puts conn.get("sdk", :transcoder => nil) #=> {"team":["matt","sergey","trond", "mark","michael","mark"]} Friday, September 13, 13 -
Couchbase Views: Map/Reduce • Map function(doc, meta) { if (doc.type
== "brewery" && doc.country) { emit(doc.country, 1); } } • Reduce function(key, values, rereduce) { return sum(values); } Friday, September 13, 13 -
Views in Ruby • Create design document conn.save_design_doc(File.read("test.json")) • Query
view ddoc = conn.design_docs["test"] ddoc.view(:group_level => 1).each do |d| puts [d.key, d.value].inspect end #=> ["Argentina", 2] ["Aruba", 1] ["Australia", 14] ["Austria", 10] .... Friday, September 13, 13 -
Rails Session Store • Gemfile gem "couchbase" • config/applica6on.rb require
'action_dispatch/middleware/session/ couchbase_store' config.session_store(:couchbase_store, :namespace => "session:", :couchbase => { :bucket => 'sessions', :default_format => :marshal}) Friday, September 13, 13 -
Rails Cache Store • Gemfile gem "couchbase" • config/applica6on.rb config.cache_store
= :couchbase_store, { :bucket => 'cache', :expires_in => 3600} Friday, September 13, 13 -
Config • Gemfile gem "couchbase-model" • config/couchbase.yaml development: hostname: localhost
port: 8091 bucket: beer-sample Friday, September 13, 13 -
Model • app/model/beer.rb class Beer < Couchbase::Model attribute :name attribute
:abv, :default => 0 attribute :description belongs_to :brewery view :all, :limit => 31 view :by_category, :include_docs => false, :group => true before_save do |doc| doc.abv = doc.abv.to_f end end Friday, September 13, 13 -
Views • app/model/beer/by_category/map.js function(doc, meta) { if (doc.type == "beer")
{ emit(doc.category || "Unknown"); } } • app/model/beer/by_category/reduce.js _count Friday, September 13, 13