public
Last active

Cassandra Java Driver

  • Download Gist
CassandraDriver.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package com.example.cassandra;
 
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.Query;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
 
public class Cassandra {
 
private Session session = null;
 
public Cassandra(){
 
this("127.0.0.1");
}
 
public Cassandra(String contactPoint){
 
Cluster cluster = new Cluster.Builder().
addContactPoints(contactPoint).build();
 
//I previously created a keyspace called "android"
this.session = cluster.connect("android");
}
 
public void saveOrUpdate(String cql) {
try {
session.execute(cql);
}
catch(NoHostAvailableException nhae){
}
}
 
public ResultSet findByCQL(String cql){
try {
Query cqlQuery = new SimpleStatement(cql);
cqlQuery.setConsistencyLevel(ConsistencyLevel.QUORUM);
cqlQuery.enableTracing();
return session.execute(cqlQuery);
}
catch(NoHostAvailableException nhae){
}
return null;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.