C# Driver 1.0 for Apache Cassandra

Connecting to a Cassandra cluster

The C# driver provides a Cluster class which is your client application's entry point for connecting to a Cassandra cluster and retrieving metadata.

Prerequisites

This tutorial assumes you have the following software installed, configured, and that you have familiarized yourself with them:

Using a Cluster object, the client connects to a node in your cluster and then retrieves metadata about the cluster and prints it out.

Procedure

  1. In the Visual Studio IDE, create a console application project. In the New Project dialog, use the following data:
    • template: Installed > Visual C# > Console Application
    • .NET framework: 4.5
    • Name: CassandraClientApplication
    • Location: <your-projects-directory>
    • Solution: Create new solution
    • Solution name: SimpleClient
  2. In the Package Manager Console window install the C# driver.
    1. Select Tools > Library Package Manager > Package Manager Console. The Package Manager Console opens at the bottom of the IDE.
    2. Type the following to install the C# driver: Install-Package CassandraCSharpDriver
    The C# driver is installed.
  3. Create a new C# class, CassandraClientApplication.SimpleClient.
    1. Right-click on the CassandraClientApplication node in the Solution Explorer and select Add > New Item.
    2. In the Add New Item dialog, add the following for your new class:
      • Select Installed > Visual C# Items > Class.
      • Name: SimpleClient.cs
    3. Select Add.
  4. Create a new C# class, .SimpleClient.
    1. Add a using directive that references the Cassandra namespace.
    2. Add a read-only property, Cluster, to hold a Cluster reference.
      private Cluster _cluster;
      
      public Cluster { get { return _cluster; } }
    3. Add an instance method member, Connect, to your new class.
      public void Connect(String node) {}
      The connect method:
      • adds a contact point (node IP address) using the Cluster.Build auxiliary class
      • builds a cluster instance
      • retrieves metadata from the cluster:
        • the name of the cluster
        • the datacenter, host name or IP address, and rack for each of the nodes in the cluster
      public void Connect(String node)
      {
          cluster = Cluster.Builder()
              .AddContactPoint(node).Build();
          Metadata metadata = cluster.Metadata;
          Console.WriteLine("Connected to cluster: " 
              + metadata.ClusterName.ToString());
      }
      
    4. Add an instance method member, Close, to shut down the cluster instance once you are finished with it.
      public void Close()
      {
          cluster.Shutdown();
      }
      
    5. In the class main method instantiate a SimpleClient object, call Connect on it, and then Close.
      public static void main(String[] args) 
      {
         SimpleClient client = new SimpleClient();
         client.Connect("127.0.0.1");
         client.Close();
      }
      

Code listing

The complete code listing illustrates:
  • connecting to a cluster
  • retrieving metadata and printing it out
  • closing the connection to the cluster
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Cassandra;

namespace CassandraApplication
{
    public class SimpleClient
    {
        private Cluster _cluster;

        public Cluster Cluster { get { return _cluster; } }

        public void Connect(String node)
        {
            _cluster = Cluster.Builder()
                .AddContactPoint(node).Build();
            Metadata metadata = _cluster.Metadata;
            /*
            Console.WriteLine("Connected to cluster: "
                + metadata.ClusterName.ToString());             
        }

        public void Close()
        {
            _cluster.Shutdown();
        }

        public static void main(String[] args) 
        {
            SimpleClient client = new SimpleClient();
            client.Connect("127.0.0.1");
            client.Close();
        }
    }
}

When run the client program prints out this metadata on the cluster's constituent nodes in the console pane:

Connected to cluster: xerxes
Datatacenter: datacenter1; Host: /127.0.0.1; Rack: rack1
Datatacenter: datacenter1; Host: /127.0.0.2; Rack: rack1
Datatacenter: datacenter1; Host: /127.0.0.3; Rack: rack1