DataStax Developer Blog

An Introduction to the C# Driver for Cassandra

By Bill Bucher -  January 6, 2014 | 8 Comments

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.

Getting Started

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

nuget_pkg_mgr_scn_ds

vs_refs_with_ds_pkgs

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:

ex_connect_and_simple_execute

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:

ex_create_ks_and_cf_1

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.

replication_dict_for_ks_create

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:

ex_simple_insert

ex_simple_query_method

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.

ex_prepared_statement1

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:

ex_batch_insert1

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:

blog_post_context

ex_LINQ_1



Comments

  1. Andor Molnár says:

    Hi Bill,

    Could you please elaborate a bit more on the batch statement?
    How does the BlogPost class look like?
    What does the GetTable() method do?

    Thanks,
    -Andor

  2. Matteo says:

    Could you please elaborate a bit more on the batch statement?
    Thank you =)

  3. Jaan says:

    Um.. for some reason I don’t see “GetTable” method. Should I use entity framework?

    1. Rob says:

      Ran into this too, try adding:
      using Cassandra.Data.Linq;

  4. Jon Davis says:

    I might suggest you strip out the sample that does not use parameterization with SQL .. http://www.datastax.com/wp-content/uploads/2014/01/ex_simple_insert.jpg .. The casual observer will see that first and may not know better than to not take the time to find how to use parameterized quer– ……
    …. wait, if someone doesn’t know how to insist on looking for how to add parameterized queries they shouldn’t be developing against Cassandra ..

  5. Jon Davis says:

    (^^ use only parameterized queries so that doing a mere string.format or string concat does not result in runtime errors or SQL injection attacks as when single quotes or ‘%’ is passed in …)

  6. aKIWAGA says:

    How do we get these Casssandra Data to a Gridview in VB.net

  7. Jason says:

    Thanks for the example. I’m betting that 99% of the people looking for an example aren’t writing a console program though. Something I’ve had a hard time seeing, is how you’d manage cluster object and session object in a web application scenario. I’ve looked at the video example and it’s far too complex for an example. Specific questions of mine are how to use Cluster and Session (new per request or static?)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>