Search in sources :

Example 1 with Session

use of com.datastax.driver.core.Session in project camel by apache.

the class CassandraEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (cluster == null && beanRef != null) {
        Object bean = CamelContextHelper.mandatoryLookup(getCamelContext(), beanRef);
        if (bean instanceof Session) {
            session = (Session) bean;
            cluster = session.getCluster();
            keyspace = session.getLoggedKeyspace();
        } else if (bean instanceof Cluster) {
            cluster = (Cluster) bean;
            session = null;
        } else {
            throw new IllegalArgumentException("CQL Bean type should be of type Session or Cluster but was " + bean);
        }
    }
    if (cluster == null && hosts != null) {
        // use the cluster builder to create the cluster
        cluster = createClusterBuilder().build();
    }
    if (cluster != null) {
        sessionHolder = new CassandraSessionHolder(cluster, keyspace);
    } else {
        sessionHolder = new CassandraSessionHolder(session);
    }
    sessionHolder.start();
}
Also used : CassandraSessionHolder(org.apache.camel.utils.cassandra.CassandraSessionHolder) Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session)

Example 2 with Session

use of com.datastax.driver.core.Session in project camel by apache.

the class CassandraComponentProducerTest method testRequestMessageStatement.

/**
     * Test with incoming message containing a header with RegularStatement.
     */
@Test
public void testRequestMessageStatement() throws Exception {
    if (!canTest()) {
        return;
    }
    Update.Where update = update("camel_user").with(set("first_name", bindMarker())).and(set("last_name", bindMarker())).where(eq("login", bindMarker()));
    Object response = producerTemplate.requestBodyAndHeader(new Object[] { "Claus 2", "Ibsen 2", "c_ibsen" }, CassandraConstants.CQL_QUERY, update);
    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) Update(com.datastax.driver.core.querybuilder.Update) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 3 with Session

use of com.datastax.driver.core.Session in project camel by apache.

the class CassandraComponentProducerTest method testLoadBalancing.

@Test
public void testLoadBalancing() throws Exception {
    if (!canTest()) {
        return;
    }
    Object response = loadBalancingPolicyTemplate.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 4 with Session

use of com.datastax.driver.core.Session in project camel by apache.

the class CassandraComponentProducerTest method testRequestMessageCql.

@Test
public void testRequestMessageCql() throws Exception {
    if (!canTest()) {
        return;
    }
    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 5 with Session

use of com.datastax.driver.core.Session in project camel by apache.

the class CassandraComponentProducerUnpreparedTest method testRequestMessageStatement.

/**
     * Test with incoming message containing a header with RegularStatement.
     */
@Test
public void testRequestMessageStatement() throws Exception {
    Update.Where update = update("camel_user").with(set("first_name", "Claus 2")).and(set("last_name", "Ibsen 2")).where(eq("login", "c_ibsen"));
    Object response = producerTemplate.requestBodyAndHeader(null, CassandraConstants.CQL_QUERY, update);
    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) Update(com.datastax.driver.core.querybuilder.Update) 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