DatabaseIntroducing Couchbase: NoSQL for Windows

Introducing Couchbase: NoSQL for Windows

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introducing Couchbase: NoSQL for Windows

In the first article in this series, “NoSQL for the MSSQL Soul” you learned enough about NoSQL databases to know at a high level the type of NoSQL database you’re looking for. Many NoSQL databases run either only on *nix systems or if they do run on Windows they aren’t meant to do so in a production environment.

Coming from the Microsoft stack, Couchbase is a key/value database that runs on Windows and has excellent support for the .NET platform. It installs with an MSI installer, runs as a proper windows service and can run very well in a production environment on windows servers.

Installation

Couchbase can be installed from http://www.couchbase.com/download.

A common issue windows developers run into when installing Couchbase to run on their dev laptop is that it will automatically bind to your laptop’s dynamically assigned IP address, then when you try to use the browser based administration tools it won’t work. To correct this, after installing edit C:Program FilesCouchbasebinservice_register.bat and change the “set NS_NAME” line to:

set   NS_NAME=ns_1@127.0.0.1

After making this change, open a command prompt on the same folder and run service_unregister.bat then service_register.bat.

Buckets?

When you launch the browser-based Couchbase tools for the first time, you will notice that instead of having lists of databases containing tables, there are just buckets with no tables at all. A bucket in Couchbase is a collection of key/value pair data.

Your first instinct coming from a relational SQL background might be to create a separate bucket for each type of data. For example, you might create a Customers bucket and an Organizations bucket. Although it is a valid approach, it will prove difficult to manage as you end up with hundreds of buckets with no real benefit to your application. From a design standpoint, the right grain of bucket should be a decision more tied to what grain at which you want to be able to control settings, replicas, and separation of unrelated applications.

Couchbase has two types of buckets, Couchbase and Memcached. Memcached is purely an in-memory database typically used for things like session state. If the server crashes or is restarted, the contents of a Memcached bucket are gone. A Couchbase bucket is a persistent bucket that will be stored to disk.

To get started create a bucket with the default settings to play with called, “ExampleBucket”. Since this is a test bucket on your development laptop, I would recommend setting the memory quota to the smallest size permitted at 100mb. On a production server you would want to set the memory quota as high as possible.

Creating and Storing Data

Download the .NET client libraries from http://www.couchbase.com/develop and create a new web project referencing the DLLs you downloaded. Modify the web.config to include the Couchbase configuration shown below.

<configuration>
   <configSections>
   <section name="couchbase" 
  type="Couchbase.Configuration.CouchbaseClientSection,
  Couchbase"/>
   </configSections>
   <couchbase>
   <servers bucket="default" bucketPassword="">
   <add uri="http://127.0.0.1:8091/pools/default"/>
   </servers>
   </couchbase>
   ...
  </configuration>

Add references to your code file to “Enyim.Caching.Memcached” and “System.Runtime.Serialization.Json”.

To store your first item in Couchbase, you will need to create a CouchbaseClient and store it.

CouchbaseClient client = new CouchbaseClient("ExampleBucket","");

client.Store(StoreMode.Set, "MyFirstKey", "MyFirstValue");
  Console.WriteLine("MyKey:{0}", (string)cm.Get("MyKey"));

At the most base level that is it. Couchbase is now a giant persistent hashtable.

It is worth noting that similar to SqlConnections, there is overhead in creating and connecting with a CouchbaseClient so you will want to implement a mechanism such as a singleton to keep it persistently available and shared.

More Capable Data Types

Although you can store any type of value in Couchbase, Couchbase really starts coming alive when you start using it to store JSON objects. Fortunately, .NET has excellent built-in handling of JSON types. You can declare a POCO (Plain Old CLR Object) with no special markup at all like the one shown below and serialize it to or from JSON using the built in DataContractJsonSerializer.

public
  class Customer
  {
   public string CustomerName { get; set; }
   public DateTime BirthDay { get; set; }
   public CustomerOrder[] OrderHistory { get; set; }
  }
  public class CustomerOrder
  {
   public Guid OrderNumber { get; set; }
   public decimal Total { get; set; }
  }
  public string CustomerToJSON(Customer c) {
   DataContractJsonSerializer ser = new
  DataContractJsonSerializer(typeof(Customer));
   MemoryStream ms = new MemoryStream();
   ser.WriteObject(ms, c);
   ms.Position = 0;
   return new StreamReader(ms).ReadToEnd();
  }
  public Customer CustomerFromJSON(string s)
  {
   DataContractJsonSerializer ser = new
  DataContractJsonSerializer(typeof(Customer));
   return (Customer)ser.ReadObject( new MemoryStream(Encoding.UTF8.GetBytes(s))
  );
  }
  With these objects and utility methods you can save and restore customer
  records from your buckets in Couchbase.

Customer
  customer = new Customer()
  {
   CustomerName = "David Talbot",
   BirthDay = new DateTime(),
   OrderHistory = new CustomerOrder[] {
   new CustomerOrder() {
   OrderNumber = Guid.NewGuid(),
   Total = 500.0m
   },
   new CustomerOrder() {
   OrderNumber = Guid.NewGuid(),
   Total = 221.25m
   }
   }
  };

Guid
  MyCustomerKey = Guid.NewGUID();
  //Connect To Couchbase
  CouchbaseClient client = new
  CouchbaseClient("ExampleBucket","");
  //Store the customer as JSON
  client.Store(StoreMode.Set, MyCustomerKey.ToString(), 
   CustomerToJSON(customer));
  //Load the customer from couchbase
  Customer fromCouchBase = CustomerFromJSON(
   (string)client.Get(MyCustomerKey.ToString()));

Most developers will load and store their data in Couchbase in JSON but you can technically store any kind of string value in Couchbase using any encoding desired.

Conclusion

It is quick and easy to get up and running with Couchbase and start loading and saving objects. The next part of this series covers Map/Reduce, which is the functional equivalent in Couchbase of indexes and aggregations in Microsoft SQL Server.

About the Author:

David Talbot has over 14 years of experience in the software industry specializing in creating rich user interface web applications. He is also the author of Applied ADO.NET and numerous articles on technology.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories