Riak and Node.js: Best
served with HTTP
Philipp Fehre https://github.com/sideshowcoder
What is Riak?
A Key Value store
A Dynamo store!
Distributed
Replicated
Fault tolerant
Scaling
Basically means add more machines
Gains
more storage
more throughput
lower latency
And it is Web friendly
HTTP Interface
Value is irrelevant so you can store JSON
Support for JavaScript functions
When to use Riak
Data Organization
keys identify objects
objects have metadata
objects are grouped into buckets
More Data Features
Links
Secoundary indexes
Timeouts
HTTP
Riak understands simple HTTP
Create some data
curl -v -XPUT http://localhost:8098/riak/test/doc 
-H "Content-Type: application/json" 
-d '{"bar":"baz"}...
Query by key
curl -v http://localhost:8098/riak/test/doc
Everything can be done
via HTTP
like setting properties of
the bucket
curl -v -XPUT -H "Content-Type: application/json" 
-d '{"props":{"n_val":5}}'
or MapReduce
curl -XPOST http://localhost:8098/mapred 
-H 'Content-Type: application/json' 
-d '{ "inputs":"training",
"qu...
Ok but we do JavaScript
... or more like Node
Using Riak from Node
JavaScript all the way
down
if you want it to be!
The Client
var db = require("riak-js").getClient()
Store an Object
var obj = { val: "foo", bar: true }
db.save("foos", "foo_1", obj, {}, function(err) {
if(!err) console.log...
Retrieve an Object
db.get("foos", "foo_1", function(err, data, meta) {
if(!err) {
console.log(data)
console.log(meta)
}
})
Map and Reduce
"MapReduce is a programming model for processing large data
sets with a parallel, distributed algorithm on ...
Riak MapReduce Queries
provide a set of keys
provide a map function
provide a reduce function
Select Keys
db.mapreduce.add("foos")
var key_filter = [["tokenize", "_", 1],
["to_lower"],
["matches", "foo"]]
db.mapreduce.add({ bucket: "foos", key_filters: ...
Map a buildin
db.mapreduce
.add("foos")
.map({ name: 'Riak.mapValueJson', keep: true })
.run()
Map custom function
var func = function(v, _keyData, arg) {
var v = Riak.mapValuesJson(v)[0];
(v.val === "foo") ? return [...
Reduce with custom
var func = function(values) {
return [ values.length ]
}
db.mapreduce...
.reduce(func)
.run()
A little more complex
function(values) {
return values.reduce(function(acc, v) {
if(v.bar) {
var curr = acc["bar"] || 0
ac...
Questions
· Usergroup: www.meetup.com/Riak-Munchen/
· More on the basics: littleriakbook.com
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Riak Intro at Munich Node.js
Upcoming SlideShare
Loading in …5
×

Riak Intro at Munich Node.js

1,143
-1

Published on

Introduction to Riak, riak-js and MapReduce at Munich node.js user group

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

No Downloads
Views
Total Views
1,143
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Riak Intro at Munich Node.js

  1. 1. Riak and Node.js: Best served with HTTP Philipp Fehre https://github.com/sideshowcoder
  2. 2. What is Riak? A Key Value store A Dynamo store!
  3. 3. Distributed
  4. 4. Replicated
  5. 5. Fault tolerant
  6. 6. Scaling Basically means add more machines
  7. 7. Gains more storage more throughput lower latency
  8. 8. And it is Web friendly HTTP Interface Value is irrelevant so you can store JSON Support for JavaScript functions
  9. 9. When to use Riak
  10. 10. Data Organization keys identify objects objects have metadata objects are grouped into buckets
  11. 11. More Data Features Links Secoundary indexes Timeouts
  12. 12. HTTP Riak understands simple HTTP
  13. 13. Create some data curl -v -XPUT http://localhost:8098/riak/test/doc -H "Content-Type: application/json" -d '{"bar":"baz"}'
  14. 14. Query by key curl -v http://localhost:8098/riak/test/doc
  15. 15. Everything can be done via HTTP
  16. 16. like setting properties of the bucket curl -v -XPUT -H "Content-Type: application/json" -d '{"props":{"n_val":5}}'
  17. 17. or MapReduce curl -XPOST http://localhost:8098/mapred -H 'Content-Type: application/json' -d '{ "inputs":"training", "query":[{"map":{"language":"javascript", "source":"function(riakObject) { var val = riakObject.values[0].data.match(/pizza/g); return [[riakObject.key, (val ? val.length : 0)]]; }"}}]}'
  18. 18. Ok but we do JavaScript
  19. 19. ... or more like Node
  20. 20. Using Riak from Node
  21. 21. JavaScript all the way down if you want it to be!
  22. 22. The Client var db = require("riak-js").getClient()
  23. 23. Store an Object var obj = { val: "foo", bar: true } db.save("foos", "foo_1", obj, {}, function(err) { if(!err) console.log("stored") })
  24. 24. Retrieve an Object db.get("foos", "foo_1", function(err, data, meta) { if(!err) { console.log(data) console.log(meta) } })
  25. 25. Map and Reduce "MapReduce is a programming model for processing large data sets with a parallel, distributed algorithm on a cluster" -Wikipedia
  26. 26. Riak MapReduce Queries provide a set of keys provide a map function provide a reduce function
  27. 27. Select Keys db.mapreduce.add("foos")
  28. 28. var key_filter = [["tokenize", "_", 1], ["to_lower"], ["matches", "foo"]] db.mapreduce.add({ bucket: "foos", key_filters: key_filter })
  29. 29. Map a buildin db.mapreduce .add("foos") .map({ name: 'Riak.mapValueJson', keep: true }) .run()
  30. 30. Map custom function var func = function(v, _keyData, arg) { var v = Riak.mapValuesJson(v)[0]; (v.val === "foo") ? return [v] : return []; } db.mapreduce.add("foos").map(func).run()
  31. 31. Reduce with custom var func = function(values) { return [ values.length ] } db.mapreduce... .reduce(func) .run()
  32. 32. A little more complex function(values) { return values.reduce(function(acc, v) { if(v.bar) { var curr = acc["bar"] || 0 acc["bar"] = curr + 1 } return acc; }) }
  33. 33. Questions · Usergroup: www.meetup.com/Riak-Munchen/ · More on the basics: littleriakbook.com

×