Cassandra.Client는 org.apache.cassandra.thrift 패키지의 클래스이고, 결국, Client는 Cassandra의 Inner 클래스가 됩니다..그리고, 위 패키지는 Thrift라는 데이타 serialize/deserialize 라이브러리(Google Protocol Buffers랑 비슷)를 통해서 전송될 데이타를 만들고, 전송된 데이타를 처리하고 있겠죵.. Cassandra의 언어별 클라이언트 라이브러리는 http://wiki.apache.org/cassandra/ClientOptions 페이지에 자세히 기술이 되어 있습니다.. 클라이언트 라이브러리들은 보통 Cassandra 서버에 붙는 Connection에 대한 풀링을 제공하는데, Connection 풀링은 org.apache.cassandra.thrift 패키지의 Cassandra.Client를 풀링해서 구현 할 수 있습니다.
아래 코드는 Cassandra.Client의 풀링을 통한 Cassandra 서버의 연결에 대한 풀링기능을 제공하고 있습니다..^^
* CassandraClientArrayFactory.java
package net.sjava.cassandra.test;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
/**
* Cassandra.Client 풀링 클래스
*
* @author mcsong@gmail.com
* @since 2010/11/30
*
*/
public class CassandraClientArrayFactory {
/**
* host array
*/
private static String[] hosts = “127.0.0.1,127.0.0.1”.split(“,”) ;
/**
* port
*/
private static int port = 9160;
/**
* pooing count
*/
private static int count = 10;
//
private static ConcurrentLinkedQueue<Cassandra.Client> clients = new ConcurrentLinkedQueue<Cassandra.Client>();
/**
* Cassandra.Client를 생성한다.
* @return
* @throws Exception
*/
private static Cassandra.Client createCassandraClient() throws Exception {
int index = new Random().nextInt(hosts.length);
TTransport socket = new TSocket(hosts[index], port);
socket.open();
return new Cassandra.Client(new TBinaryProtocol(socket, false, false));
}
/**
* Cassandra.Client를 풀에서 가져온다.
* @return
* @throws Exception
*/
public static Cassandra.Client poll() throws Exception {
if (!clients.isEmpty() && clients.peek() != null)
return clients.poll();
return createCassandraClient();
}
/**
* Cassandra.Client를 풀에 넣는다.
* @param client
* @throws Exception
*/
public static void push(Cassandra.Client client) throws Exception {
System.out.println(“channel size : ” + clients.size());
if (clients.size() >= count) {
client = null; // garbage
return;
}
clients.offer(client);
}
}
* CassandraClientArrayFactoryTest.java
package net.sjava.cassandra.test;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import static net.sjava.cassandra.test.CassandraClientArrayFactory.poll;
import static net.sjava.cassandra.test.CassandraClientArrayFactory.push;
public class CassandraClientArrayFactoryTest {
public static void main(String[] args) {
String keyspace = “Keyspace1”;
String columnFamily = “Standard2”;
String key = “key1”;
long timestamp = System.currentTimeMillis();
Cassandra.Client client = null;
try {
client = poll();
for(int i=0; i < 15; i++)
push(client);
String value =”bbbbbbbbbbbbbbbbbbbbbbbbbbbbb”;
ColumnPath cPath = new ColumnPath(columnFamily);
cPath.setColumn(“name”.getBytes(“utf-8”));
client.insert(keyspace, key, cPath, value.getBytes(“utf-8”), timestamp, ConsistencyLevel.ONE);
Column col = client.get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
System.out.println(“Column name: ” + new String(col.name, “utf-8”));
System.out.println(“Column value: ” + new String(col.value, “utf-8”));
value =”ccccccccccccccccccccccccccccccccccccccc”;
long time2 = System.currentTimeMillis();
client.insert(keyspace, key, cPath, value.getBytes(“utf-8”), time2, ConsistencyLevel.ONE);
Column col2 = client.get(keyspace, key, cPath, ConsistencyLevel.ONE).getColumn();
System.out.println(“Column2 name: ” + new String(col2.name, “utf-8”));
System.out.println(“Column2 value: ” + new String(col2.value, “utf-8”));
} catch(Exception e) {
e.printStackTrace();
} finally {
if(client != null)
try {
push(client);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}