蓝色的蜜蜂是什么蜂:详解NoSQL数据库Apache Cassandra的配置_Apache_开发学院

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 03:36:31
详解NoSQL数据库Apache Cassandra的配置时间:2010-04-28 09:35:24来源:网络 作者:未知 点击:616次 在这里我们将介绍的是NoSQL数据库Apache Cassandra的配置与相关问题。现在数据库市场对于NoSQL的关注度日益升高,我们也该看到未来数据库技术的变革。
在这里我们将介绍的是NoSQL数据库Apache Cassandra的配置与相关问题。现在数据库市场对于NoSQL的关注度日益升高,我们也该看到未来数据库技术的变革。上次说了安装的问题,可以参考《VirtualBox 虚拟机 Debian系统上安装Cassandra步骤及遇到的问题》。当然,在windows下也可以使用,但是要设置JAVA_HOME参数,然后启动目录bin里的cassandra.bat。编辑cassandra.bat看到01.if NOT DEFINED CASSANDRA_HOME set CASSANDRA_HOME=%CD% 改成01.if NOT DEFINED CASSANDRA_HOME set CASSANDRA_HOME=F:\apache-cassandra-0.5.1 “F:\apache-cassandra-0.5.1”是我的安装目录。一、cassandra的单节点服务器配置先说下cassandra的配置,还是讲Linux下的。需要配置的文件一共有三个,当然,也可以使用默认配置。这个三个文件分别是:bin/cassandra.in.shconf/log4j.propertiesconf/storage-conf.xml其中,log4j.properties是日志的配置,其它两个是配置的运行环境。cassandra.in.sh文件一般不需要调整,当然,加入你没有使用alternatives调整java的默认环境,而你又需要使用jre6.0,这种情况下,可以设置cassandra.in.sh中 01.#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访问cassandracassandra用到了另外一个好用的东西:thrift。这个东东可以在http://www.thrift-rpc.org/下载。具体在http://www.thrift-rpc.org/?p=thrift.git;a=shortlog;h=refs/misc/instant,一般点第一个snapshot就行了,这是最新的。版本几个小时更新一个,太牛叉了。下载完后,搞到Linux上,解压。进入目录后进行安装。01.#chmod +x *  //设置执行权限  02.#./bootstrap.sh  03.#./configure  04.#make  05.#make install 安装好了,接下来,开始生成操作。切换到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项目,就可以写以下代码调用:01.using System;  02.using Thrift.Transport;  03.using Thrift.Protocol;  04.using Apache.Cassandra;namespace TestCa {      05.class Program {          06.static void Main(string[] args)   07.{              08.TTransport transport = new TSocket("192.168.93.30", 9160);              09.TProtocol protocol = new TBinaryProtocol(transport);              10.Cassandra.Client client = new Cassandra.Client(protocol);              11.transport.Open();              12.System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;              13.long timeStamp = DateTime.Now.Millisecond;            14.ColumnPath nameColumnPath = 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.timeStamp,                            28.ConsistencyLevel.ONE);              29.ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "1", nameColumnPath, ConsistencyLevel.ONE);              30.Console.WriteLine("Keyspace1/Standard1 列值: 键: {0}, 值: {1}",                                31.utf8Encoding.GetString(returnedColumn.Column.Name),                              32.utf8Encoding.GetString(returnedColumn.Column.Value));            33.transport.Close();              34.Console.ReadKey();          35.}    }} 而Java的就变成/home/xieping/thrift/ompiler/cpp/thrift -gen java cassandra.thriftjava相应的代码01.import static me.prettyprint.cassandra.utils.StringUtils.bytes;  02.import java.io.UnsupportedEncodingException;  03.import org.apache.cassandra.service.Cassandra;  04.import org.apache.cassandra.service.ColumnOrSuperColumn;  05.import org.apache.cassandra.service.ColumnPath;  06.import org.apache.cassandra.service.ConsistencyLevel;  07.import org.apache.cassandra.service.InvalidRequestException;  08.import org.apache.cassandra.service.NotFoundException;  09.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./**     * @param args       18.* @throws Exception        19.*/      20.public static void main(String[] args) throws Exception {          21.Long startTime = System.currentTimeMillis();           22.for(int i = 0;i < 10000;i++){              23.run();          24.}          25.Long endTime = System.currentTimeMillis();         System.out.println("程序运行到此处计算机当前毫秒数 " + startTime);          26.System.out.println("程序共计运行 "+ (endTime-startTime)+" 毫秒");      27. }          28.static void run() throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, UnsupportedEncodingException{        TTransport transport = new TSocket("192.168.93.30",9160);          29.TProtocol protocol = new TBinaryProtocol(transport);          30.Cassandra.Client client = new Cassandra.Client(protocol);          31.transport.open();          32.Long timeStamp = System.currentTimeMillis();                   33.ColumnPath nameColumnPath = new ColumnPath("Standard1",null,bytes("name"));          34.client.insert("Keyspace1",                        35."1",                      nameColumnPath,                        36.bytes("测试数据1"),                      timeStamp,                      ConsistencyLevel.ONE);          37.client.insert("Keyspace1",                        38."2",                      nameColumnPath,                        39.bytes("测试数据2"),                      timeStamp,                      ConsistencyLevel.ONE);          40.ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "1", nameColumnPath, ConsistencyLevel.ONE);                  41.System.out.println(String.format("key:%s;value:%s",                   42.new String(returnedColumn.column.name),                new String(returnedColumn.column.value,"utf-8")));          43.transport.close();     44. }    } 
本篇文章来源于:开发学院 http://edu.codepub.com   原文链接:http://edu.codepub.com/2010/0428/22385.php