use of com.github.fhuss.kafka.streams.cep.serdes.JsonSequenceSerde in project kafkastreams-cep by fhussonnois.
the class CEPStreamIntegrationTest method testPatternGivenMultipleRecordKeys.
@Test
public void testPatternGivenMultipleRecordKeys() {
// Build query
ComplexStreamsBuilder builder = new ComplexStreamsBuilder();
CEPStream<String, Integer> stream = builder.stream(INPUT_TOPIC_1, Consumed.with(STRING_SERDE, Serdes.Integer()));
KStream<String, Sequence<String, Integer>> sequences = stream.query(DEFAULT_TEST_QUERY, SIMPLE_PATTERN, Queried.with(STRING_SERDE, Serdes.Integer()));
sequences.to(OUTPUT_TOPIC_1, Produced.with(STRING_SERDE, new JsonSequenceSerde<>()));
Topology topology = builder.build();
driver = new TopologyTestDriver(topology, streamsConfiguration);
ConsumerRecordFactory<String, Integer> factory = new ConsumerRecordFactory<>(Serdes.String().serializer(), Serdes.Integer().serializer());
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 0));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K2, -10));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K2, 0));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 3));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K2, 6));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 1));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 2));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 6));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K2, 4));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K2, 4));
// JSON values are de-serialized as double
List<ProducerRecord<String, Sequence<String, Double>>> results = new ArrayList<>();
results.add(driver.readOutput(OUTPUT_TOPIC_1, STRING_SERDE.deserializer(), new JsonSequenceSerde.SequenceDeserializer<>()));
results.add(driver.readOutput(OUTPUT_TOPIC_1, STRING_SERDE.deserializer(), new JsonSequenceSerde.SequenceDeserializer<>()));
Assert.assertEquals(2, results.size());
final ProducerRecord<String, Sequence<String, Double>> kvOne = results.get(0);
Assert.assertEquals(K1, kvOne.key());
assertStagesNames(kvOne.value(), STAGE_1, STAGE_2, STAGE_3);
assertStagesValue(kvOne.value(), STAGE_1, 0.0);
assertStagesValue(kvOne.value(), STAGE_2, 3.0, 1.0, 2.0);
assertStagesValue(kvOne.value(), STAGE_3, 6.0);
final ProducerRecord<String, Sequence<String, Double>> kvTwo = results.get(1);
Assert.assertEquals(K2, kvTwo.key());
assertStagesNames(kvTwo.value(), STAGE_1, STAGE_2, STAGE_3);
assertStagesValue(kvTwo.value(), STAGE_1, 0.0);
assertStagesValue(kvTwo.value(), STAGE_2, 6.0, 4.0);
assertStagesValue(kvTwo.value(), STAGE_3, 4.0);
}
use of com.github.fhuss.kafka.streams.cep.serdes.JsonSequenceSerde in project kafkastreams-cep by fhussonnois.
the class CEPStreamIntegrationTest method testPatternGivenRecordsFromMultipleTopics.
@Test
public void testPatternGivenRecordsFromMultipleTopics() {
// Build query
ComplexStreamsBuilder builder = new ComplexStreamsBuilder();
CEPStream<String, Integer> stream = builder.stream(Arrays.asList(INPUT_TOPIC_1, INPUT_TOPIC_2), Consumed.with(STRING_SERDE, Serdes.Integer()));
KStream<String, Sequence<String, Integer>> sequences = stream.query(DEFAULT_TEST_QUERY, PATTERN_MULTIPLE_TOPICS, Queried.with(STRING_SERDE, Serdes.Integer()));
sequences.to(OUTPUT_TOPIC_1, Produced.with(STRING_SERDE, new JsonSequenceSerde<>()));
Topology topology = builder.build();
driver = new TopologyTestDriver(topology, streamsConfiguration);
ConsumerRecordFactory<String, Integer> factory = new ConsumerRecordFactory<>(Serdes.String().serializer(), Serdes.Integer().serializer());
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 0));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 1));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 2));
driver.pipeInput(factory.create(INPUT_TOPIC_1, K1, 3));
driver.pipeInput(factory.create(INPUT_TOPIC_2, K1, 6));
driver.pipeInput(factory.create(INPUT_TOPIC_2, K1, 10));
// JSON values are de-serialized as double
List<ProducerRecord<String, Sequence<String, Double>>> results = new ArrayList<>();
ProducerRecord<String, Sequence<String, Double>> record;
do {
record = driver.readOutput(OUTPUT_TOPIC_1, STRING_SERDE.deserializer(), new JsonSequenceSerde.SequenceDeserializer<>());
if (record != null) {
results.add(record);
}
} while (record != null);
results.forEach(System.out::println);
Assert.assertEquals(2, results.size());
final ProducerRecord<String, Sequence<String, Double>> first = results.get(0);
Assert.assertEquals(K1, first.key());
assertStagesNames(first.value(), STAGE_1, STAGE_2, STAGE_3);
assertStagesValue(first.value(), STAGE_1, 0.0);
assertStagesTopic(first.value(), STAGE_1, INPUT_TOPIC_1);
assertStagesValue(first.value(), STAGE_2, 1.0, 2.0, 3.0);
assertStagesTopic(first.value(), STAGE_2, INPUT_TOPIC_1, INPUT_TOPIC_1, INPUT_TOPIC_1);
assertStagesValue(first.value(), STAGE_3, 6.0);
assertStagesTopic(first.value(), STAGE_3, INPUT_TOPIC_2);
final ProducerRecord<String, Sequence<String, Double>> second = results.get(1);
Assert.assertEquals(K1, second.key());
assertStagesNames(second.value(), STAGE_1, STAGE_2, STAGE_3);
assertStagesValue(second.value(), STAGE_1, 0.0);
assertStagesTopic(second.value(), STAGE_1, INPUT_TOPIC_1);
assertStagesValue(second.value(), STAGE_2, 1.0, 2.0, 3.0);
assertStagesTopic(second.value(), STAGE_2, INPUT_TOPIC_1, INPUT_TOPIC_1, INPUT_TOPIC_1);
assertStagesValue(second.value(), STAGE_3, 10.0);
assertStagesTopic(second.value(), STAGE_3, INPUT_TOPIC_2);
}
Aggregations