Search in sources :

Example 61 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project cdc-apache-cassandra by datastax.

the class CassandraClient method executeWithDowngradeConsistencyRetry.

CompletionStage<Tuple2<AsyncResultSet, ConsistencyLevel>> executeWithDowngradeConsistencyRetry(CqlSession cqlSession, BoundStatement boundStatement, List<ConsistencyLevel> consistencyLevels) {
    final ConsistencyLevel cl = consistencyLevels.remove(0);
    final BoundStatement statement = boundStatement.setConsistencyLevel(cl);
    log.debug("Trying with CL={} statement={}", cl, statement.getPreparedStatement().getQuery());
    final CompletionStage<Tuple2<AsyncResultSet, ConsistencyLevel>> completionStage = cqlSession.executeAsync(statement).thenApply(rx -> new Tuple2<>(rx, cl));
    return completionStage.handle((r, ex) -> {
        if (ex == null || !(ex instanceof UnavailableException) || consistencyLevels.isEmpty()) {
            log.debug("Executed CL={} statement={}", cl, statement.getPreparedStatement().getQuery());
            return completionStage;
        }
        return completionStage.handleAsync((r1, ex1) -> executeWithDowngradeConsistencyRetry(cqlSession, statement, consistencyLevels)).thenCompose(Function.identity());
    }).thenCompose(Function.identity());
}
Also used : ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) SessionBuilder(com.datastax.oss.common.sink.ssl.SessionBuilder) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder) AUTH_PROVIDER_SERVICE(com.datastax.dse.driver.api.core.config.DseDriverOption.AUTH_PROVIDER_SERVICE) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) NonNull(edu.umd.cs.findbugs.annotations.NonNull) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) AuthenticatorConfig(com.datastax.oss.common.sink.config.AuthenticatorConfig) Path(java.nio.file.Path) Select(com.datastax.oss.driver.api.querybuilder.select.Select) DseDriverOption(com.datastax.dse.driver.api.core.config.DseDriverOption) SslConfig(com.datastax.oss.common.sink.config.SslConfig) TypedDriverOption(com.datastax.oss.driver.api.core.config.TypedDriverOption) QueryBuilder.bindMarker(com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) QueryBuilder.selectFrom(com.datastax.oss.driver.api.querybuilder.QueryBuilder.selectFrom) InetSocketAddress(java.net.InetSocketAddress) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Slf4j(lombok.extern.slf4j.Slf4j) CompletionStage(java.util.concurrent.CompletionStage) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) PlainTextAuthProvider(com.datastax.oss.driver.internal.core.auth.PlainTextAuthProvider) Node(com.datastax.oss.driver.api.core.metadata.Node) DseGssApiAuthProvider(com.datastax.dse.driver.internal.core.auth.DseGssApiAuthProvider) java.util(java.util) Getter(lombok.Getter) Function(java.util.function.Function) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) AUTH_PROVIDER_SASL_PROPERTIES(com.datastax.dse.driver.api.core.config.DseDriverOption.AUTH_PROVIDER_SASL_PROPERTIES) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ConfigFactory(com.typesafe.config.ConfigFactory) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) Row(com.datastax.oss.driver.api.core.cql.Row) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) NodeState(com.datastax.oss.driver.api.core.metadata.NodeState) Config(com.typesafe.config.Config) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) ContactPointsValidator(com.datastax.oss.common.sink.config.ContactPointsValidator) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) DefaultDriverConfigLoader(com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader) DefaultProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder) ExecutionException(java.util.concurrent.ExecutionException) Tuple2(io.vavr.Tuple2) SchemaChangeListener(com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener) Tuple3(io.vavr.Tuple3) UUIDUtil.generateClientId(com.datastax.oss.common.sink.util.UUIDUtil.generateClientId) Tuple2(io.vavr.Tuple2) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 62 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project stream-applications by spring-cloud.

the class CassandraAppClusterConfiguration method keyspaceCreator.

