layer:11

an addiction to or an obsession with acquiring, manipulating, and sharing information

Connecting to Cassandra with C# and Thrift

Here’s something I wanted to write down for a long time now, because every time I have to do this (when Cassandra changes its interface), I have already forgotten what and exactly how needs to be done. Let’s assume that we have Cassandra up and running, because most distributions should have a package ready to use, or at least a build script. And now we want to connect to it with C#. What we need is two C# libraries, Thrift and Cassandra. So, let’s build them from source.

What exactly is Thrift? It’s a framework for building services and clients for those services - you write a file that describes what you want, then use the Thrift compiler to get a server skeleton and a RPC client in any language that Thrift supports.

First thing we need is a Thrift compiler and a Thrift library for C#. Let’s download the Thrift source and simply ./configure && make it, no need to install. You might want to disable support for other languages, they don’t get in the way, but I had problems building a Ruby library from Thrift 0.7.0. Now we have a C# Thrift library (lib/csharp/Thrift.dll) and a Thrift compiler (compiler/cpp/thrift). This library doesn’t give us much by itself, but it’s required for the Cassandra library, that we’ll generate with the Thrift compiler.

Let’s get the Thrift definition file, that describes how Cassandra works, it’s interface/cassandra.thrift from the Cassandra source. Now we can use the Thrift compiler, to generate C# source files for Cassandra client: ./thrift -gen csharp cassandra.thrift. Output is in gen-csharp/Apache/Cassandra, just cd there and build it all: dmcs -r:Thrift -t:library -out:Apache.Cassandra.dll *.cs. Don’t forget to copy Thrift.dll there, or add the appropriate -lib:.

That should be it, we have all we need - a Thrift library (Thrift.dll) and a Cassandra client library (Apache.Cassandra.dll).

| Archive | RSS | E-mail | Twitter | Alvis Mikovs