Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi all,
Can anyone help me out in connecting to cassandra db through my C# web Application as i am new to C# .Net .
Thanks in Advance
Posted
Updated 31-Jan-12 2:30am
v2

 
Share this answer
 
This is the code for connecting to Cassandra Database but u need to provide to Cluster name to which u want to connect

public bool GetConnectionObject(string clusterName)
{
bool isConnected;
ClusterManager.Shutdown();
try
{
XmlConfigurator.Configure();
_cluster = ClusterManager.GetCluster(clusterName);
isConnected = true;
}
catch (Exception ex)
{
throw;
}


return isConnected;

}
 
Share this answer
 
So to get Cassandra up and running in a Virtual Machine (or physical if you choose) you should follow the following steps (valid as at 5 Nov 2010):

Create a virtual machine to host Ubuntu 9.10 x86 Desktop. I use VirtualBox as my VM host on windows so I will refer to this here but feel free to substitute it with you favourite VM software. The VM I created has 384MB allocated to it with an 8GB virtual hard drive using a bridged network adapter (so it can be accessed from the Windows XP host). Once you've gone through the install process install the client tools so that you change the screen resolution, share folders, etc. To do this with virtual box go to Devices > Install Guest Additions and then open up a terminal window and execute

sudo /media/cdrom/VBoxLinuxAdditions-x86.run

Once this is complete you will need to reboot your VM.
Now it is time to install Cassandra/Thrift prerequisites and software that we will to set everything up:

sudo apt-get install libboost-dev automake libtool flex bison pkg-config g++ -y
sudo apt-get install sun-java6-jdk -y
sudo apt-get apt-get install ant -y

Once that is done we can install other useful Ubuntu software that we will use:

sudo apt-get install rapidsvn -y
sudo apt-get install monodevelop -y

I also like to install the following:

sudo apt-get install nautilus-open-terminal -y
sudo apt-get install samba smbfs smbclient winbind -y
sudo apt-get install nautilus-share -y

You may discover that there is an issue with your Java runtime home directory. If that occurs, select Sun's Java Runtime as the default Java version by running the following command from the terminal window and selecting the third option:

sudo update-alternatives --config java

Now open up Nautilus by going to Places > Home Folder. Right click and create a Development folder. Within the Development folder create another two folders: Cassandra and Thrift.
We can now get the source for Cassandra and Thrift. Run RapidSVN by going to Applications > Programming > RapidSVN. Check out the source from SVN by going to the RapidSVN menu option Repository > Checkout. In the URL enter http://svn.apache.org/repos/asf/incubator/thrift/trunk and for the Destination Directory browse to the Thrift development folder. So it should look like this:


Now get the Cassandra code by doing the same thing but entering http://svn.apache.org/repos/asf/incubator/cassandra/trunk as the URL and selecting the Cassandra directory.
Once we have all the code we are ready to compile Cassandra, so let's do that by executing the following commands from the terminal window:

cd ~/Development/Cassandra
ant

Start the Cassandra server by opening a new terminal window and running the following commands:

sudo ~/Development/Cassandra/bin/cassandra -f

The running server will look like the screenshot below. You should take note of the thrift binding address of localhost/127.0.0.1:9160.


Now we want to get communicating with the server, so we will need to compile Thrift so that we can use it to generate our C# client:

cd ~/Development/Thrift
./bootstrap.sh
./configure
make
sudo make install

Now we can finally generate a C# client for the Thrift calls to Cassandra:

cd ~/Development/Cassandra/interface/
~/Development/Thrift/compiler/cpp/thrift -gen csharp cassandra.thrift

This will create the following directories inside of the Cassandra/interface folder: gen-csharp/Apache/Cassandra. These files along with the Thrift.dll will be copied into a new demo project we will create using MonoDevelop in the Development folder. So again in the terminal window execute:

