Riak Intro at Munich Node.js

42
Riak and Node.js: Best served with HTTP Philipp Fehre https://github.com/sideshowcoder

description

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

Transcript of Riak Intro at Munich Node.js

Page 1: Riak Intro at Munich Node.js

Riak and Node.js: Bestserved with HTTP

Philipp Fehre https://github.com/sideshowcoder

Page 2: Riak Intro at Munich Node.js
Page 3: Riak Intro at Munich Node.js

What is Riak?A Key Value store

A Dynamo store!

Page 4: Riak Intro at Munich Node.js

Distributed

Page 5: Riak Intro at Munich Node.js
Page 6: Riak Intro at Munich Node.js
Page 7: Riak Intro at Munich Node.js

Replicated

Page 8: Riak Intro at Munich Node.js
Page 9: Riak Intro at Munich Node.js

Fault tolerant

Page 10: Riak Intro at Munich Node.js
Page 11: Riak Intro at Munich Node.js
Page 12: Riak Intro at Munich Node.js
Page 13: Riak Intro at Munich Node.js

ScalingBasically means add more machines

Page 14: Riak Intro at Munich Node.js

Gainsmore storage

more throughput

lower latency

Page 15: Riak Intro at Munich Node.js

And it is Web friendlyHTTP Interface

Value is irrelevant so you can store JSON

Support for JavaScript functions

Page 16: Riak Intro at Munich Node.js

When to use Riak

Page 17: Riak Intro at Munich Node.js
Page 18: Riak Intro at Munich Node.js

Data Organizationkeys identify objects

objects have metadata

objects are grouped into buckets

Page 19: Riak Intro at Munich Node.js

More Data FeaturesLinks

Secoundary indexes

Timeouts

Page 20: Riak Intro at Munich Node.js

HTTPRiak understands simple HTTP

Page 21: Riak Intro at Munich Node.js

Create some datacurl -v -XPUT http://localhost:8098/riak/test/doc \ -H "Content-Type: application/json" \ -d '{"bar":"baz"}'

Page 22: Riak Intro at Munich Node.js

Query by keycurl -v http://localhost:8098/riak/test/doc

Page 23: Riak Intro at Munich Node.js

Everything can be donevia HTTP

Page 24: Riak Intro at Munich Node.js

like setting properties ofthe bucket

curl -v -XPUT -H "Content-Type: application/json" \ -d '{"props":{"n_val":5}}'

Page 25: Riak Intro at Munich Node.js

or MapReducecurl -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)]]; }"}}]}'

Page 26: Riak Intro at Munich Node.js

Ok but we do JavaScript

Page 27: Riak Intro at Munich Node.js

... or more like Node

Page 28: Riak Intro at Munich Node.js

Using Riak from Node

Page 29: Riak Intro at Munich Node.js

JavaScript all the waydown

if you want it to be!

Page 30: Riak Intro at Munich Node.js

The Clientvar db = require("riak-js").getClient()

Page 31: Riak Intro at Munich Node.js

Store an Objectvar obj = { val: "foo", bar: true }db.save("foos", "foo_1", obj, {}, function(err) { if(!err) console.log("stored")})

Page 32: Riak Intro at Munich Node.js

Retrieve an Objectdb.get("foos", "foo_1", function(err, data, meta) { if(!err) { console.log(data) console.log(meta) }})

Page 33: Riak Intro at Munich Node.js

Map and Reduce"MapReduce is a programming model for processing large data

sets with a parallel, distributed algorithm on a cluster" --Wikipedia

Page 34: Riak Intro at Munich Node.js
Page 35: Riak Intro at Munich Node.js

Riak MapReduce Queriesprovide a set of keys

provide a map function

provide a reduce function

Page 36: Riak Intro at Munich Node.js

Select Keysdb.mapreduce.add("foos")

Page 37: Riak Intro at Munich Node.js

var key_filter = [["tokenize", "_", 1], ["to_lower"], ["matches", "foo"]]db.mapreduce.add({ bucket: "foos", key_filters: key_filter })

Page 38: Riak Intro at Munich Node.js

Map a buildindb.mapreduce .add("foos") .map({ name: 'Riak.mapValueJson', keep: true }) .run()

Page 39: Riak Intro at Munich Node.js

Map custom functionvar func = function(v, _keyData, arg) { var v = Riak.mapValuesJson(v)[0]; (v.val === "foo") ? return [v] : return [];}db.mapreduce.add("foos").map(func).run()

Page 40: Riak Intro at Munich Node.js

Reduce with customvar func = function(values) { return [ values.length ]}db.mapreduce... .reduce(func) .run()

Page 41: Riak Intro at Munich Node.js

A little more complexfunction(values) { return values.reduce(function(acc, v) { if(v.bar) { var curr = acc["bar"] || 0 acc["bar"] = curr + 1 } return acc; })}

Page 42: Riak Intro at Munich Node.js

Questions· Usergroup: www.meetup.com/Riak-Munchen/

· More on the basics: littleriakbook.com