cassandra-java-clientを使ってみよう

編集中の記事が消えてしまいました。。。

まず最初にここからcassandra-java-clientをダウンロードします
現行で最新のjarは jcsadra-0.1-SNAPSHOT-2009-11-27.jar なのでこれを使います

次にcassandraやcassanra-java-clientが依存しているcommons-poolやthriftをダウンロードしてきます
commons-poolはここからダウンロードしてきます

回答したフォルダの中にあるcommons-pool-x.x.x.jarを利用します

次にthriftはここからダウンロードしてきます
thriftはjarファイルでなくソースをそのまま公開しているのでダウンロードしたフォルダの
thrift/lib/java/以下のファイルをjarファイルに固めておきます、名前はthrift.jarとかでいいんじゃないですかね
(ちなみにビルド・パスにlog4j.jarとslf4j-api.jarとslf4j-log4j.jarを通してからコンパイルしないとうまく動きませんでした)
log4j,slf4j-api,slf4j-log4jはCASSANDRA_HOME/libに入ってるやつを使えばいいと思います)

準備が整ったところでeclipsejavaプロジェクトをひとつ作ってビルド・パスに

jcsadra-01-SNAPSHOT.jar(cassandra-java-client)
thrift.jar
commons-pool.jar
apache-cassamdra.jar(CASSANDRA_HOME/libにあるやつ)
log4j.jar
slf4j-api.jar
slf4j-log4j.jar

を通しておきます

次にsrc以下に適当にPoolController.javaファイルを作ります(ちなみに以下のソースはcassandra-java-client公式にあるこことかここなどを参考にしています)

public class PoolController {
	private int maxIdle;
	private long maxWaitWhenBlockByExhausted;
	private ExhaustedAction exhaustedAction;
	private int maxActive;
	private int port;
	private String serviceURL;
	SimpleCassandraClientPool pool;
	AtomicInteger count;
	
	public PoolController(){
		createPool();
	}
	
	public void createPool(){
		this.serviceURL = "localhost";
		this.port = 9160;
		this.maxActive = 50;
		this.exhaustedAction = CassandraClientPool.ExhaustedAction.WHEN_EXHAUSTED_BLOCK;
		this.maxWaitWhenBlockByExhausted = 5000;
		this.maxIdle = 5;
		
		pool = new SimpleCassandraClientPool(serviceURL, port);
		count = new AtomicInteger(0);
	}
	public void closePool(){
		pool.close();
	}
	public SimpleCassandraClientPool getPool(){
		return this.pool;
	}
}

はい適当です、今回はとりあえず動けばいいという事なので必要最低限です、詳しくは上で述べた参照元のサイトを見るか自分で考えてください><

次にTest.javaというファイルを作って

public class Test {
	private static final String KEY_SPACE = "Keyspace1";
	private static final String COLUM_NAME = "Standard1";
	private static final String encoding = "utf-8";
	
	public static void main(String[] args){
		try{
			SimpleCassandraClientPool pool = new PoolController().getPool();
			CassandraClient client = pool.getClient();
			KeySpace ks = client.getKeySpace(KEY_SPACE);
			
			ColumnPath cp = new ColumnPath(COLUM_NAME, null, "testKey".getBytes(encoding));
			ks.insert("testKey", cp, ("testValue".getBytes(encoding)));
			
			Column col = ks.getColumn("testKey", cp);
			String value = new String(col.getValue(), encoding);
			System.out.println(value);
		}catch(TException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

として実行してみると

testValue

と出力されます(当然ですがcassandraを起動していないとエラーが出ます)
cassandra-cliで確認してみても

cassandra> get Keyspace1.Standard1['testKey']
=> (column=testKey, value=testValue, timestamp=1270216704518)
Returned 1 results.

と無事insertされていることが確認できます。

ちなみにKeyspace1とかStandard1ってなんだよ…どこでそんなの設定したんだよ…という方は
CASSANDRA_HOME/conf/storage-conf.xmlを確認してみると

<Storage>
    <ClusterName>Test Cluster</ClusterName>
        <Keyspaces>
            <Keyspace Name="Keyspace1">
                <ColumnFamily CompareWith="BytesType" Name="Standard1"/>
....

となっているので確認してみるとよいでしょう!!
あと様々な設定もここでできるので英語ですがコメントの説明を読んでみるといいと思います。