Search in sources :

Example 11 with WindowedRow

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

the class KsMaterializedSessionTableIQv2 method get.

@Override
public KsMaterializedQueryResult<WindowedRow> get(final GenericKey key, final int partition, final Range<Instant> windowStart, final Range<Instant> windowEnd, final Optional<Position> position) {
    try {
        final WindowRangeQuery<GenericKey, GenericRow> query = WindowRangeQuery.withKey(key);
        StateQueryRequest<KeyValueIterator<Windowed<GenericKey>, GenericRow>> request = inStore(stateStore.getStateStoreName()).withQuery(query);
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<Windowed<GenericKey>, GenericRow>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<Windowed<GenericKey>, GenericRow>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        }
        try (KeyValueIterator<Windowed<GenericKey>, GenericRow> it = queryResult.getResult()) {
            final Builder<WindowedRow> builder = ImmutableList.builder();
            while (it.hasNext()) {
                final KeyValue<Windowed<GenericKey>, GenericRow> next = it.next();
                final Window wnd = next.key.window();
                if (!windowStart.contains(wnd.startTime())) {
                    continue;
                }
                if (!windowEnd.contains(wnd.endTime())) {
                    continue;
                }
                final long rowTime = wnd.end();
                final WindowedRow row = WindowedRow.of(stateStore.schema(), next.key, next.value, rowTime);
                builder.add(row);
            }
            return KsMaterializedQueryResult.rowIteratorWithPosition(builder.build().iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to get value from materialized table", e);
    }
}
Also used : Window(org.apache.kafka.streams.kstream.Window) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) GenericRow(io.confluent.ksql.GenericRow) Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Example 12 with WindowedRow

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

the class KsMaterializedWindowTableIQv2 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 Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds);
        final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds);
        final WindowKeyQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = WindowKeyQuery.withKeyAndWindowStartRange(key, lower, upper);
        StateQueryRequest<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query);
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final KafkaStreams streams = stateStore.getKafkaStreams();
        final StateQueryResult<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> result = streams.query(request);
        final QueryResult<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        }
        if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        }
        try (WindowStoreIterator<ValueAndTimestamp<GenericRow>> it = queryResult.getResult()) {
            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.rowIteratorWithPosition(builder.build().iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to get value from materialized table", e);
    }
}
Also used : WindowStoreIterator(org.apache.kafka.streams.state.WindowStoreIterator) KafkaStreams(org.apache.kafka.streams.KafkaStreams) 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) GenericRow(io.confluent.ksql.GenericRow) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Example 13 with WindowedRow

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

the class KsMaterializedSessionTable method findSession.

private List<WindowedRow> findSession(final ReadOnlySessionStore<GenericKey, GenericRow> store, final GenericKey key, final Range<Instant> windowStart, final Range<Instant> windowEnd) {
    try (KeyValueIterator<Windowed<GenericKey>, GenericRow> it = cacheBypassFetcher.fetch(store, key)) {
        final Builder<WindowedRow> builder = ImmutableList.builder();
        while (it.hasNext()) {
            final KeyValue<Windowed<GenericKey>, GenericRow> next = it.next();
            final Window wnd = next.key.window();
            if (!windowStart.contains(wnd.startTime())) {
                continue;
            }
            if (!windowEnd.contains(wnd.endTime())) {
                continue;
            }
            final long rowTime = wnd.end();
            final WindowedRow row = WindowedRow.of(stateStore.schema(), next.key, next.value, rowTime);
            builder.add(row);
        }
        return builder.build();
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) GenericRow(io.confluent.ksql.GenericRow) Window(org.apache.kafka.streams.kstream.Window) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Example 14 with WindowedRow

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

the class KsMaterializedWindowTableTest method shouldMaintainResultOrder.

@Test
public void shouldMaintainResultOrder() {
    // Given:
    when(fetchIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    final Instant start = WINDOW_START_BOUNDS.lowerEndpoint();
    when(fetchIterator.next()).thenReturn(new KeyValue<>(start.toEpochMilli(), VALUE_1)).thenReturn(new KeyValue<>(start.plusMillis(1).toEpochMilli(), VALUE_2)).thenReturn(new KeyValue<>(start.plusMillis(2).toEpochMilli(), VALUE_3)).thenThrow(new AssertionError());
    when(cacheBypassFetcher.fetch(eq(tableStore), any(), any(), any())).thenReturn(fetchIterator);
    // When:
    final Iterator<WindowedRow> rowIterator = table.get(A_KEY, PARTITION, Range.all(), Range.all()).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(true));
    final List<WindowedRow> resultList = Lists.newArrayList(rowIterator);
    assertThat(resultList, contains(WindowedRow.of(SCHEMA, windowedKey(start), VALUE_1.value(), VALUE_1.timestamp()), WindowedRow.of(SCHEMA, windowedKey(start.plusMillis(1)), VALUE_2.value(), VALUE_2.timestamp()), WindowedRow.of(SCHEMA, windowedKey(start.plusMillis(2)), VALUE_3.value(), VALUE_3.timestamp())));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Instant(java.time.Instant) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow) Test(org.junit.Test)

Example 15 with WindowedRow

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

the class KsMaterializedWindowTableTest method shouldReturnValuesForClosedEndBounds_fetchAll.

@Test
public void shouldReturnValuesForClosedEndBounds_fetchAll() {
    // Given:
    final Range<Instant> end = Range.closed(NOW, NOW.plusSeconds(10));
    final Range<Instant> startEqiv = Range.closed(end.lowerEndpoint().minus(WINDOW_SIZE), end.lowerEndpoint().minus(WINDOW_SIZE));
    when(keyValueIterator.hasNext()).thenReturn(true, true, false);
    when(keyValueIterator.next()).thenReturn(new KeyValue<>(new Windowed<>(A_KEY, new TimeWindow(startEqiv.lowerEndpoint().toEpochMilli(), startEqiv.lowerEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_1)).thenReturn(new KeyValue<>(new Windowed<>(A_KEY2, new TimeWindow(startEqiv.upperEndpoint().toEpochMilli(), startEqiv.upperEndpoint().toEpochMilli() + WINDOW_SIZE.toMillis())), VALUE_2)).thenThrow(new AssertionError());
    // When:
    final Iterator<WindowedRow> rowIterator = table.get(PARTITION, Range.all(), end).rowIterator;
    // Then:
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(startEqiv.lowerEndpoint()), VALUE_1.value(), VALUE_1.timestamp())));
    assertThat(rowIterator.hasNext(), is(true));
    assertThat(rowIterator.next(), is(WindowedRow.of(SCHEMA, windowedKey(A_KEY2, startEqiv.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)

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