use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project ff4j by ff4j.
the class EventRepositoryCassandra method buildDynamicStatementAudit.
private SimpleStatement buildDynamicStatementAudit(EventQueryDefinition query) {
StringBuilder sb = new StringBuilder();
sb.append("SELECT * FROM " + AUDIT_TABLE);
sb.append(" WHERE (" + AUDIT_ATT_TIME + "> ?) ");
sb.append(" AND (" + AUDIT_ATT_TIME + "< ?) ");
List<Object> parameters = new ArrayList<>();
parameters.add(Instant.ofEpochMilli(query.getFrom()));
parameters.add(Instant.ofEpochMilli(query.getTo()));
// Name is the PARTITION KEY
if (!query.getNamesFilter().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_NAME + " IN ?)");
parameters.add(query.getNamesFilter());
}
if (!query.getActionFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_ACTION + " IN ?)");
parameters.add(query.getActionFilters());
}
if (!query.getHostFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_HOSTNAME + " IN ?)");
parameters.add(query.getHostFilters());
}
if (!query.getSourceFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_SOURCE + " IN ? )");
parameters.add(query.getSourceFilters());
}
sb.append(" ALLOW FILTERING");
SimpleStatementBuilder builder = SimpleStatement.builder(sb.toString());
for (Object o : parameters) {
builder = builder.addPositionalValue(o);
}
// Accelerate the query
SimpleStatement ss = builder.build();
ss.setConsistencyLevel(ConsistencyLevel.ONE);
ss.setTimeout(Duration.ofMinutes(10));
ss.setTracing(false);
return ss;
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project management-api-for-apache-cassandra by k8ssandra.
the class CqlService method executePreparedStatement.
public ResultSet executePreparedStatement(File cassandraUnixSocketFile, String query, Object... values) throws ConnectionClosedException {
CqlSession session = UnixSocketCQLAccess.get(cassandraUnixSocketFile).orElse(null);
if (session == null || session.isClosed()) {
throw new ConnectionClosedException("Internal connection to Cassandra closed");
}
SimpleStatementBuilder ssb = new SimpleStatementBuilder(query);
for (Object obj : values) {
ssb = ssb.addPositionalValue(obj);
}
try {
return session.execute(ssb.build());
} catch (NoNodeAvailableException e) {
try {
session.close();
} catch (Throwable t) {
// close quietly
}
throw e;
}
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project ff4j by ff4j.
the class EventRepositoryCassandra method buildDynamicStatementHitCount.
private SimpleStatement buildDynamicStatementHitCount(EventQueryDefinition query) {
// Default settings
StringBuilder sb = new StringBuilder();
sb.append("SELECT * FROM " + AUDIT_HITCOUNT_TABLE);
sb.append(" WHERE (" + AUDIT_ATT_TIME + "> ?) ");
sb.append(" AND (" + AUDIT_ATT_TIME + "< ?) ");
List<Object> parameters = new ArrayList<>();
parameters.add(Instant.ofEpochMilli(query.getFrom()));
parameters.add(Instant.ofEpochMilli(query.getTo()));
// Name is the PARTITION KEY
if (!query.getNamesFilter().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_NAME + " IN ?)");
parameters.add(query.getNamesFilter());
}
if (!query.getActionFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_ACTION + " IN ?)");
parameters.add(query.getActionFilters());
}
if (!query.getHostFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_HOSTNAME + " IN ?)");
parameters.add(query.getHostFilters());
}
if (!query.getSourceFilters().isEmpty()) {
sb.append(" AND (" + AUDIT_ATT_SOURCE + " IN ? )");
parameters.add(query.getSourceFilters());
}
sb.append(" ALLOW FILTERING");
SimpleStatementBuilder builder = SimpleStatement.builder(sb.toString());
for (Object o : parameters) {
builder = builder.addPositionalValue(o);
}
// Accelerate the query
SimpleStatement ss = builder.build();
ss.setConsistencyLevel(ConsistencyLevel.ONE);
ss.setTimeout(Duration.ofMinutes(10));
ss.setTracing(false);
return ss;
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project ff4j by ff4j.
the class EventRepositoryCassandra method getEventByUUID.
/**
* {@inheritDoc}
*/
@Override
public Event getEventByUUID(String uuid, Long timestamp) {
Util.assertHasLength(uuid);
BoundStatement stmtFindEventById = psReadEventById.bind(UUID.fromString(uuid));
ResultSet rs = getCqlSession().execute(stmtFindEventById);
Row row = rs.one();
if (null == row) {
/*
* Was not in table audit, so maybe hitcount but need allow filtering
* It will be slow no need to prepare :p (full scan)
*/
SimpleStatement ss = new SimpleStatementBuilder("SELECT * FROM " + AUDIT_HITCOUNT_TABLE + " WHERE " + AUDIT_ATT_UID + "= ?" + " ALLOW FILTERING").addPositionalValue(UUID.fromString(uuid)).build();
ResultSet rs2 = getCqlSession().execute(ss);
Row row2 = rs2.one();
if (null == row2) {
return null;
}
return mapEventHit(row2);
}
return mapEventRow(row);
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder in project java-driver by datastax.
the class BoundStatementCcmIT method should_propagate_attributes_when_preparing_a_simple_statement.
@Test
public void should_propagate_attributes_when_preparing_a_simple_statement() {
CqlSession session = sessionRule.session();
DriverExecutionProfile mockProfile = session.getContext().getConfig().getDefaultProfile().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(10));
ByteBuffer mockPagingState = Bytes.fromHexString("0xaaaa");
CqlIdentifier mockKeyspace = supportsPerRequestKeyspace(session) ? CqlIdentifier.fromCql("system") : null;
CqlIdentifier mockRoutingKeyspace = CqlIdentifier.fromCql("mockRoutingKeyspace");
ByteBuffer mockRoutingKey = Bytes.fromHexString("0xbbbb");
Token mockRoutingToken = session.getMetadata().getTokenMap().get().newToken(mockRoutingKey);
Map<String, ByteBuffer> mockCustomPayload = NullAllowingImmutableMap.of("key1", Bytes.fromHexString("0xcccc"));
Duration mockTimeout = Duration.ofSeconds(1);
ConsistencyLevel mockCl = DefaultConsistencyLevel.LOCAL_QUORUM;
ConsistencyLevel mockSerialCl = DefaultConsistencyLevel.LOCAL_SERIAL;
int mockPageSize = 2000;
SimpleStatementBuilder simpleStatementBuilder = SimpleStatement.builder("SELECT release_version FROM system.local").setExecutionProfile(mockProfile).setPagingState(mockPagingState).setKeyspace(mockKeyspace).setRoutingKeyspace(mockRoutingKeyspace).setRoutingKey(mockRoutingKey).setRoutingToken(mockRoutingToken).setQueryTimestamp(42).setIdempotence(true).setTracing().setTimeout(mockTimeout).setConsistencyLevel(mockCl).setSerialConsistencyLevel(mockSerialCl).setPageSize(mockPageSize);
if (atLeastV4) {
simpleStatementBuilder = simpleStatementBuilder.addCustomPayload("key1", mockCustomPayload.get("key1"));
}
PreparedStatement preparedStatement = session.prepare(simpleStatementBuilder.build());
// Cover all the ways to create bound statements:
ImmutableList<Function<PreparedStatement, BoundStatement>> createMethods = ImmutableList.of(PreparedStatement::bind, p -> p.boundStatementBuilder().build());
for (Function<PreparedStatement, BoundStatement> createMethod : createMethods) {
BoundStatement boundStatement = createMethod.apply(preparedStatement);
assertThat(boundStatement.getExecutionProfile()).isEqualTo(mockProfile);
assertThat(boundStatement.getPagingState()).isEqualTo(mockPagingState);
assertThat(boundStatement.getRoutingKeyspace()).isEqualTo(mockKeyspace != null ? mockKeyspace : mockRoutingKeyspace);
assertThat(boundStatement.getRoutingKey()).isEqualTo(mockRoutingKey);
assertThat(boundStatement.getRoutingToken()).isEqualTo(mockRoutingToken);
if (atLeastV4) {
assertThat(boundStatement.getCustomPayload()).isEqualTo(mockCustomPayload);
}
assertThat(boundStatement.isIdempotent()).isTrue();
assertThat(boundStatement.isTracing()).isTrue();
assertThat(boundStatement.getTimeout()).isEqualTo(mockTimeout);
assertThat(boundStatement.getConsistencyLevel()).isEqualTo(mockCl);
assertThat(boundStatement.getSerialConsistencyLevel()).isEqualTo(mockSerialCl);
assertThat(boundStatement.getPageSize()).isEqualTo(mockPageSize);
// Bound statements do not support per-query keyspaces, so this is not set
assertThat(boundStatement.getKeyspace()).isNull();
// Should not be propagated
assertThat(boundStatement.getQueryTimestamp()).isEqualTo(Statement.NO_DEFAULT_TIMESTAMP);
}
}
Aggregations