Developing with Couchbase_Getting_started_Tokyo_2014

542 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
542
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Bulletize the text. Make sure the builds work.
  • Bulletize the text. Make sure build work properly.
  • Bulletize the text. Make sure build work properly.
  • Couchbase client
    Basically, give people an overview of how the SDK’s get notified when there is a topology change via async REST.
    On the SDK side, we make sure the SDK’s as much as possible, get to the know the new cluster config – over streaming API.
  • Again, through Streaming API, our Client is made aware of the new Topology
    Under ideal conditions no pauses – usually realtime.
  • So all the SDKs are responsible of accessing data but also keep the cluster topology up to date.
  • Getting started with Couchbase is intentionally very easy. Couchbase is Open Source under an Apache 2.0 license, but there are binaries built for just about any platform. Most of the time, getting started with Couchbase is just a matter of downloading the package for your OS or the setup.
    Setting up the client library is equally simple.
  • Download product and install the sdk:
    - install in the session
  • Access the value in a fast and consistent way!
  • We can have more durability PersistTo and ReplicateTo. Persist to master (1), two, three.
    Using PersistTo master is the vbucket on which the key is stored. If we use PersistTo, we trade off some performance for more data Durability.
    ReplicateTo is the fastest, as it’s just sending the doc to the other node.
    We only use these when we really need it.
  • Access the value in a fast and consistent way!
  • We can have more durability PersistTo and ReplicateTo. Persist to master (1), two, three.
    Using PersistTo master is the vbucket on which the key is stored. If we use PersistTo, we trade off some performance for more data Durability.
    ReplicateTo is the fastest, as it’s just sending the doc to the other node.
    We only use these when we really need it.
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • We’re looking up by the email address which is the key.
    If user found, show details, if not, ‘user not registered’
  • We’re adding a field into our model here. With zero downtime. True agile development.
    Finding the user, adding the fields, saving.
  • If we have two different app servers trying to update the doc, the first one that gets there using the CAS value, will win. The second will fail.
    Every time any data is changed in a document, the CAS value changes. If we use the same CAS twice, it’s not going to work twice.
  • Developing with Couchbase_Getting_started_Tokyo_2014

    1. 1. Developing with Couchbase: Getting Started att Ingenthron irector, Developer Solutions
    2. 2. Getting Started
    3. 3. Couchbase SDK Official SDKs Ruby Python Community SDKs Go Clojure www.couchbase.com/develop
    4. 4. Client Architecture Overview • Based on the information given, the Client tries to establish an initial connection 最初に Couchbase Server と同期する • Once that’s done, it connects to a streaming API (HTTP chunked) 同期後、ストリーミングの API に接続する( HTTP に投げ る) •接続により、クラスタのアップデートがフェッチされる Cluster updates are fetched from that connection • Failover/Add/Remove scenarios all update the clients in near real-time – no application restarts required! フェイルオーバーとサーバの追加・削除は、ほぼリアルタイムで 行われ、アプリケーションの再起動は不要 •Key/Value アクセスは直接ノードから行われる the nodes Key/Value access is done directly against • For View access one of the nodes is picked out which aggregates the results. View へのアクセスにより、各ノード(に保持されるドキュメン ト)の結果が集められる。
    5. 5. Client Setup: Getting Cluster Configuration e mak Couchbase Client App Server r ct onne c i on og opol et eceiv y
    6. 6. Bootstrap 1. GET /pools Pool をゲットする 2. Look for thePool を探すpool "default" デフォルトの 3. GET /pools/default デフォルトの Pool をゲットする 4. Look for the "buckets" hash which contains the bucket list バケットリストを持つバケットを探す バケットリストを持つバケットをデフォルトプールよりを 5. GET /pools/default/buckets ゲットする 6. Parse the list of buckets and extract the one provided by バケットリストをパース(解析)し、アプリケーションから提 the application 供されるものを選択する 7. バケットリストを持つバケットをデフォルトプールよりを GET /pools/default/buckets/ ゲットする
    7. 7. Client Setup: Getting Cluster Configuration Couchbase Client New Node Added to Cluster and Coming Online Couchbase Couchbase Topology Topology Update Update
    8. 8. SDK & libcouchbase Dependency • Java, .Net, C, Go are native • The SDK does not have dependency on other language • Ruby, PHP, Python, Node have dependency to libcouchbase • They are “wrapper” on the top of the C library • Scala, Clojure, JRuby are using Couchbase Java SDK
    9. 9. How to Get Started
    10. 10. Quick Start : Couchbase Server Download from http://www.couchbase.com/download Install via .rpm, .deb, .exe, or .app EASY!
    11. 11. Quick Start : Client 1. Go to the developer page http://www.couchbase.com/develop デベロッパーページを参照する 2. Select your language 使用言語を選択する 3. Follow the “Getting Started” page Getting Started ページの記述に従う • Java • C • .Net • Python • Ruby • Node • PHP • …
    12. 12. Demonstration
    13. 13. Basics: Retrieve et (key) ドキュメントの取り ­ Retrieve a document 出し ets(key) ­ Retrieve a document and the CAS value associated ドキュメントと CAS 値の取り出し with the object (more on this in a bit)
    14. 14. Basics: Create, Update, Delete et (key, value) ­ ドキュメントを保存する。ドキュメントが存在する場合は Store a document, overwrites if exists 上書き保存。 dd (key, value) ドキュメントを保存する。ドキュメントが存在する場合は ­ エラー /exception を返す。 Store a document, error/exception if it already exists ドキュメントを保存する。ドキュメントが存在しない場合 eplace (key, value) ­ はエラー document, error/exception if doesn’t exist Store a /exception を返す。 ドキュメントを削除する。 elete(key) ­ Delete the document from the system
    15. 15. Fundamentals • Couchbase is structured as a Key-Value store: every Document has a Key and a Value Couchbse Server は KVS として構成され、全てのドキュメントは Key と Value を持つ • Key can be up to 250 characters long Key は最大 250 文字 • Keys are unique, within a database (bucket), there can only be one document with a associated key Key はバケット単位で、ユニーク。キーは唯一のドキュメントと関連付 けられる。 • Keys are completely in the control of the application developer, there is no internal mechanism for key generation Key はアプリケーション開発者により自由に制御可能。内部的なキー生 成メカニズムは持っていない。 • Values can be JSON, strings, numbers, binary blobs, or a special Value はatomic 、文字列、数、バイナリブロブ、または符号無しの整数 positive JSON counter (unsigned integer) Value は最大 20MB
    16. 16. Dealing with keys • You are responsible of the key Key を定義する責任は、開発者に帰します。 • Keys can be build from: • UUID • Atomic Counter • Date/TimeStamp • Contains Separator: • User:001 • Product_XYZ
    17. 17. What about the value ? • Couchbase stores the data the way you send them Couchbase は(クライアントから)送られてきたデータをそのまま格 • 納します。 For example: • cb.set(“mykey”, “This is my Value”); • cb.set(“mykey”, 100.2); • cb.set(“mykey”, javaObjectSerialized); • What about JSON? JSON ドキュメントの場合は? • cb.set(“mykey”, "{"msg" : "This is the value"}"); • cb.set(“mykey”, json.toJson( myJavaObject) ); • Your application deals with the JSON Document
    18. 18. Q&A
    19. 19. Getting Started – Part 2
    20. 20. Basics: Retrieve おさらい et (key) ドキュメントの取り ­ Retrieve a document 出し ets(key) ドキュメントと CAS 値の取り出し ­ Retrieve a document and the CAS value associated with the object (more on this in a bit)
    21. 21. Basics: Create, Update, Delete et (key, value) ­ ドキュメントを保存する。ドキュメントが存在する場合は Store a document, overwrites if exists 上書き保存。 ドキュメントを保存する。ドキュメントが存在する場合は dd (key, value) ­ エラー /exception を返す。 Store a document, error/exception if it already exists ドキュメントを保存し、ドキュメントが存在しない場合は エラー /exception を返す。 eplace (key, value) ­ Store a document, error/exception if doesn’t exist ドキュメントを削除する。
    22. 22. Other Options 永続性要件 urability Requirements ­ PersistTo.ONE ­ ReplicateTo.ONE ­ Simply add this to your method call when needed • ops = cb.set(“mykey”, myObject, PersistTo.ONE); ドキュメントの有効期限 (TTL) ime to Live (TTL) ­ Used to delete the value after the specified time • cb.set(“mykey”, 30 x 24 x 60 x 60, myObject); // keep the value in Couchbase for 30 days.
    23. 23. Atomic Integers Atomic Counters are a special structure in Couchbase, they are executed in order and are Positive Integer Values Atomic Counter は Couchbase により特別に取り扱われる構造であり、値 は正の整数とし、 ( 厳密に ) 番号順に実行される •set (key, value) ­ Use set to initialize the counter • cb.set(“my_counter”, 1) Counter をスタートする •incr (key) ­ Increase an atomic counter value, default by 1 • cb.incr(“my_counter”) # now it’s 2 •decr (key) ­ 1 から順に番号を大きくする Decrease an atomic counter value, default by 1 • cb.decr(“my_counter”) # now it’s 1 1 から順に番号を小さくする
    24. 24. Couchbase Patterns •Atomic Counter for keys Key としての Atomic Counter •Find your ID by numbers, loop on the values, … •Lookup Key サーチ •Lookup a document/values using multiple keys •Lists リスト •Lookup pattern, using list of values インデックスとクエリ •Indices and Queries
    25. 25. Use a Counter Counter の使用(例えば新規ユーザ登 録) cb = new CouchbaseClient(uris, "default", ""); Gson json = new Gson() // create a new User User user = new User("John Doe", "john@demo.com", "7621387216"); long userCounter = cb.incr("user_counter", 1, 1); cb.set( "user:"+ userCounter, json.toJson(user) ); // create a new user; user = new User("Jane Smith Doe", "jane@demo.com", "355662216"); userCounter = cb.incr("user_counter", 1); cb.set( "user:"+ userCounter, json.toJson(user) );
    26. 26. Use a Counter
    27. 27. Lookup 情報のサーチ cb = new CouchbaseClient(uris, "default", ""); Gson json = new Gson() // create a new User User user = new User("John Doe", "john@demo.com", "7621387216"); long userCounter = cb.incr("user_counter", 1); String key = "user:"+ userCounter; cb.set(key, json.toJson(user) ); // create another key for lookup cb.set(“email:john@demo.com”, key); // Find by email String keyToUser = cb.get(“email:john@demo.com”); Object user = cb.get(keyToUser);
    28. 28. Agile Model Development
    29. 29. Simple Example in Ruby # example.rb require “./user.rb” u1 = User.new({ :email => robin@couchbase.com, :name => “Robin Johnson”, :title => “Developer Advocate”, :twitter => “@rbin” }) u1.save # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter def initialize(attr = {}) attr.each do |name, value| setter = "#{name}=” next unless respond_to?(setter) send(setter, value) end end def save client = Couchbase.bucket client.set(@email.downcase, self.to_json) end end
    30. 30. Add Lookup Class Method # example.rb require “./user.rb” u1 = User.new({ :email => robin@couchbase.com, :name => “Robin Johnson”, :title => “Developer Advocate”, :twitter => “@rbin” }) u1.save u1 = User.find_by_email(“robin@couchbase.com”) if u1 puts “User Found!” puts u1.inspect else puts “User Not Registered!” end # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter def initialize(attr = {}) ... end def save c = Couchbase.bucket c.set(@email.downcase, self.to_json) end def self.find_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
    31. 31. Agile Model Development # example.rb require “./user.rb” u1 = User.find_by_email(“robin@couchbase.com”) if u1 u1.fb_id = “682829292” u1.fb_token = “f02jdjd020d8373730djd02” u1.save else # create user end # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter attr_accessor :fb_id, :fb_token def initialize(attr = {}) ... end def save ... end def self.find_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
    32. 32. Concurrency & more
    33. 33. Compare and Swap Optimistic Concurrency in a Distributed System 分散システムにおける楽観的同時並行性 # actors.rb c = Couchbase.bucket c.set(“mydoc”, { :myvalue => nil } doc1, flags, cas = c.get(“mydoc”, :extended => true) # c.set (“mydoc”, { “myvalue”: true }, :cas => cas) # will fail because cas has changed c.set (“mydoc”, { “myvalue”: true }, :cas => cas) Success CAS mismatch!
    34. 34. Replica Read レプリカデータの読込み ead the replica when you cannot reach the node responsible for the data try { value = (String)cb.get(key); System.out.println("Master node read"); } catch (Exception e ) { value = (String)cb.getFromReplica(key); System.out.println("Doing a Replica Read"); }
    35. 35. Conclusion • Couchbase Server is installed Couchbase Server のインストール • Couchbase Client SDK is installed Couchbase Client SDK のインストール • Core operations to save and get the data データ読み書きのための基本オペレーション • What’s next? •ドキュメントをどのようにデザインすればよいか?(ドキュメントデ How do you design your data? Document Design ザイン) •データをどのように検索すればよいか?(インデックスとクエ How do you find your data? Indexing and Querying リ) •その他に、、、よく使われるデータモデルと、データ統合 Do even more… Common Models and Integration
    36. 36. Q&A

    ×