mkdir ~/Development/CassandraDemo
cp ~/Development/Cassandra/interface/gen-csharp ~/Development/CassandraDemo -R
cp ~/Development/Thrift/lib/csharp/Thrift.dll ~/Development/CassandraDemo

Open up MonoDevelop and create a new console application by going to File > New > Solution. Now do the following:
Select Console project
Enter CassandraDemo as the name
For the location browse to the Development folder that we created in your home directory.
Uncheck the 'Create separate Solution directory' check box.
Click the Forward button
Don't make any changes on the next screen and just click Ok.
Right click on the CassandraDemo project in the left tree view and select Add > Add Files.... Navigate to ~/Development/CassandraDemo/gen-csharp/Apache/Cassandra and select all the files in the folder. You will now have a project that looks like this:


Add a reference to the Thrift assembly by right clicking on References and selecting Edit References. Change to the .Net Assembly tab and double click on the Thrift.dll and click OK.
Now we can add the sample code to test it all out. So double click on Main.cs to ensure that is the class in focus in the editor. Replace the contents of the file with this:
namespace CassandraDemo
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;

class Program
{
static void Main(string[] args)
{
TTransport transport = new TSocket("localhost", 9160);
TProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);

Console.WriteLine("Opening connection");
transport.Open();

System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;

long timeStamp = DateTime.Now.Millisecond;
ColumnPath nameColumnPath = new ColumnPath()
{
Column_family = "Standard1",
Column = utf8Encoding.GetBytes("name")
};

Console.WriteLine("Inserting name columns");

//Insert the data into the column 'name'
client.insert("Keyspace1",
"1",
nameColumnPath,
utf8Encoding.GetBytes("Joe Bloggs"),
timeStamp,
ConsistencyLevel.ONE);

client.insert("Keyspace1",
"2",
nameColumnPath,
utf8Encoding.GetBytes("Joe Soap"),
timeStamp,
ConsistencyLevel.ONE);

//Simple single value get using the key to get column 'name'
ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "1", nameColumnPath, ConsistencyLevel.ONE);
Console.WriteLine("Column Data in Keyspace1/Standard1: name: {0}, value: {1}",
utf8Encoding.GetString(returnedColumn.Column.Name),
utf8Encoding.GetString(returnedColumn.Column.Value));

Console.WriteLine("Getting splice range");

//Read an entire row
SlicePredicate predicate = new SlicePredicate()
{
Slice_range = new SliceRange()
{
//Start and Finish cannot be null
Start = new byte[0],
Finish = new byte[0],
Count = 10,
Reversed = false
}
};

ColumnParent parent = new ColumnParent() { Column_family = "Standard1" };
Dictionary<string>> results = client.multiget_slice("Keyspace1",
new List<string>() { "1", "2"},
parent,
predicate,
ConsistencyLevel.ONE);

foreach (KeyValuePair<string,>> resultPair in results)
{
Console.WriteLine("Key: {0}", resultPair.Key);

foreach (ColumnOrSuperColumn resultColumn in resultPair.Value)
{
Column column = resultColumn.Column;
Console.WriteLine("name: {0}, value: {1}", utf8Encoding.GetString(column.Name), utf8Encoding.GetString(column.Value));
}
}

Console.WriteLine("Closing connection");
transport.Close();
}
}
}
Hit F5 and watch the Application Output window. This is what you should see:



Hopefully this is enough to get you going and have you fill in the remaining gaps. If you found it useful please post a comment. If the Cassandra guys would li
 
Share this answer
 
Devices > Install Guest Additions and then open up a terminal window and execute

sudo /media/cdrom/VBoxLinuxAdditions-x86.run

Once this is complete you will need to reboot your VM.
Now it is time to install Cassandra/Thrift prerequisites and software that we will to set everything up:

sudo apt-get install libboost-dev automake libtool flex bison pkg-config g++ -y
sudo apt-get install sun-java6-jdk -y
sudo apt-get apt-get install ant -y

Once that is done we can install other useful Ubuntu software that we will use:

