DB/Cassandra
카산드라 API를 통해 접근
알 수 없는 사용자
2012. 6. 7. 22:57
4.3.5 카산드라 API를 통해 접근
가장 먼저 카산드라 서버를 실행시켜야 한다
카산드라 서버는 카산드라가 있는 경로의 bin 폴더에서 cassandra.bat 를 실행시키면 된다
public class FramedConnWrapper {
private TTransport transport; // 트랜스포트
private TProtocol proto; // 프로토콜
private TSocket socket; // 소켓
public FramedConnWrapper(String host, int port) {
socket = new TSocket(host,port); // 가장 먼저 호스트서버와 포트번호를 입력하여 소켓을 생성한다
transport = new TFramedTransport(socket); // 소켓을 통해 프레임트랜스포트를 생성한다
proto = new TBinaryProtocol(transport); // 트랜스포트를 통해 바이너리프로토콜을 생성한다
}
public void open() throws Exception {
transport.open();
}
public void close() throws Exception {
transport.close();
socket.close();
}
public Cassandra.Client getClient() {
Cassandra.Client client = new Cassandra.Client(proto);
return client;
}
public static void main(String[] args) {
FramedConnWrapper f = new FramedConnWrapper("localhost", 9160);
try {
f.open(); // 트랜스포트를 연결하여 서버에 접근이 가능하다
Cassandra.Client client = f.getClient(); // 클라이언트를 얻어온다
System.out.println(client.describe_cluster_name()); // 클라이언트를 통해 클러스터 이름을 얻는다
System.out.println(client.describe_version()); // 현재 API 버전을 얻는다
System.out.println(client.describe_keyspaces().get(1)); // KeySpace 정보를 얻는다
} catch (Exception e) {
// TODO: handle exception
}
}
}