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. :-)
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'}
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!
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'}"
curl -X GET http://127.0.0.1:8091/buckets/users/keys/casey@basho.com
curl -X DELETE http://127.0.0.1:8091/buckets/users/keys/casey@basho.com
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})
db.save('users', 'casey@basho.com', {'name':'Casey Rosenthal','company':'Basho'})
db.get('users','casey@basho.com')
db.remove('users','casey@basho.com')
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')
object = bucket.new('casey@basho.com')
object.data = {:name => 'Casey Rosenthal', :company => 'Basho'}
object.store
object = bucket.get('casey@basho.com')
object.data = {:name => 'CLR', :company => 'Basho'}
object.store
object.delete
See ripple for examples of document modeling with Ruby [LINK]