Search in sources :

Example 6 with Session

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

the class CassandraAggregationSerializedHeadersTest method doPreSetup.

@Override
protected void doPreSetup() throws Exception {
    assumeTrue("Skipping test running in CI server - Fails sometimes on CI server with address already in use", System.getenv("BUILD_ID") == null);
    CassandraUnitUtils.startEmbeddedCassandra();
    cluster = CassandraUnitUtils.cassandraCluster();
    Session rootSession = cluster.connect();
    CassandraUnitUtils.loadCQLDataSet(rootSession, "NamedAggregationDataSet.cql");
    rootSession.close();
    aggregationRepository = new NamedCassandraAggregationRepository(cluster, CassandraUnitUtils.KEYSPACE, "ID");
    aggregationRepository.setTable("NAMED_CAMEL_AGGREGATION");
    aggregationRepository.setAllowSerializedHeaders(true);
    aggregationRepository.start();
    super.doPreSetup();
}
Also used : Session(com.datastax.driver.core.Session)

Example 7 with Session

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

the class CassandraAggregationTest method doPreSetup.

@Override
protected void doPreSetup() throws Exception {
    if (canTest()) {
        CassandraUnitUtils.startEmbeddedCassandra();
        cluster = CassandraUnitUtils.cassandraCluster();
        Session rootSession = cluster.connect();
        CassandraUnitUtils.loadCQLDataSet(rootSession, "NamedAggregationDataSet.cql");
        rootSession.close();
        aggregationRepository = new NamedCassandraAggregationRepository(cluster, CassandraUnitUtils.KEYSPACE, "ID");
        aggregationRepository.setTable("NAMED_CAMEL_AGGREGATION");
        aggregationRepository.start();
    }
    super.doPreSetup();
}
Also used : Session(com.datastax.driver.core.Session)

Example 8 with Session

use of com.datastax.driver.core.Session in project zipkin by openzipkin.

the class DefaultSessionFactory method create.

/**
   * Creates a session and ensures schema if configured. Closes the cluster and session if any
   * exception occurred.
   */
@Override
public Session create(Cassandra3Storage cassandra) {
    Closer closer = Closer.create();
    try {
        Cluster cluster = closer.register(buildCluster(cassandra));
        cluster.register(new QueryLogger.Builder().build());
        Session session;
        if (cassandra.ensureSchema) {
            session = closer.register(cluster.connect());
            Schema.ensureExists(cassandra.keyspace, session);
            session.execute("USE " + cassandra.keyspace);
        } else {
            session = cluster.connect(cassandra.keyspace);
        }
        initializeUDTs(session);
        return session;
    } catch (RuntimeException e) {
        try {
            closer.close();
        } catch (IOException ignored) {
        }
        throw e;
    }
}
Also used : Closer(com.google.common.io.Closer) Cluster(com.datastax.driver.core.Cluster) IOException(java.io.IOException) Session(com.datastax.driver.core.Session)

Example 9 with Session

use of com.datastax.driver.core.Session in project zipkin by openzipkin.

the class DeduplicatingExecutorTest method multithreaded.

/**
   * This shows that any number of threads perform a computation only once.
   */
@Test
public void multithreaded() throws Exception {
    Session session = mock(Session.class);
    DeduplicatingExecutor executor = new DeduplicatingExecutor(session, TimeUnit.SECONDS.toMillis(1L));
    BoundStatement statement = mock(BoundStatement.class);
    when(session.executeAsync(statement)).thenAnswer(invocationOnMock -> mock(ResultSetFuture.class));
    int loopCount = 1000;
    CountDownLatch latch = new CountDownLatch(loopCount);
    ExecutorService exec = Executors.newFixedThreadPool(10);
    Collection<ListenableFuture<?>> futures = new ConcurrentLinkedDeque<>();
    for (int i = 0; i < loopCount; i++) {
        exec.execute(() -> {
            futures.add(executor.maybeExecuteAsync(statement, "foo"));
            futures.add(executor.maybeExecuteAsync(statement, "bar"));
            latch.countDown();
        });
    }
    latch.await();
    ImmutableSet<ListenableFuture<?>> distinctFutures = ImmutableSet.copyOf(futures);
    assertThat(distinctFutures).hasSize(2);
    // expire the result
    Thread.sleep(1000L);
    // Sanity check: we don't memoize after we should have expired.
    assertThat(executor.maybeExecuteAsync(statement, "foo")).isNotIn(distinctFutures);
    assertThat(executor.maybeExecuteAsync(statement, "bar")).isNotIn(distinctFutures);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ExecutorService(java.util.concurrent.ExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CountDownLatch(java.util.concurrent.CountDownLatch) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Test(org.junit.Test)

Example 10 with Session

use of com.datastax.driver.core.Session in project presto by prestodb.

the class NativeCassandraSession method queryPartitionKeys.

protected Iterable<Row> queryPartitionKeys(CassandraTable table, List<Object> filterPrefix) {
    CassandraTableHandle tableHandle = table.getTableHandle();
    List<CassandraColumnHandle> partitionKeyColumns = table.getPartitionKeyColumns();
    if (filterPrefix.size() != partitionKeyColumns.size()) {
        return null;
    }
    Select partitionKeys = CassandraCqlUtils.selectDistinctFrom(tableHandle, partitionKeyColumns);
    addWhereClause(partitionKeys.where(), partitionKeyColumns, filterPrefix);
    return executeWithSession(session -> session.execute(partitionKeys)).all();
}
Also used : QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Iterables(com.google.common.collect.Iterables) Iterables.transform(com.google.common.collect.Iterables.transform) Logger(io.airlift.log.Logger) Row(com.datastax.driver.core.Row) Clause(com.datastax.driver.core.querybuilder.Clause) Supplier(com.google.common.base.Supplier) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Duration(io.airlift.units.Duration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Predicates.in(com.google.common.base.Predicates.in) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableList(com.google.common.collect.ImmutableList) Predicates.not(com.google.common.base.Predicates.not) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Session(com.datastax.driver.core.Session) Objects.requireNonNull(java.util.Objects.requireNonNull) Comparator.comparing(java.util.Comparator.comparing) NullableValue(com.facebook.presto.spi.predicate.NullableValue) Suppliers.memoize(com.google.common.base.Suppliers.memoize) TableMetadata(com.datastax.driver.core.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) Set(java.util.Set) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) IndexMetadata(com.datastax.driver.core.IndexMetadata) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Where(com.datastax.driver.core.querybuilder.Select.Where) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) Ordering(com.google.common.collect.Ordering) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Cluster(com.datastax.driver.core.Cluster) DataType(com.datastax.driver.core.DataType) Host(com.datastax.driver.core.Host) Select(com.datastax.driver.core.querybuilder.Select) CassandraCqlUtils(com.facebook.presto.cassandra.util.CassandraCqlUtils) Iterables.filter(com.google.common.collect.Iterables.filter) ReconnectionSchedule(com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule) JsonCodec(io.airlift.json.JsonCodec) Select(com.datastax.driver.core.querybuilder.Select)

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