通过我的C#Web应用程序连接到cassandra DB [英] Connection to cassandra DB through my C# web Application

查看:123
本文介绍了通过我的C#Web应用程序连接到cassandra DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

任何人都可以通过我的C#web应用程序帮助我连接到cassandra数据库,因为我是C#.Net的新手。

先谢谢你

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

推荐答案

您好,



尝试检查以下链接:

HTTP://www.ridgway。 co.za/archive/2009/11/06/net-developers-guide-to-getting-started-with-cassandra.aspx [ ^ ]



问候

Robert
Hi,

try to check following link:
http://www.ridgway.co.za/archive/2009/11/06/net-developers-guide-to-getting-started-with-cassandra.aspx[^]

Regards
Robert


这是连接到Cassandra数据库的代码,但你需要提供到群集名称你想要连接



public bool GetConnectionObject(string clusterName)

{

bool isConnected;

ClusterManager.Shutdown();

尝试

{

XmlConfigurator.Configure();

_cluster = ClusterManager。 GetCluster(clusterName);

isConnected = true;

}

catch(exception ex)

{

throw;

}





返回isConnected;



}
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;

}


为了让Cassandra在虚拟机中启动并运行(或者你选择物理运行),你应该遵循以下步骤( 2010年11月5日):



创建一个虚拟机来托管Ubuntu 9.10 x86桌面。我在Windows上使用VirtualBox作为我的VM主机,所以我将在这里引用它,但随意用你喜欢的VM软件替换它。我创建的VM使用桥接网络适配器分配了384MB的8GB虚拟硬盘(因此可以从Windows XP主机访问)。完成安装过程后,请安装客户端工具,以便更改屏幕分辨率,共享文件夹等。要使用虚拟框执行此操作,请转到设备>安装Guest Additions,然后打开终端窗口并执行



sudo /media/cdrom/VBoxLinuxAdditions-x86.run



一旦完成,您将需要重新启动VM。

现在是时候安装Cassandra / Thrift先决条件和软件,我们将设置一切:



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



一旦完成,我们可以安装其他有用的Ubuntu软件,我们将使用:



sudo apt-get install rapidsvn -y

sudo apt-get install monodevelop -y



我也想安装以下内容:



sudo apt-get install nautilus-open-terminal -y

sudo apt-get install samba smbfs smbclient winbind -y

sudo apt-get install nautilus-share -y



您可能会发现Java运行时主目录存在问题。如果发生这种情况,请通过从终端窗口运行以下命令并选择第三个选项,选择Sun的Java Runtime作为默认Java版本:



sudo update-alternatives - config java



现在转到Places>打开Nautilus主文件夹。右键单击并创建一个Development文件夹。在Development文件夹中创建另外两个文件夹:Cassandra和Thrift。

我们现在可以获得Cassandra和Thrift的源代码。通过转到应用程序>运行RapidSVN编程> RapidSVN。通过转到RapidSVN菜单选项Repository>查看SVN的源代码。查看。在URL中输入http://svn.apache.org/repos/asf/incubator/thrift/trunk,对于目标目录,浏览到Thrift开发文件夹。所以看起来应该是这样的:





现在通过做同样的事情来获取Cassandra代码但输入http://svn.apache .org / repos / asf / incubator / cassandra / trunk作为URL并选择Cassandra目录。

一旦我们有了所有代码,我们就可以编译Cassandra了,所以让我们通过执行以下命令来做到这一点来自终端窗口的命令:



cd~ / Development / Cassandra

ant



通过打开一个新的终端窗口并运行以下命令来启动Cassandra服务器:



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



正在运行的服务器将如下面的屏幕截图所示。您应该注意localhost / 127.0.0.1:9160的thrift绑定地址。





现在我们想要与服务器进行通信,因此我们需要编译Thrift,以便我们可以使用它来生成我们的C#客户端:



cd~ /开发/节俭

./bootstrap.sh

./configure < br $>
make

sudo make install