sudo apt-get install rapidsvn -y
sudo apt-get install monodevelop -y

I also like to install the following:

sudo apt-get install nautilus-open-terminal -y
sudo apt-get install samba smbfs smbclient winbind -y
sudo apt-get install nautilus-share -y


sudo update-alternatives --config java

cd ~/Development/Cassandra
ant

Start the Cassandra server by opening a new terminal window and running the following commands:

sudo ~/Development/Cassandra/bin/cassandra -f


cd ~/Development/Thrift
./bootstrap.sh
./configure
make
sudo make install

Now we can finally generate a C# client for the Thrift calls to Cassandra:

cd ~/Development/Cassandra/interface/
~/Development/Thrift/compiler/cpp/thrift -gen csharp cassandra.thrift


mkdir ~/Development/CassandraDemo
cp ~/Development/Cassandra/interface/gen-csharp ~/Development/CassandraDemo -R
cp ~/Development/Thrift/lib/csharp/Thrift.dll ~/Development/CassandraDemo

Open up MonoDevelop and create a new console application by going to File > New > Solution. Now do the following:
Select Console project
Enter CassandraDemo as the name
For the location browse to the Development folder that we created in your home directory.
Uncheck the 'Create separate Solution directory' check box.
Click the Forward button
Don't make any changes on the next screen and just click Ok.
Right click on the CassandraDemo project in the left tree view and select Add > Add Files.... Navigate to ~/Development/CassandraDemo/gen-csharp/Apache/Cassandra and select all the files in the folder. You will now have a project that looks like this:


namespace CassandraDemo
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;

class Program
{
static void Main(string[] args)
{
TTransport transport = new TSocket("localhost", 9160);
TProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);

Console.WriteLine("Opening connection");
transport.Open();

System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;

long timeStamp = DateTime.Now.Millisecond;
ColumnPath nameColumnPath = new ColumnPath()
{
Column_family = "Standard1",
Column = utf8Encoding.GetBytes("name")
};

Console.WriteLine("Inserting name columns");

//Insert the data into the column 'name'
client.insert("Keyspace1",
"1",
nameColumnPath,
utf8Encoding.GetBytes("Joe Bloggs"),
timeStamp,
ConsistencyLevel.ONE);

client.insert("Keyspace1",
"2",
nameColumnPath,
utf8Encoding.GetBytes("Joe Soap"),
timeStamp,
ConsistencyLevel.ONE);

//Simple single value get using the key to get column 'name'
ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "1", nameColumnPath, ConsistencyLevel.ONE);
Console.WriteLine("Column Data in Keyspace1/Standard1: name: {0}, value: {1}",
utf8Encoding.GetString(returnedColumn.Column.Name),
utf8Encoding.GetString(returnedColumn.Column.Value));

Console.WriteLine("Getting splice range");

//Read an entire row
SlicePredicate predicate = new SlicePredicate()
{
Slice_range = new SliceRange()
{
//Start and Finish cannot be null
Start = new byte[0],
Finish = new byte[0],
Count = 10,
Reversed = false
}
};

ColumnParent parent = new ColumnParent() { Column_family = "Standard1" };
Dictionary<string>> results = client.multiget_slice("Keyspace1",
new List<string>() { "1", "2"},
parent,
predicate,
ConsistencyLevel.ONE);

foreach (KeyValuePair<string,>> resultPair in results)
{
Console.WriteLine("Key: {0}", resultPair.Key);

foreach (ColumnOrSuperColumn resultColumn in resultPair.Value)
{
Column column = resultColumn.Column;
Console.WriteLine("name: {0}, value: {1}", utf8Encoding.GetString(column.Name), utf8Encoding.GetString(column.Value));
}
}

Console.WriteLine("Closing connection");
transport.Close();
}
}
}
Hit F5 and watch the Application Output window. This is what you should see:



Hopefully this is enough to get you going and have you fill in the remaining gaps. If you found it useful please post a comment. If the Cassandra guys would li
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900