use of com.datastax.driver.core.PreparedStatement in project pinpoint by naver.
the class CassandraDatastaxITBase method test.
@Test
public void test() throws Exception {
final int testId = 99;
final String testValue = "testValue";
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
Session myKeyspaceSession = null;
try {
myKeyspaceSession = cluster.connect(TEST_KEYSPACE);
// ===============================================
// Insert Data (PreparedStatement, BoundStatement)
PreparedStatement preparedStatement = myKeyspaceSession.prepare(CQL_INSERT);
BoundStatement boundStatement = new BoundStatement(preparedStatement);
boundStatement.bind(testId, testValue);
myKeyspaceSession.execute(boundStatement);
verifier.printCache();
// Cluster#connect(String)
Class<?> clusterClass = Class.forName("com.datastax.driver.core.Cluster");
Method connect = clusterClass.getDeclaredMethod("connect", String.class);
verifier.verifyTrace(event(CASSANDRA, connect, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
// SessionManager#prepare(String) OR AbstractSession#prepare(String)
Class<?> sessionClass;
try {
sessionClass = Class.forName("com.datastax.driver.core.AbstractSession");
} catch (ClassNotFoundException e) {
sessionClass = Class.forName("com.datastax.driver.core.SessionManager");
}
Method prepare = sessionClass.getDeclaredMethod("prepare", String.class);
verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
// SessionManager#execute(Statement) OR AbstractSession#execute(Statement)
Method execute = sessionClass.getDeclaredMethod("execute", Statement.class);
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
// ====================
// Select Data (String)
final String cqlSelect = String.format("SELECT %s, %s FROM %s WHERE %s = %d", TEST_COL_ID, TEST_COL_VALUE, TEST_TABLE, TEST_COL_ID, testId);
verifySelect(myKeyspaceSession.execute(cqlSelect), testId, testValue);
verifier.printCache();
// SessionManager#execute(String) OR AbstractSession#execute(String)
execute = sessionClass.getDeclaredMethod("execute", String.class);
String normalizedSelectSql = SQL_PARSER.normalizedSql(cqlSelect).getNormalizedSql();
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedSelectSql, String.valueOf(testId))));
// ====================
// Delete Data (String)
final String cqlDelete = String.format("DELETE FROM %s.%s WHERE %s = ?", TEST_KEYSPACE, TEST_TABLE, TEST_COL_ID);
myKeyspaceSession.execute(cqlDelete, testId);
verifier.printCache();
// SessionManager#execute(String, Object[]) OR AbstractSession#execute(String, Object[])
execute = sessionClass.getDeclaredMethod("execute", String.class, Object[].class);
String normalizedDeleteSql = SQL_PARSER.normalizedSql(cqlDelete).getNormalizedSql();
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedDeleteSql, null)));
} finally {
closeSession(myKeyspaceSession);
}
}
use of com.datastax.driver.core.PreparedStatement in project camel by apache.
the class CassandraProducer method executePreparedStatement.
/**
* Execute CQL as PreparedStatement
*/
private ResultSet executePreparedStatement(Session session, Object messageCql, Object[] cqlParams) {
ResultSet resultSet;
PreparedStatement lPreparedStatement;
if (messageCql == null) {
// URI CQL
lPreparedStatement = this.preparedStatement;
} else if (messageCql instanceof String) {
// Message CQL
lPreparedStatement = getEndpoint().prepareStatement((String) messageCql);
} else if (messageCql instanceof RegularStatement) {
// Message Statement
lPreparedStatement = getEndpoint().getSession().prepare((RegularStatement) messageCql);
} else {
throw new IllegalArgumentException("Invalid " + CassandraConstants.CQL_QUERY + " header");
}
if (isEmpty(cqlParams)) {
resultSet = session.execute(lPreparedStatement.bind());
} else {
resultSet = session.execute(lPreparedStatement.bind(cqlParams));
}
return resultSet;
}
use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.
the class PersistenceRAInterfaceProxy method checkSmsExists.
public int checkSmsExists(long dueSlot, String targetId) throws PersistenceException {
try {
String s1 = "select \"ID\" from \"SLOT_MESSAGES_TABLE" + this.getTableName(dueSlot) + "\" where \"DUE_SLOT\"=? and \"TARGET_ID\"=?;";
PreparedStatement ps = session.prepare(s1);
BoundStatement boundStatement = new BoundStatement(ps);
boundStatement.bind(dueSlot, targetId);
ResultSet rs = session.execute(boundStatement);
return rs.all().size();
} catch (Exception e) {
int ggg = 0;
ggg = 0;
return -1;
}
}
use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.
the class PersistenceRAInterfaceProxy method testCassandraAccess.
public boolean testCassandraAccess() {
try {
Cluster cluster = Cluster.builder().addContactPoint(ip).build();
try {
Metadata metadata = cluster.getMetadata();
for (Host host : metadata.getAllHosts()) {
logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
}
Session session = cluster.connect();
session.execute("USE \"" + keyspace + "\"");
// testing if a keyspace is acceptable
int tstRes = 0;
PreparedStatement ps;
BoundStatement boundStatement;
try {
ps = session.prepare("SELECT * from \"TEST_TABLE\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
session.execute(boundStatement);
tstRes = 1;
} catch (Exception e) {
int g1 = 0;
g1++;
}
ProtocolVersion protVersion = DBOperations.getProtocolVersion(cluster);
if (protVersion == ProtocolVersion.V1) {
throw new Exception("We do not support cassandra databse 1.2 more");
// if (tstRes == 0) {
// session.execute("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
// }
//
// // deleting of current tables
// try {
// session.execute("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
// } catch (Exception e) {
// int g1 = 0;
// g1++;
// }
} else {
if (tstRes == 0) {
ps = session.prepare("CREATE TABLE \"TEST_TABLE\" (id uuid primary key);");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
session.execute(boundStatement);
}
// deleting of current tables
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_CURRENT_SLOT_TABLE + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
}
// 1
Date dt = new Date();
Date dt2 = new Date(new Date().getTime() + 1000 * 60 * 60 * 24);
Date dt3 = new Date(new Date().getTime() - 1000 * 60 * 60 * 24);
String tName = this.getTableName(dt);
doTrauncateTables(session, tName);
// 2
tName = this.getTableName(dt2);
doTrauncateTables(session, tName);
// 3
tName = this.getTableName(dt3);
doTrauncateTables(session, tName);
return true;
} finally {
cluster.close();
// cluster.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.
the class PersistenceRAInterfaceProxy method doTrauncateTables.
private void doTrauncateTables(Session session, String tName) {
PreparedStatement ps;
BoundStatement boundStatement;
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_DST_SLOT_TABLE + tName + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_SLOT_MESSAGES_TABLE + tName + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_MESSAGES + tName + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_MES_ID + tName + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
ps = session.prepare("TRUNCATE \"" + Schema.FAMILY_DLV_MES_ID + tName + "\";");
boundStatement = new BoundStatement(ps);
boundStatement.bind();
try {
session.execute(boundStatement);
} catch (Exception e) {
int g1 = 0;
g1++;
}
}
Aggregations