Coding with Riak (from Velocity 2015)

Embed Size (px)

Citation preview

  1. 1. Coding with Riak by Matt Brender
  2. 2. 2 { "text": Hello #VelocityConf!", "entities": { "hashtags": [#VelocityConf], "symbols": [], "urls": [], "user_mentions": [{ "screen_name": mjbrender", "name": Matt Brender", "id": 4948123, "id_str": 42424242", "indices": [81, 92] }, { "screen_name": mjbrender", "name": Matt Brender", "id": 376825877, "id_str": "376825877", "indices": [121, 132] }] }}
  3. 3. 3
  4. 4. git clone https://github.com/basho-labs riak-dev cluster 4
  5. 5. (riak-dev-cluster) rake bootstrap (riak-dev-cluster) rake member_status ---- Cluster Status ---- Ring ready: true 5 Starting the riak-dev-cluster for a local dev environment
  6. 6. 6 Starting the Riak.app for a local dev environment
  7. 7. 7 Starting the Riak.app getting search going right away https://github.com/mjbrender/riak- app-enable-search
  8. 8. Its not about NoSQL 8
  9. 9. What we really need.. 9
  10. 10. is a database that allows my app to scale 10
  11. 11. Enter Riak 11
  12. 12. Using Riak the art of key/value 12
  13. 13. Keys simply binary values used to identify Objects.* Values can be numbers, strings, objects, binaries, etc. Buckets used to define a virtual namespace for storing Riak objects. Data Model Riak stores data as a combination of keys and values in buckets 13
  14. 14. curl http://127.0.0.1:8098/types/places/buckets/country/keys/US { "Alpha2_s": "US, "Alpha3_s": "USA, "EnglishName_s": "United States, "NumericCode_i": 840 } Riak offers both HTTP and Protocol Buffers APIs. The following HTTP API example uses curl to retrieve a value by key: Note: Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data think XML, but smaller, faster, and simpler. Data Model 14
  15. 15. There are a diverse group of client libraries for Riak that support both the HTTP and Protocol Buffer APIs: Basho Supported Libraries: Java Ruby Python PHP Erlang .NET Node.js Community Libraries: C Clojure Go Perl Scala R Polylingual
  16. 16. export RIAK_HOST=http://localhost:8098 curl -v $RIAK_HOST/buckets/capitals/keys/usa-X PUT-H "content-type: text/plain-d "Washington D.C. * Trying 127.0.0.1... connected > PUT /buckets/capitals/keys/usa HTTP/1.1 > Content-Type: text/plain > < HTTP/1.1 204 No Content 16 Storing data Through HTTP
  17. 17. curl $RIAK_HOST/buckets/capitals/keys/usa Washington D.C. 17 Retrieving data Through HTTP
  18. 18. curl -v $RIAK_HOST/buckets/capitals/keys/usa Trying 127.0.0.1... Connected > GET /buckets/capitals/keys/usa HTTP/1.1 < HTTP/1.1 200 OK < X-Riak-Vclock: a85hYGBgzGDKBVIcR4M2cvvL1tzJYEpkzGNluGw/+QRfFgA= < Vary: Accept-Encoding < Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue) < Link: /capitals>; rel="up" < Last-Modified: Tue, 26 May 2015 14:44:35 GMT < ETag: "1yAFlUinalK2zNd7LpkOgU Washington D.C. 18 Retrieving data Through HTTP (extended)
  19. 19. gem install riak-client Successfully installed riak-client-2.1.0 1 gem installed 19 https://github.com/basho/riak-ruby-client A Simple Ruby App Starting with the client
  20. 20. >> require 'riak' => true >> client = Riak::Client.new(:pb_port => 8087) => #]> >> client.ping => true 20 A Simple Ruby App That stores and retrieves data
  21. 21. >> default_bucket = client.bucket("default") => # >> value = default_bucket.new("Monty Python") => #]> >> value.data = ["Graham Chapman", "Eric Idle", "Terry Gilliam", "Terry Jones", "John Cleese", "Michael Palin] >> value.store() => #]> 21 A Simple Ruby App That stores and retrieves data
  22. 22. >> fetched = default_bucket.get("Monty Python") => #]> >> fetched.data.to_json => "["Graham Chapman","Eric Idle","Terry Gilliam","Terry Jones","John Cleese","Michael Palin"]" 22 A Simple Ruby App That stores and retrieves data
  23. 23. Searching in Riak
  24. 24. riak-2.1.0 cat etc/riak.conf | grep -i search ## this to be set to 'active', including search. ## To enable Search set this 'on'. search = on 24 Solr will index any field that it recognizes, based on the index's schema. The default schema (_yz_default) uses the suffix to decide the field type (_s represents a string, _i is an integer, _b is binary and so on). A Simple Search Enabling search in Riak
  25. 25. >> client.create_search_index(libraries, _yz_default) => true >> bucket = client.bucket(coding-with-riak) => # >> bucket.properties = {search_index => libraries} 25 A Simple Search Enabling search in Riak
  26. 26. Riak stores data as a combination of keys and values in buckets A SIMPLE SEARCH Key Value Bucket ruby { "name_s" : "Ruby Client", "maintainer_s" : Basho", "popular_b" : true } coding-with-riak go { "name_s" : "Go Client", "popular_b" : true, "maintainer_s" : Community" } coding-with-riak 26 A Simple Search Using Apache Solr
  27. 27. >> results = client.search("libraries", "maintainer_s:Basho") => {"max_score"=>1.8109302520751953, "num_found"=>1, "docs"=>[{"score"=>"1.81093030000000010382e+00", "_yz_rb"=>"coding-with-riak", "_yz_rt"=>"default", "_yz_rk"=>"ruby", "_yz_id"=>"1*default*coding-with- riak*ruby*57", "name_s"=>"Ruby Client", "maintainer_s"=>Basho", "popular_b"=>"true"}]} 27 A Simple Search Using Apache Solr
  28. 28. >> py = bucket.new("python") => #]> >> py.data = {"name_s"=>"Python Client", "maintainer_s"=>Basho", "popular_b"=>true} => {"name_s"=>"Python Client", "maintainer_s"=>Basho", "popular_b"=>true} >> py.store 28 A Simple Search Using Apache Solr