Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Riak with node.js

13,721 views

Published on

Learn how to use Riak with node.js applications.

Published in: Technology
  • Be the first to comment

Riak with node.js

  1. 1. Riak with node.js Sean Cribbs Mathias Meyer Developer Advocatesbasho
  2. 2. Upcoming Events Mark Phillips, Community Manager mark@basho.com @pharkmillupsbasho
  3. 3. Why Riak with node.js?basho
  4. 4. Why Riak with node.js? • Simple to understand and usebasho
  5. 5. Why Riak with node.js? • Simple to understand and use • Flexible storage optionsbasho
  6. 6. Why Riak with node.js? • Simple to understand and use • Flexible storage options • Awesome client library (riak-js)basho
  7. 7. Why Riak with node.js? • Simple to understand and use • Flexible storage options • Awesome client library (riak-js) • Fits lots of use-casesbasho
  8. 8. Getting Startedbasho
  9. 9. Getting Started Install Riak or Riak Search: http://downloads.basho.combasho
  10. 10. Getting Started Install Riak or Riak Search: http://downloads.basho.com $ npm install riak-jsbasho
  11. 11. Getting Started Install Riak or Riak Search: http://downloads.basho.com $ npm install riak-js $ git clone git://github.com/frank06/riak-jsbasho
  12. 12. Getting Started Install Riak or Riak Search: http://downloads.basho.com $ npm install riak-js $ git clone git://github.com/frank06/riak-js Website: http://riakjs.orgbasho
  13. 13. Basic Client Operationsbasho
  14. 14. Basic Operationsbasho
  15. 15. Basic Operations // Get a client var db = require(riak-js).getClient()basho
  16. 16. Basic Operations // Get a client var db = require(riak-js).getClient() // Fetch an object db.get(webinars, node.js,   function(err, data) {     console.log("Found the webinar:" + sys.inspect(data))   })basho
  17. 17. Basic Operations // Get a client var db = require(riak-js).getClient() // Fetch an object typical node db.get(webinars, node.js, callback style   function(err, data) {     console.log("Found the webinar:" + sys.inspect(data))   })basho
  18. 18. Basic Operations // Get a client var db = require(riak-js).getClient() // Fetch an object typical node db.get(webinars, node.js, callback style   function(err, data) {     console.log("Found the webinar:" + sys.inspect(data))   }) // Store an object db.save(webinars, node.js,         {date:"2011-03-15",          title: "Riak with node.js",          presenters: ["Sean", "Mathias"]})basho
  19. 19. Basic Operationsbasho
  20. 20. Basic Operations // Delete an object db.remove(webinars, node.js)basho
  21. 21. Basic Operations // Delete an object db.remove(webinars, node.js) // Does this key exist? db.exists(frameworks, node.js)basho
  22. 22. Basic Operations // Delete an object db.remove(webinars, node.js) // Does this key exist? db.exists(frameworks, node.js) // Give me them all db.getAll(frameworks)basho
  23. 23. Basic Operations // Delete an object db.remove(webinars, node.js) // Does this key exist? db.exists(frameworks, node.js) // Give me them all db.getAll(frameworks) // But filter them too (uses MapReduce) db.getAll(frameworks, { where: { language: "javascript" }})basho
  24. 24. Basic Operations // Delete an object db.remove(webinars, node.js) // Does this key exist? db.exists(frameworks, node.js) // Give me them all db.getAll(frameworks) // But filter them too (uses MapReduce) db.getAll(frameworks, { where: { language: "javascript" }}) // How many are there? db.count(frameworks)basho
  25. 25. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) function cb(err, data, META){ }basho
  26. 26. Meta Object db.save(‘bucket’, ‘key’, data, META, cb)basho
  27. 27. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) • Request-time optionsbasho
  28. 28. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) • Request-time options •contentTypebasho
  29. 29. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) • Request-time options •contentType •clientIdbasho
  30. 30. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) • Request-time options •contentType •clientId • links, usermetabasho
  31. 31. Meta Object db.save(‘bucket’, ‘key’, data, META, cb) • Request-time options •contentType •clientId • links, usermeta • quorums (r,w,dw), returnBodybasho
  32. 32. Meta Object function cb(err, data, META){ }basho
  33. 33. Meta Object function cb(err, data, META){ } • Response-time metadatabasho
  34. 34. Meta Object function cb(err, data, META){ } • Response-time metadata • bucket, key, vclockbasho
  35. 35. Meta Object function cb(err, data, META){ } • Response-time metadata • bucket, key, vclock • links, usermetabasho
  36. 36. Meta Object function cb(err, data, META){ } • Response-time metadata • bucket, key, vclock • links, usermeta • lastMod, etag, statusCodebasho
  37. 37. Using Metabasho
  38. 38. Using Meta // Add a link to the object db.get(frameworks, node.js,   function(err, data, meta){     meta.addLink({bucket:vm, key:v8, tag:vm});     db.save(frameworks, node.js, data, meta)   })basho
  39. 39. Using Meta // Add a link to the object db.get(frameworks, node.js,   function(err, data, meta){     meta.addLink({bucket:vm, key:v8, tag:vm});     db.save(frameworks, node.js, data, meta)   }) // Set the content type db.save(pages, index.html,         <html><body>Hello, world!</body></html>,         {contentType: html})basho
  40. 40. Querying Beyond Key-Valuebasho
  41. 41. Link-walkingbasho
  42. 42. Link-walking // Find friends db.walk(people, sean, [[_,friend]])basho
  43. 43. Link-walking // Find friends db.walk(people, sean, [[_,friend]]) // Find acquaintances of friends db.walk(people, mathias,         [[_,friend],          [people,_]])basho
  44. 44. MapReducebasho
  45. 45. MapReduce // Execute a MapReduce query db.add(frameworks) // Use all keys in bucket   .map(Riak.mapValuesJson) // Map using a built-in   .run(function(err, data){ // Execute the query          console.log(data);        })basho
  46. 46. MapReduce // Execute a MapReduce query db.add(frameworks) // Use all keys in bucket   .map(Riak.mapValuesJson) // Map using a built-in   .run(function(err, data){ // Execute the query          console.log(data);        }) // Send a focused query db.add(frameworks, node.js) // Start with one key   .link({ bucket: "vm"}) // Follow some links   .map(function(obj){ // Map using an inline          var data =             Riak.mapValuesJson(obj)[0];          return [data.name]        })   .reduce(Riak.reduceSort) // Sort via reduce   .run()basho
  47. 47. MapReducebasho
  48. 48. MapReduce // Initiate MapReduce with a key-filter db.add({ bucket: emails,          key_filters: [["ends_with", "basho.com"]] }) // Map with a phase-argument   .map({ name:Riak.mapByFields,          arg: { joined:2010-01-01 })   .run()basho
  49. 49. Riak Searchbasho
  50. 50. Riak Search // Turn on automatic document indexing db.enableIndex(documents)basho
  51. 51. Riak Search // Turn on automatic document indexing db.enableIndex(documents) // Perform a simple search db.search(documents, title:scalable)basho
  52. 52. Riak Search // Turn on automatic document indexing db.enableIndex(documents) // Perform a simple search db.search(documents, title:scalable) // Start a MapReduce query with search db.addSearch(documents, title:scalable) // From here, add map and reduce phasesbasho
  53. 53. Running Testsbasho
  54. 54. Test Serverbasho
  55. 55. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServerbasho
  56. 56. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServer var testServer = new TestServer({binDir: "/usr/local/bin"})basho
  57. 57. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServer var testServer = new TestServer({binDir: "/usr/local/bin"}) var vows = require(vows), assert = require(assert)basho
  58. 58. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServer var testServer = new TestServer({binDir: "/usr/local/bin"}) var vows = require(vows), assert = require(assert) suite = vows.describe("Example tests")basho
  59. 59. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServer var testServer = new TestServer({binDir: "/usr/local/bin"}) var vows = require(vows), assert = require(assert) suite = vows.describe("Example tests") // Bootstrap the test server suite.addBatch({   setup:{     topic: function(){       var cb = this.callback       testServer.prepare(function(){ testServer.start(cb) })     },basho
  60. 60. Test Server // Add test server to your tests var TestServer = require(riak-js).TestServer var testServer = new TestServer({binDir: "/usr/local/bin"}) var vows = require(vows), assert = require(assert) suite = vows.describe("Example tests") // Bootstrap the test server suite.addBatch({   setup:{     topic: function(){       var cb = this.callback       testServer.prepare(function(){ testServer.start(cb) })     },    works: function(){      assert.isTrue(testServer.started)    } }})basho
  61. 61. Test Serverbasho
  62. 62. Test Server // Clear data before a test suite.addBatch({   stores data:{      topic: function(){        var cb = this.callback        testServer.clear(function(){          db.save(companies, basho, {city:"Cambridge"}, cb)        })      },      successfully: function(err, data, meta){ }   }})basho
  63. 63. Test Server // Clear data before a test suite.addBatch({   stores data:{      topic: function(){        var cb = this.callback        testServer.clear(function(){          db.save(companies, basho, {city:"Cambridge"}, cb)        })      },      successfully: function(err, data, meta){ }   }}) // Shutdown the server at the end suite.addBatch({   teardown:{     topic: function() {       testServer.stop(this.callback)     },     works:function(){       assert.ok(!testServer.started)     }   }})basho
  64. 64. Demo & Code https://github.com/basho/riaktantbasho
  65. 65. Hosted on Joyent http://no.de Demo & Code https://github.com/basho/riaktantbasho
  66. 66. Plug Interested in learning about support, consulting, or Enterprise features? Email info@basho.com or go to http://www.basho.com/ contact.php to talk with us. www.basho.combasho
  67. 67. Plug Interested in learning about support, consulting, or Enterprise features? Email info@basho.com or go to http://www.basho.com/ contact.php to talk with us. www.basho.com mathias@basho.com @roidrage sean@basho.com @seancribbsbasho
  68. 68. Questions?basho

×