Riak Intro at Munich Node.js

Post on 08-May-2015

1.029 views 1 download

description

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

Transcript of Riak Intro at Munich Node.js

Riak and Node.js: Bestserved with HTTP

Philipp Fehre https://github.com/sideshowcoder

What is Riak?A Key Value store

A Dynamo store!

Distributed

Replicated

Fault tolerant

ScalingBasically means add more machines

Gainsmore storage

more throughput

lower latency

And it is Web friendlyHTTP Interface

Value is irrelevant so you can store JSON

Support for JavaScript functions

When to use Riak

Data Organizationkeys identify objects

objects have metadata

objects are grouped into buckets

More Data FeaturesLinks

Secoundary indexes

Timeouts

HTTPRiak understands simple HTTP

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

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

Everything can be donevia HTTP

like setting properties ofthe bucket

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

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

Ok but we do JavaScript

... or more like Node

Using Riak from Node

JavaScript all the waydown

if you want it to be!

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

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

Retrieve an Objectdb.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 a cluster" --Wikipedia

Riak MapReduce Queriesprovide a set of keys

provide a map function

provide a reduce function

Select Keysdb.mapreduce.add("foos")

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

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

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()

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

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; })}

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

· More on the basics: littleriakbook.com