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());
}
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;
}
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));
}
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());
}
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;
}
Aggregations