gpt4 book ai didi

java - 如何将 KStream 打印到控制台?

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:39 25 4
gpt4 key购买 nike

我创建了一个 Kafka 主题并向其推送了一条消息。

所以

bin/kafka-console-consumer --bootstrap-server abc.xyz.com:9092 --topic myTopic --from-beginning --property print.key=true --property key.separator="-"

打印

key1-customer1

在命令行上。

我想从这个主题创建一个 Kafka Stream,并想在控制台上打印这个 key1-customer1

我为它写了以下内容:

final Properties streamsConfiguration = new Properties();

streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "app-id");
streamsConfiguration.put(StreamsConfig.CLIENT_ID_CONFIG, "client-id");
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "abc.xyz.com:9092");

streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

// Records should be flushed every 10 seconds. This is less than the default
// in order to keep this example interactive.
streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 10 * 1000);
// For illustrative purposes we disable record caches
streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);

final StreamsBuilder builder = new StreamsBuilder();
final KStream<String, String> customerStream = builder.stream("myTopic");
customerStream.foreach(new ForeachAction<String, String>() {
public void apply(String key, String value) {
System.out.println(key + ": " + value);
}
});

final KafkaStreams streams = new KafkaStreams(builder.build(), streamsConfiguration);

streams.start();

Runtime.getRuntime().addShutdownHook(new Thread(streams::close));

这不会失败。但是,这不会在控制台上打印任何内容 this answer建议。

我是卡夫卡的新手。因此,任何使这项工作成功的建议都会对我有很大帮助。

最佳答案

TL;DR 使用 Printed .

import org.apache.kafka.streams.kstream.Printed
val sysout = Printed
.toSysOut[String, String]
.withLabel("customerStream")
customerStream.print(sysout)

关于java - 如何将 KStream 打印到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47919702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com