Skip to content

Instantly share code, notes, and snippets.

@shehaaz
Last active December 15, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shehaaz/5279566 to your computer and use it in GitHub Desktop.
Save shehaaz/5279566 to your computer and use it in GitHub Desktop.
Cassandra Java Driver
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment