Search in sources :

Example 31 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForOpenStartBounds.

@Test
public void shouldReturnValuesForOpenStartBounds() {
    // Given:
    final Range<Instant> start = Range.open(NOW, NOW.plusSeconds(10));
    final StateQueryResult partitionResult = new StateQueryResult();
    final QueryResult queryResult = QueryResult.forResult(fetchIterator);
    queryResult.setPosition(POSITION);
    partitionResult.addResult(PARTITION, queryResult);
    when(kafkaStreams.query(any())).thenReturn(partitionResult);
    when(fetchIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    when(fetchIterator.next()).thenReturn(new KeyValue<>(start.lowerEndpoint().toEpochMilli(), VALUE_1)).thenReturn(new KeyValue<>(start.lowerEndpoint().plusMillis(1).toEpochMilli(), VALUE_2)).thenReturn(new KeyValue<>(start.upperEndpoint().toEpochMilli(), VALUE_3)).thenThrow(new AssertionError());
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(A_KEY, PARTITION, start, Range.all());
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(start.lowerEndpoint().plusMillis(1)), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) QueryResult(org.apache.kafka.streams.query.QueryResult) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 32 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2Test method shouldReturnValuesForClosedStartBounds_fetchAll.

@Test
public void shouldReturnValuesForClosedStartBounds_fetchAll() {
    // Given:
    final Range<Instant> start = Range.closed(Instant.ofEpochMilli(System.currentTimeMillis()), NOW.plusSeconds(10));
    final StateQueryResult partitionResult = new StateQueryResult();
    final QueryResult queryResult = QueryResult.forResult(keyValueIterator);
    queryResult.setPosition(POSITION);
    partitionResult.addResult(PARTITION, queryResult);
    when(kafkaStreams.query(any())).thenReturn(partitionResult);
    when(keyValueIterator.hasNext()).thenReturn(true, true, false);
    when(keyValueIterator.next()).thenReturn(new KeyValue<>(new Windowed<>(A_KEY, new TimeWindow(start.lowerEndpoint().toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(start.upperEndpoint().toEpochMilli(), start.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_2)).thenThrow(new AssertionError());
    // When:
    final KsMaterializedQueryResult<WindowedRow> result = table.get(PARTITION, start, Range.all());
    // Then:
    final Iterator<WindowedRow> rowIterator = result.getRowIterator();
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY, start.lowerEndpoint()), VALUE_1.value(), VALUE_1.timestamp())));
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, start.upperEndpoint()), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(rowIterator.hasNext(), is(false));
    assertThat(result.getPosition(), not(Optional.empty()));
    assertThat(result.getPosition().get(), is(POSITION));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Windowed(org.apache.kafka.streams.kstream.Windowed) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) QueryResult(org.apache.kafka.streams.query.QueryResult) StateQueryResult(org.apache.kafka.streams.query.StateQueryResult) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 33 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTableTest method shouldReturnValuesForClosedStartBounds_fetchAll.

@Test
public void shouldReturnValuesForClosedStartBounds_fetchAll() {
    // Given:
    final Range<Instant> start = Range.closed(NOW, NOW.plusSeconds(10));
    when(keyValueIterator.hasNext()).thenReturn(true, true, false);
    when(keyValueIterator.next()).thenReturn(new KeyValue<>(new Windowed<>(A_KEY, new TimeWindow(start.lowerEndpoint().toEpochMilli(), start.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(start.upperEndpoint().toEpochMilli(), start.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_2)).thenThrow(new AssertionError());
    // When:
    final Iterator<WindowedRow> rowIterator = table.get(PARTITION, start, Range.all()).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(start.lowerEndpoint()), VALUE_1.value(), VALUE_1.timestamp())));
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, start.upperEndpoint()), VALUE_2.value(), VALUE_2.timestamp())));
    assertThat(rowIterator.hasNext(), is(false));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 34 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTable method get.

