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);
}
}
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);
}
}
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();
}
}
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())));
}
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));
}
Aggregations