Created
May 4, 2015 05:52
-
-
Save vishnu667/1725c9bd6f132af3d04c to your computer and use it in GitHub Desktop.
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
/** | |
Maven Dependency | |
<dependency> | |
<groupId>org.cassandraunit</groupId> | |
<artifactId>cassandra-unit</artifactId> | |
<version>1.2.0.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.datastax.cassandra</groupId> | |
<artifactId>cassandra-driver-core</artifactId> | |
<version>2.1.2</version> | |
</dependency> | |
**/ | |
import com.datastax.driver.core.*; | |
import java.util.List; | |
public final class CassandraConnector { | |
private CassandraConnector(){} | |
private static final String CIP = "127.0.0.1"; | |
private static Cluster cluster = Cluster.builder().addContactPoint(CIP).build(); | |
private static Session session = null; | |
private static Session getSession() { | |
if (session == null) { | |
session = cluster.connect(); | |
session.execute("use dmp"); | |
} | |
return session; | |
} | |
public static Row getRowWhere(String columnFamily,String indexedColumnName,Long value){ | |
List<Row> result = getRowListWhere(columnFamily, indexedColumnName, value); | |
Row row = null; | |
for(Row currentRow:result){ | |
row = currentRow; | |
} | |
return row; | |
} | |
public static Row getRowWhere(String columnFamily,String indexedColumnName,String value){ | |
List<Row> result = getRowListWhere(columnFamily, indexedColumnName, value); | |
Row row = null; | |
for(Row currentRow:result){ | |
row = currentRow; | |
} | |
return row; | |
} | |
public static List<Row> getRowListWhere(String columnFamily,String indexedColumnName,String value){ | |
ResultSet result=getSession().execute("select * from "+columnFamily+" where "+indexedColumnName+"=\'"+value+"\'"); | |
return result.all(); | |
} | |
public static List<Row> getRowListWhere(String columnFamily,String indexedColumnName,Long value){ | |
ResultSet result=getSession().execute("select * from "+columnFamily+" where "+indexedColumnName+"="+String.valueOf(value)); | |
return result.all(); | |
} | |
public static List<Row> getRowList(String columnFamily){ | |
ResultSet result=getSession().execute("select * from "+columnFamily); | |
return result.all(); | |
} | |
//public static void insertRow(List<String> coloumnNames,List<String> columnValues){ | |
public static void insertRow(Statement statement){ | |
getSession().execute(statement); | |
} | |
public static List<Row> executeStatement(Statement statement){ | |
return getSession().execute(statement).all(); | |
} | |
public static void close() { | |
cluster.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment