详解NoSQL数据库Apache Cassandra的配置

在这里我们将介绍的是NoSQL数据库Apache Cassandra的配置与相关问题。现在数据库市场对于NoSQL的关注度日益升高,我们也该看到未来数据库技术的变革

上次说了安装的问题,可以参考《VirtualBox 虚拟机 Debian系统上安装Cassandra步骤及遇到的问题》。当然,在windows下也可以使用,但是要设置JAVA_HOME参数,然后启动目录 bin里的cassandra.bat。编辑cassandra.bat看到

 
 
  1. ifNOTDEFINEDCASSANDRA_HOMEset CASSANDRA_HOME =%CD%

改成

 
 
  1. ifNOTDEFINEDCASSANDRA_HOMEset CASSANDRA_HOME = F :/apache-cassandra-0.5.1

“F:/apache-cassandra-0.5.1”是我的安装目录。

一、cassandra的单节点服务器配置

先说下cassandra的配置,还是讲Linux下的。需要配置的文件一共有三个,当然,也可以使用默认配置。

这个三个文件分别是:

bin/cassandra.in.sh

conf/log4j.properties

conf/storage-conf.xml

其中,log4j.properties是日志的配置,其它两个是配置的运行环境。

cassandra.in.sh文件一般不需要调整,当然,加入你没有使用alternatives调整java的默认环境,而你又需要使用 jre6.0,这种情况下,可以设置cassandra.in.sh中

 
 
  1. # JAVA_HOME =/usr/local/jdk6

JAVA_HOME=/usr/local/jre6 #这里是你的jre解压缩的路径

log4j.properties的配置网上讲的很多,就不说了。

storage-conf.xml的配置是最重要的。

第一个是Keyspaces,这个默认只设置了Keyspace1,可以增加另外的Keyspaces。客户端调用需要使用这个名字。

Keyspace节点中的KeysCachedFraction设置的键索引的内存大小。说明上也写了,假如键的数量较少,长度较长,可以增加这个 值。而设置为0,则是禁用。

接下来是设置ColumnFamily,这里配置的名称,在客户端调用时候也要是有。另外还指定了列的类型。

ReplicationFactor设置了副本的数目,这个是在分布式部署中有用,保持数据的冗余,以至于某几台服务坏掉,能保证数据完整。

CommitLogDirectory以及接下来的几行都是设置目录的,这个就不说了。

Seeds也是和分部署主从服务器部署方式有关的,本文不准备讲这个。

ThriftAddress是比较重要的,这个是设置客户端访问的,而ThriftPort是设置访问的端口。接下来的部分是和性能有关的,这些说 明可以仔细阅读。贫道对下面的设置也理解不深入,就不献丑了。

二、如何编程访问cassandra

从http://incubator.apache.org/cassandra/找了好久,找到了http://github.com /rantav/hector(java)。这个是一个访问cassandra的包装。很遗憾的是,我使用这个包装访问时候,读取一个Key的值需要 7~8秒!!!晕倒。我开始以为是虚拟机的原因,结果部署到其他两台linux服务器上还是一样。当然这些机器和我的机器都不在同一个网段,我不知道这点 是不是会对性能有很大的影响。后来,我放到自己机器上,以及把写好的程序当道目标机器上,读取速度变成了20MS每条。性能相差也太大了。一个是速度慢得 和蚂蚁一样,而第二次则是坐上乌龟了。

其它语言的访问包装可以在http://wiki.apache.org/cassandra/ClientExamples这里找到。当然,没 有C#的。

三、用C#和Java访问cassandra

cassandra用到了另外一个好用的东西:thrift。这个小编可以在http://www.thrift-rpc.org/下载。

具体在http://www.thrift-rpc.org/?p=thrift.git;a=shortlog;h=refs/misc /instant,一般点第一个snapshot就行了,这是最新的。版本几个小时更新一个,太牛叉了。

下载完后,搞到Linux上,解压。进入目录后进行安装。

 
 
  1. #chmod+x*//设置执行权限
  2. #./bootstrap.sh
  3. #./configure
  4. #make
  5. #makeinstall

安装好了,接下来,开始生成操作。

切换到cassandra的interface目录。

然后,使用/home/xieping/thrift/ompiler/cpp/thrift -gen csharp cassandra.thrift 命令生成。运行该命令后,在interface目录增加了gen-csharp目录。把它搞到你的机器,然后,切换到/home/xieping /thrift/lib/csharp目录。把src目录搞下来。打开Thrift.csproj文件,右键Thrift项目,设置编译符号为 NET_2_0。新建个C#项目,把gen-csharp目录下的东西添加进去,然后,引用Thrift项目,就可以写以下代码调用:

 
 
  1. using System;
  2. using Thrift.Transport;
  3. using Thrift.Protocol;
  4. using Apache.Cassandra; namespace TestCa{
  5. class Program{
  6. static void Main( string []args)
  7. {
  8. TTransporttransport= new TSocket( "192.168.93.30" ,9160);
  9. TProtocolprotocol= new TBinaryProtocol(transport);
  10. Cassandra.Clientclient= new Cassandra.Client(protocol);
  11. transport.Open();
  12. System.Text.Encodingutf8Encoding=System.Text.Encoding.UTF8;
  13. long timeStamp=DateTime.Now.Millisecond;
  14. ColumnPathnameColumnPath= new ColumnPath(){
  15. Column_family= "Standard1" ,
  16. Column=utf8Encoding.GetBytes( "name" )
  17. };
  18. client.insert( "Keyspace1" ,
  19. "1" ,nameColumnPath,
  20. utf8Encoding.GetBytes( "测试输入1" ),
  21. timeStamp,
  22. ConsistencyLevel.ONE);
  23. client.insert( "Keyspace1" ,
  24. "2" ,
  25. nameColumnPath,
  26. utf8Encoding.GetBytes( "测试输入2" ),
  27. ConsistencyLevel.ONE);
  28. ColumnOrSuperColumnreturnedColumn=client. get ( "Keyspace1" , "1" ,ConsistencyLevel.ONE);
  29. Console.WriteLine( "Keyspace1/Standard1列值:键:{0},值:{1}" ,
  30. utf8Encoding.GetString(returnedColumn.Column.Name),
  31. utf8Encoding.GetString(returnedColumn.Column.Value));
  32. transport.Close();
  33. Console.ReadKey();
  34. }}}

而Java的就变成

/home/xieping/thrift/ompiler/cpp/thrift -gen java cassandra.thrift

java相应的代码

 
 
  1. import static me.prettyprint.cassandra.utils.StringUtils.bytes;
  2. import java.io.UnsupportedEncodingException;
  3. import org.apache.cassandra.service.Cassandra;
  4. import org.apache.cassandra.service.ColumnOrSuperColumn;
  5. import org.apache.cassandra.service.ColumnPath;
  6. import org.apache.cassandra.service.ConsistencyLevel;
  7. import org.apache.cassandra.service.InvalidRequestException;
  8. import org.apache.cassandra.service.NotFoundException;
  9. import org.apache.cassandra.service.TimedOutException;
  10. import org.apache.cassandra.service.UnavailableException;
  11. import org.apache.thrift.TException;
  12. import org.apache.thrift.protocol.TBinaryProtocol;
  13. import org.apache.thrift.protocol.TProtocol;
  14. import org.apache.thrift.transport.*; public class Program{
  15. public class s{
  16. }
  17. /***@paramargs
  18. *@throwsException
  19. */
  20. public static void main(String[]args) throws Exception{
  21. LongstartTime=System.currentTimeMillis();
  22. for ( int i= 0 ;i< 10000 ;i++){
  23. run();
  24. }
  25. LongendTime=System.currentTimeMillis();System.out.println( "程序运行到此处计算机当前毫秒数" +startTime);
  26. System.out.println( "程序共计运行" +(endTime-startTime)+ "毫秒" );
  27. }
  28. static void run() throws InvalidRequestException,UnavailableException,TimedOutException,TException,NotFoundException,UnsupportedEncodingException{TTransporttransport= new TSocket( "192.168.93.30" , 9160 );
  29. TProtocolprotocol= new TBinaryProtocol(transport);
  30. Cassandra.Clientclient= new Cassandra.Client(protocol);
  31. transport.open();
  32. LongtimeStamp=System.currentTimeMillis();
  33. ColumnPathnameColumnPath= new ColumnPath( "Standard1" , null ,bytes( "name" ));
  34. client.insert( "Keyspace1" ,
  35. "1" ,
  36. bytes( "测试数据1" ),timeStamp,ConsistencyLevel.ONE);
  37. client.insert( "Keyspace1" ,
  38. bytes( "测试数据2" ),ConsistencyLevel.ONE);
  39. ColumnOrSuperColumnreturnedColumn=client.get( "Keyspace1" ,ConsistencyLevel.ONE);
  40. System.out.println(String.format( "key:%s;value:%s" ,
  41. new String(returnedColumn.column.name), new String(returnedColumn.column.value, "utf-8" )));
  42. transport.close();
  43. }}
原文标题: facebookde 的 NoSQL数据库cassandra的配置与调用(java&&c#)

相关文章

文章浏览阅读752次。关系型数据库关系型数据库是一个结构化的...
文章浏览阅读687次,点赞2次,收藏5次。商城系统中,抢购和秒...
文章浏览阅读1.4k次。MongoTemplate开发spring-data-mongodb...
文章浏览阅读887次,点赞10次,收藏19次。1.背景介绍1. 背景...
文章浏览阅读819次。MongoDB连接失败记录_edentialmechanisn...
文章浏览阅读470次。mongodb抽取数据到ES,使用ELK内部插件无...