Search in sources :

Example 1 with JsonSequenceSerde

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);
}
Also used : ArrayList(java.util.ArrayList) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Sequence(com.github.fhuss.kafka.streams.cep.core.Sequence) Topology(org.apache.kafka.streams.Topology) ConsumerRecordFactory(org.apache.kafka.streams.test.ConsumerRecordFactory) JsonSequenceSerde(com.github.fhuss.kafka.streams.cep.serdes.JsonSequenceSerde) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 2 with JsonSequenceSerde

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);
}
Also used : ArrayList(java.util.ArrayList) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Sequence(com.github.fhuss.kafka.streams.cep.core.Sequence) Topology(org.apache.kafka.streams.Topology) ConsumerRecordFactory(org.apache.kafka.streams.test.ConsumerRecordFactory) JsonSequenceSerde(com.github.fhuss.kafka.streams.cep.serdes.JsonSequenceSerde) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Aggregations

Sequence (com.github.fhuss.kafka.streams.cep.core.Sequence)2 JsonSequenceSerde (com.github.fhuss.kafka.streams.cep.serdes.JsonSequenceSerde)2 ArrayList (java.util.ArrayList)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 Topology (org.apache.kafka.streams.Topology)2 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)2 ConsumerRecordFactory (org.apache.kafka.streams.test.ConsumerRecordFactory)2 Test (org.junit.Test)2