Skip to content

Instantly share code, notes, and snippets.

@clr
Created October 24, 2012 06:29
Show Gist options
  • Select an option

  • Save clr/3944385 to your computer and use it in GitHub Desktop.

Select an option

Save clr/3944385 to your computer and use it in GitHub Desktop.
Application Development on Riak: Getting Started

The CRUD model is a well-known pattern in application development. In the CRUD lingo, you can Create, Read, Update, or Delete your data object. This lines up well with the Resource Access Pattern, with which many are familiar as a component of a RESTful API.

The Resource Access Pattern is very popular because of its simplicity. Writing a Resource Access Pattern on top of Riak is really simple too. Riak uses a RESTful HTTP syntax, and maps the operations to CRUD like so:

CRUD Verb HTTP Verb URL Description
Create PUT /buckets/resource-type/keys/key-name Make a new resource at the given key and store the submitted data.
POST /buckets/resource-type Make a new resource and get the unique key generated for it.
Read GET /buckets/resource-type/keys/key-name Get the resource data back for the given key.
Update PUT /buckets/resource-type/keys/key-name Store the submitted data for the given key.
Delete DELETE /buckets/resource-type/keys/key-name Delete this resource.

This is the simplest way of using Riak, and it is the quintessential use of a key-value store. This is what Riak does best, and if you can structure your application in such a way that all of your interactions with the storage layer utilize the Resource Access Pattern, then you will go very far with Riak. :-)

Example

So many applications have Users, and in so many of those applications. Users are often a read-heavy piece of data. We create a User once, and then we repeatedly look up that User during the course of interacting with her or him. The resource type is obvious here, but we want the key name to be meaningful as well. A unique identifier is better if there is some logical significance to the key that will help you to retrieve it later. Let us assume that we will look users up by their email address.

Our data can be whatever we want to store about the user. We could store a marshaled object, a bit of text, a JSON object -- to Riak it is all just opaque binary with a mimetype attached. To keep things simple, let's format our data in JSON.

Our Riak object will look like:

KEY: casey@basho.com
DATA: {'name':'Casey Rosenthal','company':'Basho'}

Command Line

We can CRUD this data with Riak right from the command line! Get your Riak cluster up and running [LINK] and let's add a User!

Create, Update

curl -X PUT http://127.0.0.1:8091/buckets/users/keys/casey@basho.com -H 'Content-Type: application/json' -d "{'name':'Casey Rosenthal','company':'Basho'}"

Read

curl -X GET http://127.0.0.1:8091/buckets/users/keys/casey@basho.com

Delete

curl -X DELETE http://127.0.0.1:8091/buckets/users/keys/casey@basho.com

Node.js

Get your Riak cluster up and running [LINK] and let's add a User! Make sure you have Node.js installed [LINK] and we will be using this library to interact with Riak: https://github.com/basho-labs/riak-j See the tests, particularly http-client-test.js, for more examples.

Get Node.js warmed up and connect to Riak: npm install riak-js node db = require('riak-js').getClient({port:8091, debug:false})

Create, Update

db.save('users', 'casey@basho.com', {'name':'Casey Rosenthal','company':'Basho'})

Read

db.get('users','casey@basho.com')

Delete

db.remove('users','casey@basho.com')

Ruby

Get your Riak cluster up and running [LINK] and let's add a User! Make sure you have Ruby installed [LINK] and we will be using this library to interact with Riak: https://github.com/basho/riak-ruby-client

Get Ruby warmed up and connect to Riak: gem install riak-client irb require ‘riak’ client = Riak::Client.new(:host => '127.0.0.1', :http_port => 8091) bucket = client.bucket('users')

Create

object = bucket.new('casey@basho.com')
object.data = {:name => 'Casey Rosenthal', :company => 'Basho'}
object.store

Read

object = bucket.get('casey@basho.com')

Update

object.data = {:name => 'CLR', :company => 'Basho'}
object.store

Delete

object.delete

See ripple for examples of document modeling with Ruby [LINK]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment