use of com.datastax.driver.core.Session in project camel by apache.
the class CassandraConsumer method poll.
@Override
protected int poll() throws Exception {
// Execute CQL Query
Session session = getEndpoint().getSessionHolder().getSession();
ResultSet resultSet;
if (isPrepareStatements()) {
resultSet = session.execute(preparedStatement.bind());
} else {
resultSet = session.execute(getEndpoint().getCql());
}
// Create message from ResultSet
Exchange exchange = getEndpoint().createExchange();
Message message = exchange.getIn();
getEndpoint().fillMessage(resultSet, message);
try {
// send message to next processor in the route
getProcessor().process(exchange);
// number of messages polled
return 1;
} finally {
// log exception if an exception occurred and was not handled
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
}
use of com.datastax.driver.core.Session in project camel by apache.
the class CassandraComponentProducerUnpreparedTest method testRequestUriCql.
@Test
public void testRequestUriCql() throws Exception {
Object response = producerTemplate.requestBody(Arrays.asList("w_jiang", "Willem", "Jiang"));
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "w_jiang");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Willem", row.getString("first_name"));
assertEquals("Jiang", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.Session in project camel by apache.
the class CassandraComponentProducerUnpreparedTest method testRequestMessageCql.
@Test
public void testRequestMessageCql() throws Exception {
Object response = producerTemplate.requestBodyAndHeader(new Object[] { "Claus 2", "Ibsen 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, "update camel_user set first_name=?, last_name=? where login=?");
Cluster cluster = CassandraUnitUtils.cassandraCluster();
Session session = cluster.connect(CassandraUnitUtils.KEYSPACE);
ResultSet resultSet = session.execute("select login, first_name, last_name from camel_user where login = ?", "c_ibsen");
Row row = resultSet.one();
assertNotNull(row);
assertEquals("Claus 2", row.getString("first_name"));
assertEquals("Ibsen 2", row.getString("last_name"));
session.close();
cluster.close();
}
use of com.datastax.driver.core.Session in project camel by apache.
the class CassandraProducer method execute.
/**
* Execute CQL query using incoming message body has statement parameters.
*/
private ResultSet execute(Message message) {
Object messageCql = message.getHeader(CassandraConstants.CQL_QUERY);
// Convert Empty string to null
if (messageCql instanceof String && ((String) messageCql).isEmpty()) {
messageCql = null;
}
Object[] cqlParams = getCqlParams(message);
ResultSet resultSet;
Session session = getEndpoint().getSessionHolder().getSession();
if (isPrepareStatements()) {
resultSet = executePreparedStatement(session, messageCql, cqlParams);
} else {
resultSet = executeStatement(session, messageCql, cqlParams);
}
return resultSet;
}
use of com.datastax.driver.core.Session 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);
}
}
Aggregations