[英]Apache Kafka Stream to Stream
使用 Apache Kafka 我想將從 topic1 收到的記錄傳遞給 topic2 stream。 但仔細閱讀文檔后,我無法將事件發送到 topic2 stream。 這是我的代碼
@Test
public void topicToTopic() throws InterruptedException {
String broker = embeddedKafkaBroker.getBrokersAsString();
String topic1 = "Consumer-topic-1";
String topic2 = "Consumer-topic-2";
Properties config = getSourceConfig(broker);
Serde<String> stringSerde = Serdes.String();
StreamsBuilder builder = new StreamsBuilder();
builder.stream(topic1, Consumed.with(stringSerde, stringSerde))
.peek((k, v) -> System.out.println("Topic 1 Observed event:" + v))
.mapValues((ValueMapper<String, String>) String::toUpperCase)
.to(topic2, Produced.with(stringSerde, stringSerde));
Topology topology = builder.build();
KafkaStreams streams = new KafkaStreams(topology, config);
streams.start();
Thread.sleep(2000);
publishMessages(broker, topic1);
StreamsBuilder builder1 = new StreamsBuilder();
builder1.stream(topic2, Consumed.with(stringSerde, stringSerde))
.foreach((key, value) -> System.out.println("key: " + key + " -> " + value));
Topology topology1 = builder1.build();
KafkaStreams streams1 = new KafkaStreams(topology1, config);
streams1.start();
Thread.sleep(2000);
streams.close();
streams1.close();
}
知道有什么問題嗎?
問候
我認為您需要在發布消息之前啟動消費者 stream
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.