use of org.apache.kafka.streams.kstream.ValueTransformerWithKey in project apache-kafka-on-k8s by banzaicloud.
the class KStreamTransformValuesTest method testTransformWithKey.
@Test
public void testTransformWithKey() {
StreamsBuilder builder = new StreamsBuilder();
ValueTransformerWithKeySupplier<Integer, Number, Integer> valueTransformerSupplier = new ValueTransformerWithKeySupplier<Integer, Number, Integer>() {
public ValueTransformerWithKey<Integer, Number, Integer> get() {
return new ValueTransformerWithKey<Integer, Number, Integer>() {
private int total = 0;
@Override
public void init(final ProcessorContext context) {
}
@Override
public Integer transform(final Integer readOnlyKey, final Number value) {
total += value.intValue() + readOnlyKey;
return total;
}
@Override
public void close() {
}
};
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
KStream<Integer, Integer> stream;
MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
stream = builder.stream(topicName, Consumed.with(intSerde, intSerde));
stream.transformValues(valueTransformerSupplier).process(processor);
driver.setUp(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey * 10);
}
String[] expected = { "1:11", "10:121", "100:1221", "1000:12221" };
assertArrayEquals(expected, processor.processed.toArray());
}
use of org.apache.kafka.streams.kstream.ValueTransformerWithKey in project kafka by apache.
the class KTableImplTest method shouldPreserveSerdesForOperators.
@Test
public void shouldPreserveSerdesForOperators() {
final StreamsBuilder builder = new StreamsBuilder();
final KTable<String, String> table1 = builder.table("topic-2", stringConsumed);
final ConsumedInternal<String, String> consumedInternal = new ConsumedInternal<>(stringConsumed);
final KeyValueMapper<String, String, String> selector = (key, value) -> key;
final ValueMapper<String, String> mapper = value -> value;
final ValueJoiner<String, String, String> joiner = (value1, value2) -> value1;
final ValueTransformerWithKeySupplier<String, String, String> valueTransformerWithKeySupplier = () -> new ValueTransformerWithKey<String, String, String>() {
@Override
public void init(final ProcessorContext context) {
}
@Override
public String transform(final String key, final String value) {
return value;
}
@Override
public void close() {
}
};
assertEquals(((AbstractStream) table1.filter((key, value) -> false)).keySerde(), consumedInternal.keySerde());
assertEquals(((AbstractStream) table1.filter((key, value) -> false)).valueSerde(), consumedInternal.valueSerde());
assertEquals(((AbstractStream) table1.filter((key, value) -> false, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.filter((key, value) -> false, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.filterNot((key, value) -> false)).keySerde(), consumedInternal.keySerde());
assertEquals(((AbstractStream) table1.filterNot((key, value) -> false)).valueSerde(), consumedInternal.valueSerde());
assertEquals(((AbstractStream) table1.filterNot((key, value) -> false, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.filterNot((key, value) -> false, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.mapValues(mapper)).keySerde(), consumedInternal.keySerde());
assertNull(((AbstractStream) table1.mapValues(mapper)).valueSerde());
assertEquals(((AbstractStream) table1.mapValues(mapper, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.mapValues(mapper, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.toStream()).keySerde(), consumedInternal.keySerde());
assertEquals(((AbstractStream) table1.toStream()).valueSerde(), consumedInternal.valueSerde());
assertNull(((AbstractStream) table1.toStream(selector)).keySerde());
assertEquals(((AbstractStream) table1.toStream(selector)).valueSerde(), consumedInternal.valueSerde());
assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier)).keySerde(), consumedInternal.keySerde());
assertNull(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier)).valueSerde());
assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.transformValues(valueTransformerWithKeySupplier, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertNull(((AbstractStream) table1.groupBy(KeyValue::new)).keySerde());
assertNull(((AbstractStream) table1.groupBy(KeyValue::new)).valueSerde());
assertEquals(((AbstractStream) table1.groupBy(KeyValue::new, Grouped.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.groupBy(KeyValue::new, Grouped.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.join(table1, joiner)).keySerde(), consumedInternal.keySerde());
assertNull(((AbstractStream) table1.join(table1, joiner)).valueSerde());
assertEquals(((AbstractStream) table1.join(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.join(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.leftJoin(table1, joiner)).keySerde(), consumedInternal.keySerde());
assertNull(((AbstractStream) table1.leftJoin(table1, joiner)).valueSerde());
assertEquals(((AbstractStream) table1.leftJoin(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.leftJoin(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
assertEquals(((AbstractStream) table1.outerJoin(table1, joiner)).keySerde(), consumedInternal.keySerde());
assertNull(((AbstractStream) table1.outerJoin(table1, joiner)).valueSerde());
assertEquals(((AbstractStream) table1.outerJoin(table1, joiner, Materialized.with(mySerde, mySerde))).keySerde(), mySerde);
assertEquals(((AbstractStream) table1.outerJoin(table1, joiner, Materialized.with(mySerde, mySerde))).valueSerde(), mySerde);
}
use of org.apache.kafka.streams.kstream.ValueTransformerWithKey in project kafka by apache.
the class KStreamTransformValuesTest method testTransformWithKey.
// Old PAPI. Needs to be migrated.
@SuppressWarnings("deprecation")
@Test
public void testTransformWithKey() {
final StreamsBuilder builder = new StreamsBuilder();
final ValueTransformerWithKeySupplier<Integer, Number, Integer> valueTransformerSupplier = () -> new ValueTransformerWithKey<Integer, Number, Integer>() {
private int total = 0;
@Override
public void init(final org.apache.kafka.streams.processor.ProcessorContext context) {
}
@Override
public Integer transform(final Integer readOnlyKey, final Number value) {
total += value.intValue() + readOnlyKey;
return total;
}
@Override
public void close() {
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
final KStream<Integer, Integer> stream;
stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
stream.transformValues(valueTransformerSupplier).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<Integer, Integer> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new IntegerSerializer());
for (final int expectedKey : expectedKeys) {
inputTopic.pipeInput(expectedKey, expectedKey * 10, expectedKey / 2L);
}
}
final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(1, 11, 0), new KeyValueTimestamp<>(10, 121, 5), new KeyValueTimestamp<>(100, 1221, 50), new KeyValueTimestamp<>(1000, 12221, 500) };
assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
Aggregations