Skip to content

Instantly share code, notes, and snippets.

@simonwoo
Created March 6, 2015 09:55
Show Gist options
  • Save simonwoo/484e63e388878205c5f5 to your computer and use it in GitHub Desktop.
Save simonwoo/484e63e388878205c5f5 to your computer and use it in GitHub Desktop.
a simple demo to test cassandra
package com.restlet.testcassandra;
import com.datastax.driver.core.*;
/*
* an simple exemple to use java drive to manipuler the cassandra
* */
public class TestCassandra {
private Cluster cluster; //add a contact point
private Session session; //manage the connections
public TestCassandra(){
this.cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
this.session = cluster.connect("apispark");
}
public ResultSet execute(String query){
return session.execute(query);
}
public void close(){
this.cluster.close();
}
public static void main(String[] args) {
//
TestCassandra test = new TestCassandra();
//query and output the result
ResultSet results = test.execute("SELECT * FROM \"Tutorial\"");
for(Row row:results){
System.out.println(row);
System.out.format("id:%s\ndescription:%s\nlink:%s\nname:%s\norder:%d\n",
row.getString("id"),
row.getString("description"),
row.getString("link"),
row.getString("name"),
row.getInt("order")
);
System.out.println("------------------------------------------");
}
//close the cluster
test.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment