Topics:

Apache Cassandra
DataStax Enterprise
Drivers
C# / .NET

About this Tutorial

On this page, you'll learn just enough to get started with NoSQL Apache Cassandra and C# (.NET), including how to install, try out some basic commands and what to do next.

Requirements

To follow this tutorial, you should already have a running Cassandra instance, and have gone through the 10 minute walkthrough here: http://planetcassandra.org/create-a-keyspace-and-table/.

You should have the demo keyspace and schema still set up, we will be referring to it below.

Setup

For this demo, we're going to be creating a simple console application.  Open Visual Studio and start a new Console Application project called “GettingStarted”.  You should get an empty Program.cs class that looks something like this:

namespace GettingStarted
{
   class Program
   {
       static void Main(string[] args)
       {
       }
   }
}

To install the .NET driver, we'll use the .NET Driver's NuGet package.  From the Package Manager Console window in Visual Studio:

PM> Install-Package CassandraCSharpDriver

Or if you prefer to manage packages using the UI, you can search for CassandraCSharpDriver and install it that way:

Try it Out

We’ll be putting all our code into the console application’s Main method.

First, create an instance of a cluster, and use that to obtain a session.  A session manages connections to the cluster for you.  In our case, our cluster is just our local machine, but eventually you’ll be connecting to a real production cluster.

// Connect to the demo keyspace on our cluster running at 127.0.0.1
Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
ISession session = cluster.Connect("demo");

You’ll now be connected to the demo keyspace.  Let’s insert a user:

// Insert Bob
session.Execute("insert into users (lastname, age, city, email, firstname) values ('Jones', 35, 'Austin', 'bob@example.com', 'Bob')");

The .NET driver makes it easy to talk to Cassandra.  We can easily pull our user back out:

// Read Bob's information back and print to the console
Row result = session.Execute("select * from users where lastname='Jones'").First();
Console.WriteLine("{0} {1}", result["firstname"], result["age"]);

Now it’s Bob’s birthday, so lets update his age:

// Update Bob's age and then read it back and print to the console
session.Execute("update users set age = 36 where lastname = 'Jones'");
result = session.Execute("select * from users where lastname='Jones'").First();
Console.WriteLine("{0} {1}", result["firstname"], result["age"]);

Lastly, let’s delete Bob and then print out the first name and age of all users in Cassandra:

// Delete Bob, then try to read all users and print them to the console
session.Execute("delete from users where lastname = 'Jones'");

RowSet rows = session.Execute("select * from users");
foreach(Row row in rows)
   Console.WriteLine("{0} {1}", row["firstname"], row["age"]);

To make sure we can see the output from our console application when it runs, we’ll have it wait for the Enter key before exiting:

// Wait for enter key before exiting
Console.ReadLine();

Press F5 to run the console application from Visual Studio.  The output should look like something like this:

Bob 35
Bob 36

You’ll notice that Bob’s information no longer comes back after being deleted (others might, if you have inserted users previously).

CQL is very similar to SQL, in many cases the same syntax will work.  This makes querying for data very straightforward if you have a background with relational databases.

You have just managed to connect to a Cassandra cluster and perform queries against a live (local) database. Hopefully this demonstrates just how easy it is to use Cassandra using the .NET driver.  A Gist of the complete console application for this sample is available on GitHub.

More Resources

• Read the documentation for the .NET driver.

• Get more examples of using the .NET driver in the KillrVideo C# sample web application.

• Video/Slides: Getting Started with the Apache Cassandra C# (.NET) driver by Luke Tillman

• Read up more on CQL.