For those of you who might have seen my other blog, my goal is to convey the fact that using Couchbase with any of the language client libraries is very straightforward. The client libraries handle the complexity of the connection and the inherent distributed nature of the cluster. Here’s the Hello World code in Ruby.

require ‘rubygems’
require ‘couchbase’
client = Couchbase.new “http://127.0.0.1:8091/pools/default, :quiet=> false”
client.quiet = false
begin
spoon = client.get “spoon”
puts spoon
rescue Couchbase::Error::NotFound => e
puts “There is no spoon.”
client.set “spoon”, “Hello World!”, :ttl => 10
end
The idea behind this program is to get the value of a key named spoon and if it does not exist to create one that lives for 10 seconds. The :quiet as false and :ttl as 10 secs. properties allow for raising of an error and for the key to live 10 seconds respectively.
With Ruby and the abundance of Ruby gems, it’s possible to easily store and manipulate JSON documents . Here’s an example of Beer data in the following form (it’s been massaged a little bit) from the openbeerdb.
{“_id”:”beer_#42_Cream_Ale”,”_rev”:”1-dbd2b5d711fea235a714146a5cdae6c7″,”brewery”:”Listermann Brewing Company”,”name”:”#42 Cream Ale”,”category”:”Other Style”,”style”:”American-Style Cream Ale or Lager”,”updated”:”2010-07-22 20:00:20″},

A Ruby program to store the data would look something like below. We use the key “_id” in the document to store the details of all the beers on the world in Couchbase. A very simple program that parses the file and stores each entry based on the key.

require ‘rubygems’
require ‘couchbase’
require ‘yajl’couchbase = Couchbase.new(‘http://127.0.0.1:8091/pools/default’)
beers = Yajl::Parser.parse(File.read(‘beerdb’))
beers.each do |beer|
couchbase.set(beer[‘_id’], beer)
end

I have gone way beyond a simple Hello World. I will be contributing more towards using Ruby Client libraries and Couchbase, but, in the meantime here are some useful links.

Downloading and Installing the Couchbase server

Author

Posted by Raghavan Srinivas, Developer Advocate, Couchbase

Raghavan "Rags" Srinivas was a Developer Advocate at Couchbase getting his hands dirty with emerging technology directions and trends. His general focus area is in distributed systems, with a specialization in cloud computing. He worked on Hadoop and HBase during its early stages. He has spoken on a variety of technical topics at conferences around the world, conducted and organized Hands-on Labs and taught graduate classes in the evening. Rags brings with him about 20 years of hands-on software development and about 10 years of architecture and technology evangelism experience. He worked for Digital Equipment Corporation, Sun Microsystems, Intuit and Accenture. He has worked on several technology areas, including internals of VMS, Unix and NT to Hadoop and HBase. He has evangelized and influenced the architecture of a number of technology areas including the early releases of JavaFX, Java, Java EE, Java and XML, Java ME, AJAX and Web 2.0, Java Security and so on. Rags holds a Masters degree in Computer Science from the Center of Advanced Computer Studies at the University of Louisiana at Lafayette.

One Comment

  1. […] Zablocki introduces the new .NET SDK, while Rags Srinivas covers the updates in both the Java and Ruby SDKs.  Our own Jan Lehnardt also gives a quick rundown on the PHP SDK.  PHP and Ruby get their […]

Leave a reply