...
- Key access to ValueTransformer: While transforming values via
KStream.transformValues
andValueTransformer
, the key associated with the value may be needed, even if it is not changed. For instance, it may be used to access stores.As of now, the key is not available within these methods and interfaces, leading to the use of
KStream.transform
andTransformer
, and the unnecessary creation of newKeyValue
objects. - Key access to ValueMapper:
ValueMapper
should have read-only access to the key for the value it is mapping. Sometimes the value transformation will depend on the key.It is possible to do this with a full blown
KeyValueMapper
but that loses the promise that you won't change the key – so you might introduce a re-keying phase that is totally unnecessary. - Key access to ValueJoiner interface: In working with Kafka Stream joining, it's sometimes the case that a join key is not actually present in the values of the joins themselves (if, for example, a previous transform generated an ephemeral join key.) In such cases, the actual key of the join is not available in the ValueJoiner implementation to be used to construct the final joined value. This can be worked around by explicitly threading the join key into the value if needed, but it seems like extending the interface to pass the join key along as well would be helpful
Public Changes
KStream
interface:
Code Block | ||
---|---|---|
| ||
<GK, GV, RV> KStream<K, RV> leftJoin(final GlobalKTable<GK, GV> globalKTable, final KeyValueMapper<? super K, ? super V, ? extends GK> keyValueMapper, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super GV, ? extends RV> richValueJoinervalueJoinerWithKey); <GK, GV, RV> KStream<K, RV> join(final GlobalKTable<GK, GV> globalKTable, final KeyValueMapper<? super K, ? super V, ? extends GK> keyValueMapper, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super GV, ? extends RV> richValueJoinervalueJoinerWithKey); <VT, VR> KStream<K, VR> leftJoin(final KTable<K, VT> table, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VT, ? extends VR> richValueJoinervalueJoinerWithKey, final Serde<K> keySerde, final Serde<V> valSerde); <VT, VR> KStream<K, VR> leftJoin(final KTable<K, VT> table, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VT, ? extends VR> richValueJoinervalueJoinerWithKey); <VT, VR> KStream<K, VR> join(final KTable<K, VT> table, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VT, ? extends VR> richValueJoinervalueJoinerWithKey, final Serde<K> keySerde, final Serde<V> valSerde); <VT, VR> KStream<K, VR> join(final KTable<K, VT> table, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VT, ? extends VR> richValueJoinervalueJoinerWithKey); <VO, VR> KStream<K, VR> outerJoin(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows, final Serde<K> keySerde, final Serde<V> thisValueSerde, final Serde<VO> otherValueSerde); <VO, VR> KStream<K, VR> outerJoin(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows); <VO, VR> KStream<K, VR> leftJoin(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows, final Serde<K> keySerde, final Serde<V> thisValSerde, final Serde<VO> otherValueSerde); <VO, VR> KStream<K, VR> leftJoin(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows); <VO, VR> KStream<K, VR> join(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows, final Serde<K> keySerde, final Serde<V> thisValueSerde, final Serde<VO> otherValueSerde); <VO, VR> KStream<K, VR> join(final KStream<K, VO> otherStream, final RichValueJoiner<ValueJoinerWithKey<? super K, ? super V, ? super VO, ? extends VR> richValueJoinervalueJoinerWithKey, final JoinWindows windows); <VR> KStream<K, VR> transformValues(final ValueTransformerWithKeySupplier<? super K, ? super V, ? extends VR> valueTransformerWithKeySupplier, final String... stateStoreNames); <VR> KStream<K, VR> mapValues(ValueMapperWithKey<? super K, ? super V, ? extends VR> mapperWithKey); |
Proposed Changes
Handling lambdas
For Initializer, Aggregator, ValueMapper, ValueJoiner and their "withKey"
interfaces we support lambdas. For ValueTransformer interface we don't need lambdas by the core definition of the class.
For all above interfaces we support rich functions.
To support lambdas, we separate withKey interface from original ones, meaning we don't inherit or extend from one to another.
RichValueMapper
Code Block | ||
---|---|---|
| ||
public abstract class RichValueMapper<K, V, VR> implements ValueMapperWithKey<K, V, VR>, RichFunction { } public interface ValueMapperWithKey<K, V, VR> { VR apply(final K key, final V value); } public interface RichFunction { void init(); void close(); } |
RichValueJoiner
Code Block | ||
---|---|---|
| ||
public abstract class RichValueJoiner<K, V1, V2, VR> implements ValueJoinerWithKey<K, V1, V2, VR>, RichFunction { } public interface ValueJoinerWithKey<K, V1, V2, VR> { VR apply(final K key, final V1 value1, final V2 value2); } |
RichInitializer
Code Block | ||
---|---|---|
| ||
public abstract class RichInitializer<VA> implements Initializer<VA>, RichFunction { } |
RichAggregator
Code Block | ||
---|---|---|
| ||
public abstract class RichAggregator<K, V, VA> implements Aggregator<K, V, VA>, RichFunction { } |
ValueTransformerWithKeySupplier
Code Block | ||
---|---|---|
| ||
public interface ValueTransformerWithKeySupplier<K, V, VR> { ValueTransformerWithKey<K, V, VR> get(); } |
Handling rich functions while building the topology
RichValueMapper
Code Block | ||
---|---|---|
| ||
@Override
public <V1> KStream<K, V1> mapValues(final ValueMapperWithKey<? super K, ? super V, ? extends V1> mapperWithKey) {
Objects.requireNonNull(mapperWithKey, "mapperWithKey can't be null");
String name = topology.newName(MAPVALUES_NAME);
final RichValueMapper<K, V, V1> richValueMapper;
if (mapperWithKey instanceof RichValueMapper) {
richValueMapper = (RichValueMapper<K, V, V1>) mapperWithKey;
} else {
richValueMapper = new RichValueMapper<K, V, V1>() {
@Override
public void init() {}
@Override
public void close() {}
@Override
public V1 apply(K key, V value) {
return mapperWithKey.apply(key, value);
}
};
}
topology.addProcessor(name, new KStreamMapValues<>(richValueMapper), this.name);
return new KStreamImpl<>(topology, name, sourceNodes, this.repartitionRequired);
}
|
The PR can be found here.
Test Plan
The unit tests are changed accordingly to support the changes in core classes.
Rejected Alternatives
Lambdas are not supported
ValueMapper
example but it can be applied to other interfaces as well. Rich functions are proposed:public interface RichFunction {
void init(final ProcessorContext context);
void close();
}
public abstract class AbstractRichFunction implements RichFunction {
@Override
public void init(final ProcessorContext context) {}
@Override
public void close() {}
}
public abstract class RichValueJoiner<K, V1, V2, VR> extends AbstractRichFunction implements ValueJoiner<V1, V2, VR> {
@Override
public final VR apply(final V1 value1, final V2 value2) {
return apply(null, value1, value2);
}
public abstract VR apply(final K key, final V1 value1, final V2 value2);
}
Inside processor, we check if the instance (for example ValueMapper
instance) is rich (for example RichValueMapper
):
KStreamFlatMapValues(ValueMapper<? super V, ? extends Iterable<? extends V1>> mapper) {
this.mapper = mapper;
isRichFunction = mapper instanceof RichValueMapper ? true : false;
}
@Override
public void process(K key, V value) {
Iterable<? extends V1> newValues;
if (isRichFunction) {
newValues = ((RichValueMapper<? super K, ? super V, ? extends Iterable<? extends V1>>) mapper).apply(key, value);
} else {
newValues = mapper.apply(value);
}
for (V1 v : newValues) {
context().forward(key, v);
}
}
Not backward-compatible
We propose adding key information for ValueJoiner
, ValueTransformer
, and ValueMapper
classes and their apply(...)
methods.
As a result, we perform the following public changes (and their overloaded versions)
Class | Old | New |
---|---|---|
KStream | <VR> KStream<K, VR> mapValues(ValueMapper<? super V, ? extends VR> mapper); | <VR> KStream<K, VR> mapValues(ValueMapper<? super K, ? super V, ? extends VR> mapper); |
KStream | <VR> KStream<K, VR> transformValues(final ValueTransformerSupplier<? super V, ? extends VR> valueTransformerSupplier, final String... stateStoreNames); | <VR> KStream<K, VR> transformValues(final ValueTransformerSupplier<? super K, ? super V, ? extends VR> valueTransformerSupplier,final String... stateStoreNames); |
KStream | <VO, VR> KStream<K, VR> join(final KStream<K, VO> otherStream, final ValueJoiner<? super V, ? super VO, ? extends VR> joiner, final JoinWindows windows); | <VO, VR> KStream<K, VR> join(final KStream<K, VO> otherStream, final ValueJoiner<? super K, ? super V, ? super VO, ? extends VR> joiner, final JoinWindows windows); |
KTable | <VR> KTable<K, VR> mapValues(final ValueMapper<? super V, ? extends VR> mapper); | <VR> KTable<K, VR> mapValues(final ValueMapper<? super K, ? super V, ? extends VR> mapper); |
KTable | <VO, VR> KTable<K, VR> join(final KTable<K, VO> other, final ValueJoiner<? super V, ? super VO, ? extends VR> joiner); | <VO, VR> KTable<K, VR> join(final KTable<K, VO> other, final ValueJoiner<? super K, ? super V, ? super VO, ? extends VR> joiner); |
Lacking performance because deep-copy and need for RichFunctions
- We extend the target interfaces
ValueJoiner
,ValueTransformer
, andValueMapper as
ValueJoinerWithKey
,ValueTransformerWithKey
, andValueMapper
WithKey. In extended abstract classes we have an access to keys. - In Processor we check the actual instance of object:
this.valueTransformer = valueTransformer;
if (valueTransformer instanceof ValueTransformerWithKey) {
isTransformerWithKey = true;
} else {
isTransformerWithKey = false;
}..............
..............
@Override
public void process(K key, V value) {
if (isTransformerWithKey) {
K keyCopy = (K) Utils.deepCopy(key);
context.forward(key, ((ValueTransformerWithKey<K, V, R>) valueTransformer).transform(keyCopy, value));
} else {
context.forward(key, valueTransformer.transform(value));
}
} - As we can see from the above code snippet, we can guard the key change in Processors by deeply copying the object before calling the
apply()
method.
...