This page is meant as a template for writing a KIP. To create a KIP choose Tools->Copy on this page and modify with your content and replace the heading with the next KIP number and a description of your issue. Replace anything in italics with your own description.
Status
Current state: "Under Discussion"
Discussion thread: here
JIRA: KAFKA-13494
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
Provide an implementation of the Query
interface, introduced in KIP-796: Interactive Query v2 , to support session and window queries.
Public Interfaces
In this KIP we propose two new public classes:
- WindowKeyQuery: used to search occurrences of keys in window stores in combination with IQv2
- WindowRangeQuery: used to search per window aggregates of keys in window and session stores in combination with IQv2
Proposed Changes
The WindowKeyQuery class:
public class WindowKeyQuery<K, V> implements Query<WindowStoreIterator<V>> { public static <K, V> WindowKeyQuery<K, V> withKey(final K key); public static <K, V> WindowKeyQuery<K, V> withKeyAndWindowBounds(final K key, final Instant windowLower, final Instant windowUpper); public K getKey(); public Optional<Instant> getWindowLowerBound(); public Optional<Instant> getWindowUpperBound(); }
The WindowRangeQuery class:
public class WindowRangeQuery<K, V> implements Query<KeyValueIterator<Windowed<K>, V>> { private final Optional<K> key; private final Optional<Instant> windowLower; private final Optional<Instant> windowUpper; private WindowRangeQuery(final Optional<K> key, final Optional<Instant> windowLower, final Optional<Instant> windowUpper) { this.key = key; this.windowLower = windowLower; this.windowUpper = windowUpper; } public static <K, V> WindowRangeQuery<K, V> withKey(final K key) { return new WindowRangeQuery<>(Optional.of(key), Optional.empty(), Optional.empty()); } public static <K, V> WindowRangeQuery<K, V> withWindowRange(final Instant windowLower, final Instant windowUpper) { return new WindowRangeQuery<>(Optional.empty(), Optional.of(windowLower), Optional.of(windowUpper)); } public Optional<K> getKey() { return key; } public Optional<Instant> getWindowLowerBound() { return windowLower; } public Optional<Instant> getWindowUpperBound() { return windowUpper; } }
Examples
The following example illustrates the use of the WindowKeyQuey class.
final Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds); final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds); WindowKeyQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = WindowKeyQuery.withKeyAndWindowBounds(key, lower, upper); final StateQueryRequest<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query); final StateQueryResult<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request);
The following example illustrates the use of the WindowQuery class.
final Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds); final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds); WindowRangeQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = WindowRangeQuery.withWindowRange(lower, upper); final StateQueryRequest<KeyValueIterator<Windowed<GenericKey>, ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query); final StateQueryResult<KeyValueIterator<Windowed<GenericKey>, ValueAndTimestamp<GenericRow>>> result = stateStore.getKafkaStreams().query(request); final KeyValueIterator<Windowed<GenericKey>, ValueAndTimestamp<GenericRow>> iterator = result.getGlobalResult().getResult();
Compatibility, Deprecation, and Migration Plan
- Since this is a completely new set of APIs, no backward compatibility concerns are anticipated.
- Since nothing is deprecated in this KIP, users have no need to migrate unless they want to.