New Survey: Leveraging real-time data delivers higher revenue growth and increased developer productivity. Learn more.
The C# driver allows .Net developers to write applications that utilize Cassandra. The driver works exclusively with CQL3. The driver separates functionality between lower level API calls, such as connecting to a data store and executing queries and a LINQ2CQL layer, which is a LINQ driver.
Creating a new project that utilizes the drivers is simple. Add a project like normal, and add a reference to the driver. The packages are available through NuGet, making adding the package references even easier
One of the first things you will want to do in your project is connect to the Cassandra cluster. This is straightforward. You simply create a cluster object, and call it's Conntect() method:
A Session object is returned from Connect(). This Session instance will be used to perform all operations against the Cassandra cluster. For example, let's say you want to add a Keyspace and a ColumnFamily. This would look something like this:
The replication Dictionary in the above example contains all of the keyspace settings you want to specify at the time of creation. Leaving this off sets up a keyspace with default settings.
Querying and Inserting Data
There are many ways to work with data using the C# driver. I'll briefly cover a few of them here. The most basic way to work with data is using inline CQL statements, and passing them to the to the Session's Execute() method, similar to the following:
In a similar fashion, you could craft a CQL SELECT statement to get data out of Cassandra.
Prepared Statements are another option for working with data. As you will see in the next example, values are not specified at the time of Statement creation, but rather, at the time of execution.
The C# driver also exposes a batch job class for inserting many columns of data at a time. The following is an example of a running a batch job in single threaded mode:
The LINQ2CQL Driver
The LINQ driver can be found in the Cassandra.Data.Linq namespace. It's rather extensive, and deserves a dedicated blog post to fully describe it's capabilities. In the meantime, see the following for an example application that uses the LINQ driver. The following is a simple example to help you get up and running with LINQ2CQL: