facebookde 的 NoSQL資料庫cassandra的配置與調用(java&&c#)

來源:互聯網
上載者:User

上次說了安裝的問題,可以參考《VirtualBox 虛擬機器 Debian系統上安裝Cassandra步驟及遇到的問題》。當然,在windows下也可以使用,但是要設定JAVA_HOME參數,然後啟動目錄bin裡的cassandra.bat。編輯cassandra.bat看到

if NOT DEFINED CASSANDRA_HOME set CASSANDRA_HOME=%CD%

改成

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.sh

conf/log4j.properties

conf/storage-conf.xml

其中,log4j.properties是日誌的配置,其它兩個是配置的運行環境。

 

cassandra.in.sh檔案一般不需要調整,當然,加入你沒有使用alternatives調整java的預設環境,而你又需要使用jre6.0,這種情況下,可以設定cassandra.in.sh中

#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上,解壓。進入目錄後進行安裝。

#chmod +x *  //設定執行許可權

#./bootstrap.sh

#./configure

#make

#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項目,就可以寫以下代碼調用:

 

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

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

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

client.insert("Keyspace1",
"1",
nameColumnPath,
utf8Encoding.GetBytes("測試輸入1"),
timeStamp,
ConsistencyLevel.ONE);

client.insert("Keyspace1",
"2",
nameColumnPath,
utf8Encoding.GetBytes("測試輸入2"),
timeStamp,
ConsistencyLevel.ONE);

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));
transport.Close();

Console.ReadKey();
}

}
}

 

 

 

而Java的就變成

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

 

java相應的代碼

 

import static me.prettyprint.cassandra.utils.StringUtils.bytes;

import java.io.UnsupportedEncodingException;
import org.apache.cassandra.service.Cassandra;
import org.apache.cassandra.service.ColumnOrSuperColumn;
import org.apache.cassandra.service.ColumnPath;
import org.apache.cassandra.service.ConsistencyLevel;
import org.apache.cassandra.service.InvalidRequestException;
import org.apache.cassandra.service.NotFoundException;
import org.apache.cassandra.service.TimedOutException;
import org.apache.cassandra.service.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.*;


public class Program {
public class s{

}

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Long startTime = System.currentTimeMillis();
for(int i = 0;i < 10000;i++){
run();
}

Long endTime = System.currentTimeMillis();
System.out.println("程式運行到此處電腦當前毫秒數 " + startTime);
System.out.println("程式共計運行 "+ (endTime-startTime)+" 毫秒");
}

static void run() throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException, UnsupportedEncodingException{
TTransport transport = new TSocket("192.168.93.30",9160);
TProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);

transport.open();
Long timeStamp = System.currentTimeMillis();

ColumnPath nameColumnPath = new ColumnPath("Standard1",null,bytes("name"));


client.insert("Keyspace1",
"1",
nameColumnPath,
bytes("測試資料1"),
timeStamp,
ConsistencyLevel.ONE);

client.insert("Keyspace1",
"2",
nameColumnPath,
bytes("測試資料2"),
timeStamp,
ConsistencyLevel.ONE);


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

System.out.println(String.format("key:%s;value:%s",
new String(returnedColumn.column.name),
new String(returnedColumn.column.value,"utf-8")));

transport.close();
}

}

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.