Search in sources :

Example 21 with Session

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());
        }
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) ResultSet(com.datastax.driver.core.ResultSet) Session(com.datastax.driver.core.Session)

Example 22 with Session

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();
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 23 with Session

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();
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 24 with Session

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;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Session(com.datastax.driver.core.Session)

Example 25 with Session

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);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

Session (com.datastax.driver.core.Session)30 Cluster (com.datastax.driver.core.Cluster)16 Test (org.junit.Test)16 ResultSet (com.datastax.driver.core.ResultSet)11 Row (com.datastax.driver.core.Row)10 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3 Update (com.datastax.driver.core.querybuilder.Update)3 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)2 IOException (java.io.IOException)2 IntegrationTest (tech.sirwellington.alchemy.annotations.testing.IntegrationTest)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 DataType (com.datastax.driver.core.DataType)1 Host (com.datastax.driver.core.Host)1 IndexMetadata (com.datastax.driver.core.IndexMetadata)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 Metadata (com.datastax.driver.core.Metadata)1 QueryTrace (com.datastax.driver.core.QueryTrace)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1