现在我们最终可以为Cassandra的Thrift调用生成一个C#客户端:



cd~ /开发/ Cassandra / interface /

~ /开发/ Thrift / compiler / cpp / thrift -gen csharp cassandra.thrift



这将在Cassandra / interface文件夹中创建以下目录:gen-csharp / Apache / Cassandra。这些文件以及Thrift.dll将被复制到我们将使用Development文件夹中的MonoDevelop创建的新演示项目中。所以再次在终端窗口执行:



mkdir~ / Development / CassandraDemo

cp~ / Development / Cassandra / interface / gen-csharp 〜/开发/ CassandraDemo -R

cp~ /开发/ Thrift / lib / csharp / Thrift.dll~ /开发/ CassandraDemo



通过转到File>打开MonoDevelop并创建一个新的控制台应用程序。新>解。现在执行以下操作:

选择控制台项目

输入CassandraDemo作为名称

对于位置浏览到我们在您创建的Development文件夹中主目录。

取消选中创建单独的解决方案目录复选框。

单击转发按钮

不要对其进行任何更改下一个屏幕,然后单击确定。

右键单击左侧树视图中的CassandraDemo项目,然后选择添加>添加文件....导航到〜/ Development / CassandraDemo / gen-csharp / Apache / Cassandra并选择文件夹中的所有文件。您现在将拥有一个如下所示的项目:





通过右键单击References并选择Edit来添加对Thrift程序集的引用引用。切换到.Net Assembly选项卡并双击Thrift.dll并单击OK。

现在我们可以添加示例代码来测试它。因此,双击Main.cs以确保它是编辑器中的焦点类。用以下内容替换文件的内容:

命名空间CassandraDemo

{

使用System;

使用System.Collections.Generic;

使用System.Diagnostics;



使用Apache.Cassandra;

使用Thrift.Protocol;

使用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(打开连接);

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(名字)

};



Console.WriteLine(插入名称列);



//将数据插入名称栏目

client.insert(Keyspace1,

1,

nameColumnPath,

utf8Encoding.GetBytes(Joe Bloggs),

timeStamp,

ConsistencyLevel.ONE);



client.insert(Keyspace1,

2,

nameColumnPath,

utf8Encoding.GetBytes(Joe Soap),

timeStamp,

ConsistencyLevel.ONE);



//简单的单值获取使用键获取列'name'

ColumnOrSuperColumn returnedColumn = client.get(Keyspace1, 1,nameColumnPath,ConsistencyLevel.ONE);

Console.WriteLine(Keyspace1 / Standard1中的列数据:名称:{0},值:{1},

utf8Encoding.GetString(returnedColumn.Column.Name ),

utf8Encoding.GetString(returnedColumn.Column.Value));



Console.WriteLine(获取拼接范围);



//读一整行

SlicePredicate谓词=新的SlicePredicate()

{

Slice_range = new SliceRange()

{

//开始和结束不能为空

开始=新字节[0] ,

完成=新字节[0],

计数= 10,

反转=假

}

};



ColumnParent parent = new ColumnParent(){Column_family =Standard1};

Dictionary< string>> results = client.multiget_slice(Keyspace1,

new List< string>(){1,2},

parent,

谓词,

ConsistencyLevel.ONE);



foreach(KeyValuePair< string,>> resultPair in results)

{

Console.WriteLine (Key:{0},resultPair.Key);



foreach(在ResultPair.Value中的ColumnOrSuperColumn resultColumn)

{

列column = resultColumn.Column;

Console.WriteLine(name:{0},value:{1},utf8Encoding.GetString(column.Name),utf8Encoding.GetString(column.Value));

}

}



Console.WriteLine(关闭连接);

transport.Close();

}

}

}

点击F5并观察应用程序输出窗口。这就是你应该看到的:







希望这足以让你前进并拥有你填补剩余的空白。如果您发现它有用,请发表评论。如果Cassandra家伙会
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


这篇关于通过我的C#Web应用程序连接到cassandra DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