public KsMaterializedQueryResult<WindowedRow> get(final int partition, final Range<Instant> windowStartBounds, final Range<Instant> windowEndBounds, final Optional<Position> position) {
    try {
        final ReadOnlyWindowStore<GenericKey, ValueAndTimestamp<GenericRow>> store = stateStore.store(QueryableStoreTypes.timestampedWindowStore(), partition);
        final Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds);
        final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds);
        final KeyValueIterator<Windowed<GenericKey>, ValueAndTimestamp<GenericRow>> iterator = cacheBypassFetcherAll.fetchAll(store, lower, upper);
        return KsMaterializedQueryResult.rowIterator(Streams.stream(IteratorUtil.onComplete(iterator, iterator::close)).map(next -> {
            final Instant windowStart = next.key.window().startTime();
            if (!windowStartBounds.contains(windowStart)) {
                return null;
            }
            final Instant windowEnd = next.key.window().endTime();
            if (!windowEndBounds.contains(windowEnd)) {
                return null;
            }
            final TimeWindow window = new TimeWindow(windowStart.toEpochMilli(), windowEnd.toEpochMilli());
            final WindowedRow row = WindowedRow.of(stateStore.schema(), new Windowed<>(next.key.key(), window), next.value.value(), next.value.timestamp());
            return row;
        }).filter(Objects::nonNull).iterator());
    } catch (final Exception e) {
        throw new MaterializationException("Failed to scan materialized table", e);
    }
}
Also used : ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) Windowed(org.apache.kafka.streams.kstream.Windowed) ReadOnlyWindowStore(org.apache.kafka.streams.state.ReadOnlyWindowStore) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) WindowStoreCacheBypassFetcherAll(io.confluent.ksql.execution.streams.materialization.ks.WindowStoreCacheBypass.WindowStoreCacheBypassFetcherAll) IteratorUtil(io.confluent.ksql.util.IteratorUtil) ImmutableList(com.google.common.collect.ImmutableList) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Windowed(org.apache.kafka.streams.kstream.Windowed) Duration(java.time.Duration) WindowStoreCacheBypassFetcherRange(io.confluent.ksql.execution.streams.materialization.ks.WindowStoreCacheBypass.WindowStoreCacheBypassFetcherRange) Position(org.apache.kafka.streams.query.Position) Range(com.google.common.collect.Range) KeyValue(org.apache.kafka.streams.KeyValue) MaterializedWindowedTable(io.confluent.ksql.execution.streams.materialization.MaterializedWindowedTable) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) StreamsMaterializedWindowedTable(io.confluent.ksql.execution.streams.materialization.StreamsMaterializedWindowedTable) QueryableStoreTypes(org.apache.kafka.streams.state.QueryableStoreTypes) Objects(java.util.Objects) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) WindowStoreIterator(org.apache.kafka.streams.state.WindowStoreIterator) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) GenericKey(io.confluent.ksql.GenericKey) Builder(com.google.common.collect.ImmutableList.Builder) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) WindowStoreCacheBypassFetcher(io.confluent.ksql.execution.streams.materialization.ks.WindowStoreCacheBypass.WindowStoreCacheBypassFetcher) Instant(java.time.Instant) Objects(java.util.Objects) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException)

Example 35 with WindowedRow

use of io.confluent.ksql.execution.streams.materialization.WindowedRow in project ksql by confluentinc.

the class KsMaterializedWindowTable method get.

@Override
public KsMaterializedQueryResult<WindowedRow> get(final GenericKey key, final int partition, final Range<Instant> windowStartBounds, final Range<Instant> windowEndBounds, final Optional<Position> position) {
    try {
        final ReadOnlyWindowStore<GenericKey, ValueAndTimestamp<GenericRow>> store = stateStore.store(QueryableStoreTypes.timestampedWindowStore(), partition);
        final Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds);
        final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds);
        try (WindowStoreIterator<ValueAndTimestamp<GenericRow>> it = cacheBypassFetcher.fetch(store, key, lower, upper)) {
            final Builder<WindowedRow> builder = ImmutableList.builder();
            while (it.hasNext()) {
                final KeyValue<Long, ValueAndTimestamp<GenericRow>> next = it.next();
                final Instant windowStart = Instant.ofEpochMilli(next.key);
                if (!windowStartBounds.contains(windowStart)) {
                    continue;
                }
                final Instant windowEnd = windowStart.plus(windowSize);
                if (!windowEndBounds.contains(windowEnd)) {
                    continue;
                }
                final TimeWindow window = new TimeWindow(windowStart.toEpochMilli(), windowEnd.toEpochMilli());
                final WindowedRow row = WindowedRow.of(stateStore.schema(), new Windowed<>(key, window), next.value.value(), next.value.timestamp());
                builder.add(row);
            }
            return KsMaterializedQueryResult.rowIterator(builder.build().iterator());
        }
    } catch (final Exception e) {
        throw new MaterializationException("Failed to get value from materialized table", e);
    }
}
Also used : Instant(java.time.Instant) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Aggregations

WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)42 Test (org.junit.Test)33 Instant (java.time.Instant)32 KeyValue (org.apache.kafka.streams.KeyValue)16 Windowed (org.apache.kafka.streams.kstream.Windowed)15 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)12 QueryResult (org.apache.kafka.streams.query.QueryResult)12 StateQueryResult (org.apache.kafka.streams.query.StateQueryResult)12 GenericKey (io.confluent.ksql.GenericKey)9 GenericRow (io.confluent.ksql.GenericRow)8 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)5 MaterializedWindowedTable (io.confluent.ksql.execution.streams.materialization.MaterializedWindowedTable)5 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)4 Window (io.confluent.ksql.Window)3 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)3 Optional (java.util.Optional)3 KeyValueIterator (org.apache.kafka.streams.state.KeyValueIterator)3 WindowStoreIterator (org.apache.kafka.streams.state.WindowStoreIterator)3