Search in sources :

Example 1 with SimpleStatementBuilder

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;
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ArrayList(java.util.ArrayList) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder)

Example 2 with SimpleStatementBuilder

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;
    }
}
Also used : ConnectionClosedException(org.apache.http.ConnectionClosedException) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) NoNodeAvailableException(com.datastax.oss.driver.api.core.NoNodeAvailableException) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 3 with SimpleStatementBuilder

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;
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ArrayList(java.util.ArrayList) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder)

Example 4 with SimpleStatementBuilder

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);
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) Row(com.datastax.oss.driver.api.core.cql.Row) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 5 with SimpleStatementBuilder

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);
    }
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleStatementBuilder(com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder) Token(com.datastax.oss.driver.api.core.metadata.token.Token) Duration(java.time.Duration) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ByteBuffer(java.nio.ByteBuffer) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) DefaultConsistencyLevel(com.datastax.oss.driver.api.core.DefaultConsistencyLevel) ConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel) Function(java.util.function.Function) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test)

Aggregations

SimpleStatementBuilder (com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder)7 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 ArrayList (java.util.ArrayList)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 List (java.util.List)2 Function (java.util.function.Function)2 Test (org.junit.Test)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)1 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)1 BuildableQuery (com.datastax.oss.driver.api.querybuilder.BuildableQuery)1