Couchbase
  • Why NoSQL
  • Couchbase Server
  • Download
  • Develop
  • Deploy
  • Get Help
  • Community
  • Documentation
  • Resource Library
Home | Couchbase Blog | Rags Srinivas's blog

Hello World with Couchbase and Ruby

ShareThis

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

A longer Tutorial example of using Ruby and Couchbase

 

Posted by Rags Srinivas on January 23, 2012 | ShareThis

Comments

Rags Srinivas.

 

Raghavan "Rags" Srinivas is 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.

« Back to Rags Srinivas's Blog
« Back to Blogs

Tags

Apache CouchDB cloud couchbase Couchbase Mobile Couchbase Server CouchDB document database erlang events GeoCouch libcouchbase membase membase nosql Membase Server memcached Memcached Server moxi mysql nosql NoSQL databases open source storage engine tap interface windows
more tags

Product

  • Couchbase Server 1.8
  • Couchbase Server 2.0
  • Memcached and Couchbase Server
  • Looking for Membase?

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Careers
  • Blog

News & Events

  • Press Releases
  • Buzz
  • Events
  • CouchConf

Community

  • Forums
  • Wiki
  • Contribute
  • Gear

Engage with Us

  • Subscriptions and Support
  • Training and Services
  • Contact Us
  • Subscribe to our Newsletter
  • Buy Now

Follow Us

 

  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Contact

Copyright Couchbase 2011