@Bean
@ConditionalOnProperty("cassandra.cluster.create-keyspace")
public Object keyspaceCreator(CassandraProperties cassandraProperties, CqlSessionBuilder cqlSessionBuilder) {
    CreateKeyspaceSpecification createKeyspaceSpecification = CreateKeyspaceSpecification.createKeyspace(cassandraProperties.getKeyspaceName()).withSimpleReplication().ifNotExists();
    String createKeySpaceQuery = new CreateKeyspaceCqlGenerator(createKeyspaceSpecification).toCql();
    CqlSession systemSession = cqlSessionBuilder.withKeyspace(CqlSessionFactoryBean.CASSANDRA_SYSTEM_SESSION).build();
    CqlTemplate template = new CqlTemplate(systemSession);
    template.execute(createKeySpaceQuery);
    return null;
}
Also used : CqlTemplate(org.springframework.data.cassandra.core.cql.CqlTemplate) CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CreateKeyspaceCqlGenerator(org.springframework.data.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator) Bean(org.springframework.context.annotation.Bean) CqlSessionFactoryBean(org.springframework.data.cassandra.config.CqlSessionFactoryBean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 63 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project jnosql-communication-driver by eclipse.

the class QueryUtils method insertUDT.

private static void insertUDT(UDT udt, String keyspace, String columnFamily, CqlSession session, Map<String, Term> values) {
    final Optional<KeyspaceMetadata> keyspaceMetadata = session.getMetadata().getKeyspace(keyspace);
    UserDefinedType userType = keyspaceMetadata.flatMap(ks -> ks.getUserDefinedType(udt.getUserType())).orElseThrow(() -> new IllegalArgumentException("Missing UDT definition"));
    final TableMetadata tableMetadata = keyspaceMetadata.flatMap(k -> k.getTable(columnFamily)).orElseThrow(() -> new IllegalArgumentException("Missing Table definition"));
    final ColumnMetadata columnMetadata = tableMetadata.getColumn(getName(udt)).orElseThrow(() -> new IllegalArgumentException("Missing the column definition"));
    final DataType type = columnMetadata.getType();
    Iterable elements = Iterable.class.cast(udt.get());
    Object udtValue = getUdtValue(userType, elements, type);
    values.put(getName(udt), QueryBuilder.literal(udtValue));
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) SortType(jakarta.nosql.SortType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) HashMap(java.util.HashMap) Function(java.util.function.Function) ValueUtil(org.eclipse.jnosql.communication.driver.ValueUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Duration(java.time.Duration) Map(java.util.Map) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) Select(com.datastax.oss.driver.api.querybuilder.select.Select) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnEntity(jakarta.nosql.column.ColumnEntity) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) Collection(java.util.Collection) DataType(com.datastax.oss.driver.api.core.type.DataType) Collectors(java.util.stream.Collectors) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) ProtocolConstants(com.datastax.oss.protocol.internal.ProtocolConstants) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Value(jakarta.nosql.Value) List(java.util.List) Column(jakarta.nosql.column.Column) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) Optional(java.util.Optional) SetType(com.datastax.oss.driver.api.core.type.SetType) Term(com.datastax.oss.driver.api.querybuilder.term.Term) ColumnQuery(jakarta.nosql.column.ColumnQuery) Sort(jakarta.nosql.Sort) CommunicationException(jakarta.nosql.CommunicationException) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)

Example 64 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project jnosql-communication-driver by eclipse.

the class CassandraColumnFamilyManagerTest method shouldClose.

@Test
public void shouldClose() throws Exception {
    entityManager.close();
    DefaultCassandraColumnFamilyManager cassandraColumnFamilyManager = DefaultCassandraColumnFamilyManager.class.cast(entityManager);
    CqlSession session = cassandraColumnFamilyManager.getSession();
    assertTrue(session.isClosed());
}
Also used : CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.jupiter.api.Test)

Example 65 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project zipkin by openzipkin.

the class CassandraStorageExtension method tryToInitializeSession.

// Builds a session without trying to use a namespace or init UDTs
static CqlSession tryToInitializeSession(String contactPoint) {
    CassandraStorage storage = newStorageBuilder(contactPoint).build();
    CqlSession session = null;
    try {
        session = DefaultSessionFactory.buildSession(storage);
        session.execute("SELECT now() FROM system.local");
    } catch (Throwable e) {
        propagateIfFatal(e);
        if (session != null)
            session.close();
        assumeTrue(false, e.getMessage());
    }
    return session;
}
Also used : CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)577 Test (org.junit.Test)286 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)118 Row (com.datastax.oss.driver.api.core.cql.Row)98 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)82 Test (org.junit.jupiter.api.Test)78 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)65 List (java.util.List)54 Map (java.util.Map)52 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)51 Node (com.datastax.oss.driver.api.core.metadata.Node)50 ArrayList (java.util.ArrayList)49 BeforeClass (org.junit.BeforeClass)43 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)41 HashMap (java.util.HashMap)37 UUID (java.util.UUID)36 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)30 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)30 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)27