Search in sources :

Example 6 with RangeQuery

use of org.apache.kafka.streams.query.RangeQuery in project ksql by confluentinc.

the class KsMaterializedTableIQv2 method get.

@Override
public KsMaterializedQueryResult<Row> get(final int partition, final Optional<Position> position) {
    try {
        final RangeQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = RangeQuery.withNoBounds();
        StateQueryRequest<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query).withPartitions(ImmutableSet.of(partition));
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        } else if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        } else {
            final KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>> iterator = queryResult.getResult();
            return KsMaterializedQueryResult.rowIteratorWithPosition(Streams.stream(IteratorUtil.onComplete(iterator, iterator::close)).map(keyValue -> Row.of(stateStore.schema(), keyValue.key, keyValue.value.value(), keyValue.value.timestamp())).iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to scan materialized table", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) StateQueryRequest.inStore(org.apache.kafka.streams.query.StateQueryRequest.inStore) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) Position(org.apache.kafka.streams.query.Position) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) Row(io.confluent.ksql.execution.streams.materialization.Row) FailureReason(org.apache.kafka.streams.query.FailureReason) PositionBound(org.apache.kafka.streams.query.PositionBound) RangeQuery(org.apache.kafka.streams.query.RangeQuery) Streams(com.google.common.collect.Streams) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StreamsMaterializedTable(io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable) Objects(java.util.Objects) StateQueryRequest(org.apache.kafka.streams.query.StateQueryRequest) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) IteratorUtil(io.confluent.ksql.util.IteratorUtil) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) ImmutableList(com.google.common.collect.ImmutableList) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) KeyQuery(org.apache.kafka.streams.query.KeyQuery) Collections(java.util.Collections) QueryResult(org.apache.kafka.streams.query.QueryResult) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericRow(io.confluent.ksql.GenericRow) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey)

Example 7 with RangeQuery

use of org.apache.kafka.streams.query.RangeQuery in project ksql by confluentinc.

the class KsMaterializedTableIQv2 method get.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
@Override
public KsMaterializedQueryResult<Row> get(final int partition, final GenericKey from, final GenericKey to, final Optional<Position> position) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    try {
        final RangeQuery<GenericKey, ValueAndTimestamp<GenericRow>> query;
        if (from != null && to != null) {
            query = RangeQuery.withRange(from, to);
        } else if (from == null && to != null) {
            query = RangeQuery.withUpperBound(to);
        } else if (from != null && to == null) {
            query = RangeQuery.withLowerBound(from);
        } else {
            query = RangeQuery.withNoBounds();
        }
        StateQueryRequest<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query).withPartitions(ImmutableSet.of(partition));
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        } else if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        } else {
            final KeyValueIterator<GenericKey, ValueAndTimestamp<GenericRow>> iterator = queryResult.getResult();
            return KsMaterializedQueryResult.rowIteratorWithPosition(Streams.stream(IteratorUtil.onComplete(iterator, iterator::close)).map(keyValue -> Row.of(stateStore.schema(), keyValue.key, keyValue.value.value(), keyValue.value.timestamp())).iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to range scan materialized table", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) StateQueryRequest.inStore(org.apache.kafka.streams.query.StateQueryRequest.inStore) MaterializedTable(io.confluent.ksql.execution.streams.materialization.MaterializedTable) Position(org.apache.kafka.streams.query.Position) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) Row(io.confluent.ksql.execution.streams.materialization.Row) FailureReason(org.apache.kafka.streams.query.FailureReason) PositionBound(org.apache.kafka.streams.query.PositionBound) RangeQuery(org.apache.kafka.streams.query.RangeQuery) Streams(com.google.common.collect.Streams) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) StreamsMaterializedTable(io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable) Objects(java.util.Objects) StateQueryRequest(org.apache.kafka.streams.query.StateQueryRequest) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) IteratorUtil(io.confluent.ksql.util.IteratorUtil) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) ImmutableList(com.google.common.collect.ImmutableList) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) KeyQuery(org.apache.kafka.streams.query.KeyQuery) Collections(java.util.Collections) QueryResult(org.apache.kafka.streams.query.QueryResult) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericRow(io.confluent.ksql.GenericRow) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey)

Aggregations

RangeQuery (org.apache.kafka.streams.query.RangeQuery)7 StateQueryRequest (org.apache.kafka.streams.query.StateQueryRequest)7 Test (org.junit.Test)5 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Streams (com.google.common.collect.Streams)2 GenericKey (io.confluent.ksql.GenericKey)2 GenericRow (io.confluent.ksql.GenericRow)2 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)2 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)2 Row (io.confluent.ksql.execution.streams.materialization.Row)2 StreamsMaterializedTable (io.confluent.ksql.execution.streams.materialization.StreamsMaterializedTable)2 IteratorUtil (io.confluent.ksql.util.IteratorUtil)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 FailureReason (org.apache.kafka.streams.query.FailureReason)2 KeyQuery (org.apache.kafka.streams.query.KeyQuery)2 Position (org.apache.kafka.streams.query.Position)2 PositionBound (org.apache.kafka.streams.query.PositionBound)2