Search in sources :

Example 91 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class SessionStoreCacheBypass method fetchRangeUncached.

/*
  This method is used for range queries. It is invoked by the fetchRange method
   */
private static KeyValueIterator<Windowed<GenericKey>, GenericRow> fetchRangeUncached(final ReadOnlySessionStore<GenericKey, GenericRow> sessionStore, final GenericKey keyFrom, final GenericKey keyTo) {
    if (!(sessionStore instanceof MeteredSessionStore)) {
        throw new IllegalStateException("Expecting a MeteredSessionStore");
    } else {
        final StateSerdes<GenericKey, GenericRow> serdes = getSerdes(sessionStore);
        final Bytes rawKeyFrom = Bytes.wrap(serdes.rawKey(keyFrom));
        final Bytes rawKeyTo = Bytes.wrap(serdes.rawKey(keyTo));
        final SessionStore<Bytes, byte[]> wrapped = getInnermostStore(sessionStore);
        final KeyValueIterator<Windowed<Bytes>, byte[]> fetch = wrapped.fetch(rawKeyFrom, rawKeyTo);
        return new DeserializingIterator(fetch, serdes);
    }
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Windowed(org.apache.kafka.streams.kstream.Windowed) MeteredSessionStore(org.apache.kafka.streams.state.internals.MeteredSessionStore) Bytes(org.apache.kafka.common.utils.Bytes) GenericKey(io.confluent.ksql.GenericKey)

Example 92 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class SessionStoreCacheBypass method fetchUncached.

/*
  This method is used for single key lookups. It is invoked by the fetch method
   */
private static KeyValueIterator<Windowed<GenericKey>, GenericRow> fetchUncached(final ReadOnlySessionStore<GenericKey, GenericRow> sessionStore, final GenericKey key) {
    if (!(sessionStore instanceof MeteredSessionStore)) {
        throw new IllegalStateException("Expecting a MeteredSessionStore");
    } else {
        final StateSerdes<GenericKey, GenericRow> serdes = getSerdes(sessionStore);
        final Bytes rawKey = Bytes.wrap(serdes.rawKey(key));
        final SessionStore<Bytes, byte[]> wrapped = getInnermostStore(sessionStore);
        final KeyValueIterator<Windowed<Bytes>, byte[]> fetch = wrapped.fetch(rawKey);
        return new DeserializingIterator(fetch, serdes);
    }
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Windowed(org.apache.kafka.streams.kstream.Windowed) MeteredSessionStore(org.apache.kafka.streams.state.internals.MeteredSessionStore) Bytes(org.apache.kafka.common.utils.Bytes) GenericKey(io.confluent.ksql.GenericKey)

Example 93 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class SourceBuilder method buildTableMaterialized.

@Override
Materialized<GenericKey, GenericRow, KeyValueStore<Bytes, byte[]>> buildTableMaterialized(final SourceStep<KTableHolder<GenericKey>> source, final RuntimeBuildContext buildContext, final MaterializedFactory materializedFactory, final Serde<GenericKey> sourceKeySerde, final Serde<GenericRow> sourceValueSerde, final String stateStoreName) {
    final PhysicalSchema physicalSchema = getPhysicalSchemaWithPseudoColumnsToMaterialize(source);
    final QueryContext queryContext = addMaterializedContext(source);
    final Serde<GenericRow> valueSerdeToMaterialize = getValueSerde(buildContext, source, physicalSchema, queryContext);
    final Serde<GenericKey> keySerdeToMaterialize = getKeySerde(source, physicalSchema, buildContext, queryContext);
    return materializedFactory.create(keySerdeToMaterialize, valueSerdeToMaterialize, stateStoreName);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) QueryContext(io.confluent.ksql.execution.context.QueryContext) GenericKey(io.confluent.ksql.GenericKey)

Example 94 with GenericKey

use of io.confluent.ksql.GenericKey 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 95 with GenericKey

use of io.confluent.ksql.GenericKey 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

GenericKey (io.confluent.ksql.GenericKey)147 GenericRow (io.confluent.ksql.GenericRow)100 Test (org.junit.Test)93 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)24 Windowed (org.apache.kafka.streams.kstream.Windowed)20 WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)14 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)14 Materialized (org.apache.kafka.streams.kstream.Materialized)13 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)13 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)12 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)9 IntegrationTest (io.confluent.common.utils.IntegrationTest)8 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)8 Row (io.confluent.ksql.execution.streams.materialization.Row)8 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)8 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)8 IntegrationTest (org.apache.kafka.test.IntegrationTest)8 InOrder (org.mockito.InOrder)8 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)7 Objects (java.util.Objects)7