Last active
December 15, 2015 15:09
-
-
Save shehaaz/5279566 to your computer and use it in GitHub Desktop.
Cassandra Java Driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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