Skip to content

Instantly share code, notes, and snippets.

@amigoavena

amigoavena/KeySpaces.java

Last active Oct 12, 2015
Embed
What would you like to do?
Basic Connection to Cassandra
package com.cassandra.main;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.KsDef;
import org.apache.cassandra.thrift.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
/**
* @author havena
*
*/
public class KeySpaces {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 9160;
// The objective is to create a Cassandra.Client instance that can
// communicate with Cassandra. The Thrift framework requires several
// steps to instantiate:
TSocket socket = new TSocket(host, port);
TTransport transport = new TFramedTransport(socket);
TProtocol proto = new TBinaryProtocol(transport);
try {
transport.open();
Cassandra.Client client = new Cassandra.Client(proto);
// We call methods from the Cassandra.Client that allow the user to
// inspect the server, such as describing the cluster name and the
// version:
System.out.println("version " + client.describe_version());
System.out.println("partitioner" + client.describe_partitioner());
System.out.println("cluster name " + client.describe_cluster_name());
for (KsDef keyspace : client.describe_keyspaces()) {
System.out.println("keyspace [" +keyspace+"]");
}
transport.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.