C# Driver 1.0 for Apache Cassandra

Asynchronous I/O

You can execute statements on a session objects in two different ways. Calling Execute blocks the calling thread until the statement finishes executing, but a session also allows for asynchronous and non-blocking I/O by calling the BeginExecute and EndExecute methods.

Modify the functionality of the SimpleClient class by extending it and execute queries asynchronously on a cluster.

Procedure

  1. Add a new class, AsynchronousClient, to your CassandraApplication project. It should extend the SimpleClient class.
    namespace CassandraApplication : SimpleClient
    {
        class AsynchronousClient
        {
        }
    }
    
  2. Add an instance method, GetRowsAsynchronously, and implement it.
    1. Implement the GetRowsAsynchronously method so it returns a IAsyncResult object.
      public IAsyncResult GetRowsAsynchronously() {}
      The System.IAsyncResult interface is implemented by classes containing methods that can operate asynchronously and allow for non-blocking computation. The calling code may wait for the completion of the computation or to check if it is done.
    2. Using the SimpleStatement class, build a SELECT query that returns all the rows for the song table for all columns.
      Statement statement = new SimpleStatement("SELECT * FROM simplex.songs;");
    3. Execute the query asynchronously, by calling the BeginExecute method, and return the IAsyncResult object.
      return Session.BeginExecute(statement);
  3. Add a class method, main, to your class implementation and add calls to create the schema, load the data, and then query it using the getRows method.
    public static void main(String[] args) {
        AsynchronousClient client = new AsynchronousClient();
        client.Connect("127.0.0.1");
        client.CreateSchema();
        client.LoadData();
        IAsyncResult asyncResult = client.GetRowsAsynchronously("SELECT * FROM simplex.songs;");
        // at this point you can do other things while waiting for the query to finish
        asyncResult.AsyncWaitHandle.WaitOne();
        CqlRowSet results = client.Session.EndExecute(asyncResult);
        client.PrintResults(results);
        client.DropSchema("simplex");
        client.Close();
    }
    

AsynchronousExample code listing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Cassandra;

namespace CassandraApplication
{
    class AsynchronousClient : SimpleClient
    {
        public AsynchronousClient() { }

        public IAsyncResult GetRowsAsynchronously(String query)
        {
            Statement statement = new SimpleStatement(query);
            return Session.BeginExecute(statement, null, null);
        }

        public void PrintResults(CqlRowSet results)
        {
            Console.WriteLine(String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3, -30}",
                "title", "album", "artist", "tags"));
            Console.WriteLine("-------------------------------+-----------------------+--------------------+-------------------------------");
            foreach (CqlRow row in results.GetRows())
            {
                Console.WriteLine(String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3}",
                    row.GetValue<String>("title"), row.GetValue<String>("album"),
                    row.GetValue<String>("artist"), Prettify( row.GetValue<List<String>>("tags")
                    ) ));
            }
        }
    }

    public static void main(String[] args) {
        AsynchronousClient client = new AsynchronousClient();
        client.Connect("127.0.0.1");
        client.CreateSchema();
        client.LoadData();
        IAsyncResult asyncResult = client.GetRowsAsynchronously("SELECT * FROM simplex.songs;");
        // at this point you can do other things while waiting for the query to finish
        asyncResult.AsyncWaitHandle.WaitOne();
        CqlRowSet results = client.Session.EndExecute(asyncResult);
        client.PrintResults(results);
        client.DropSchema("simplex");
        client.Close();
    }
}