use of org.apache.kafka.streams.Topology in project kafkastreams-cep by fhussonnois.
the class CEPStockKStreamsIntegrationTest method test.
@Test
public void test() throws ExecutionException, InterruptedException {
final Collection<KeyValue<String, String>> batch1 = Arrays.asList(new KeyValue<>(null, "{\"name\":\"e1\",\"price\":100,\"volume\":1010}"), new KeyValue<>(null, "{\"name\":\"e2\",\"price\":120,\"volume\":990}"), new KeyValue<>(null, "{\"name\":\"e3\",\"price\":120,\"volume\":1005}"), new KeyValue<>(null, "{\"name\":\"e4\",\"price\":121,\"volume\":999}"), new KeyValue<>(null, "{\"name\":\"e5\",\"price\":120,\"volume\":999}"), new KeyValue<>(null, "{\"name\":\"e6\",\"price\":125,\"volume\":750}"), new KeyValue<>(null, "{\"name\":\"e7\",\"price\":120,\"volume\":950}"), new KeyValue<>(null, "{\"name\":\"e8\",\"price\":120,\"volume\":700}"));
IntegrationTestUtils.produceKeyValuesSynchronously(INPUT_STREAM, batch1, TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), mockTime);
// build query
ComplexStreamsBuilder builder = new ComplexStreamsBuilder();
CEPStream<String, StockEvent> stream = builder.stream(INPUT_STREAM);
KStream<String, Sequence<String, StockEvent>> stocks = stream.query("Stocks", Patterns.STOCKS);
stocks.mapValues(seq -> {
JSONObject json = new JSONObject();
seq.asMap().forEach((k, v) -> {
JSONArray events = new JSONArray();
json.put(k, events);
List<String> collect = v.stream().map(e -> e.value.name).collect(Collectors.toList());
Collections.reverse(collect);
collect.forEach(events::add);
});
return json.toJSONString();
}).through(OUTPUT_STREAM, Produced.with(null, Serdes.String())).print(Printed.toSysOut());
Topology topology = builder.build();
kafkaStreams = new KafkaStreams(topology, streamsConfiguration);
kafkaStreams.start();
final Properties consumerConfig = TestUtils.consumerConfig(CLUSTER.bootstrapServers(), StringDeserializer.class, StringDeserializer.class);
List<KeyValue<String, String>> result = IntegrationTestUtils.readKeyValues(OUTPUT_STREAM, consumerConfig, TimeUnit.SECONDS.toMillis(10), 4);
Assert.assertEquals(4, result.size());
Assert.assertEquals("{\"0\":[\"e1\"],\"1\":[\"e2\",\"e3\",\"e4\",\"e5\"],\"2\":[\"e6\"]}", result.get(0).value);
Assert.assertEquals("{\"0\":[\"e3\"],\"1\":[\"e4\"],\"2\":[\"e6\"]}", result.get(1).value);
Assert.assertEquals("{\"0\":[\"e1\"],\"1\":[\"e2\",\"e3\",\"e4\",\"e5\",\"e6\",\"e7\"],\"2\":[\"e8\"]}", result.get(2).value);
Assert.assertEquals("{\"0\":[\"e3\"],\"1\":[\"e4\",\"e6\"],\"2\":[\"e8\"]}", result.get(3).value);
}
use of org.apache.kafka.streams.Topology in project brave by openzipkin.
the class ITKafkaStreamsTracing method should_create_spans_from_stream_with_tracing_filter_predicate_false.
@Test
public void should_create_spans_from_stream_with_tracing_filter_predicate_false() {
String inputTopic = testName.getMethodName() + "-input";
String outputTopic = testName.getMethodName() + "-output";
StreamsBuilder builder = new StreamsBuilder();
builder.stream(inputTopic, Consumed.with(Serdes.String(), Serdes.String())).transform(kafkaStreamsTracing.filter("filter-2", (key, value) -> false)).to(outputTopic, Produced.with(Serdes.String(), Serdes.String()));
Topology topology = builder.build();
KafkaStreams streams = buildKafkaStreams(topology);
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
waitForStreamToRun(streams);
MutableSpan spanInput = testSpanHandler.takeRemoteSpan(CONSUMER);
assertThat(spanInput.tags()).containsEntry("kafka.topic", inputTopic);
MutableSpan spanProcessor = testSpanHandler.takeLocalSpan();
assertChildOf(spanProcessor, spanInput);
assertThat(spanProcessor.tags()).containsEntry(KAFKA_STREAMS_FILTERED_TAG, "true");
// the filter transformer returns false so record is dropped
streams.close();
streams.cleanUp();
}
use of org.apache.kafka.streams.Topology in project brave by openzipkin.
the class ITKafkaStreamsTracing method should_throw_exception_upwards.
@Test
public void should_throw_exception_upwards() {
TransformerSupplier<String, String, KeyValue<String, String>> transformerSupplier = kafkaStreamsTracing.transformer("exception-transformer", () -> new Transformer<String, String, KeyValue<String, String>>() {
ProcessorContext context;
@Override
public void init(ProcessorContext context) {
this.context = context;
}
@Override
public KeyValue<String, String> transform(String key, String value) {
throw new IllegalArgumentException("illegal-argument");
}
@Override
public void close() {
}
});
String inputTopic = testName.getMethodName() + "-input";
String outputTopic = testName.getMethodName() + "-output";
StreamsBuilder builder = new StreamsBuilder();
builder.stream(inputTopic, Consumed.with(Serdes.String(), Serdes.String())).transform(transformerSupplier).to(outputTopic, Produced.with(Serdes.String(), Serdes.String()));
Topology topology = builder.build();
KafkaStreams streams = buildKafkaStreams(topology);
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
waitForStreamToRun(streams);
MutableSpan spanInput = testSpanHandler.takeRemoteSpan(CONSUMER);
assertThat(spanInput.tags()).containsEntry("kafka.topic", inputTopic);
MutableSpan spanProcessor = testSpanHandler.takeLocalSpan();
assertThat(spanProcessor.error()).hasMessage("illegal-argument");
assertChildOf(spanProcessor, spanInput);
assertThat(!streams.state().isRunningOrRebalancing());
streams.close();
streams.cleanUp();
}
use of org.apache.kafka.streams.Topology in project brave by openzipkin.
the class ITKafkaStreamsTracing method should_create_spans_from_stream_with_tracing_mark_as_filtered_predicate_false.
@Test
public void should_create_spans_from_stream_with_tracing_mark_as_filtered_predicate_false() {
String inputTopic = testName.getMethodName() + "-input";
String outputTopic = testName.getMethodName() + "-output";
StreamsBuilder builder = new StreamsBuilder();
builder.stream(inputTopic, Consumed.with(Serdes.String(), Serdes.String())).transformValues(kafkaStreamsTracing.markAsFiltered("filter-2", (key, value) -> false)).filterNot((k, v) -> Objects.isNull(v)).to(outputTopic, Produced.with(Serdes.String(), Serdes.String()));
Topology topology = builder.build();
KafkaStreams streams = buildKafkaStreams(topology);
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
waitForStreamToRun(streams);
MutableSpan spanInput = testSpanHandler.takeRemoteSpan(CONSUMER);
assertThat(spanInput.tags()).containsEntry("kafka.topic", inputTopic);
MutableSpan spanProcessor = testSpanHandler.takeLocalSpan();
assertChildOf(spanProcessor, spanInput);
assertThat(spanProcessor.tags()).containsEntry(KAFKA_STREAMS_FILTERED_TAG, "true");
// the filter transformer returns false so record is dropped
streams.close();
streams.cleanUp();
}
use of org.apache.kafka.streams.Topology in project brave by openzipkin.
the class ITKafkaStreamsTracing method should_create_multiple_span_from_stream_input_topic_whenSharingDisabled.
@Test
public void should_create_multiple_span_from_stream_input_topic_whenSharingDisabled() {
String inputTopic = testName.getMethodName() + "-input";
StreamsBuilder builder = new StreamsBuilder();
builder.stream(inputTopic).foreach((k, v) -> {
});
Topology topology = builder.build();
KafkaStreamsTracing kafkaStreamsTracing = KafkaStreamsTracing.newBuilder(tracing).singleRootSpanOnReceiveBatch(false).build();
KafkaStreams streams = kafkaStreamsTracing.kafkaStreams(topology, streamsProperties());
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
send(new ProducerRecord<>(inputTopic, TEST_KEY, TEST_VALUE));
waitForStreamToRun(streams);
for (int i = 0; i < 3; i++) {
MutableSpan spanInput = testSpanHandler.takeRemoteSpan(CONSUMER);
assertThat(spanInput.tags()).containsEntry("kafka.topic", inputTopic);
}
streams.close();
streams.cleanUp();
}
Aggregations