[英]How to print KStream to console?
我創建了一個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));
這不會失敗。 但是,這不會在控制台上打印任何內容,因為這個答案表明。
我是卡夫卡的新手。 所以做這項工作的任何建議都會對我有所幫助。
TL; DR使用印刷版 。
import org.apache.kafka.streams.kstream.Printed
val sysout = Printed
.toSysOut[String, String]
.withLabel("customerStream")
customerStream.print(sysout)
我會嘗試取消設置CLIENT_ID_CONFIG
並僅保留APPLICATION_ID_CONFIG
。 Kafka Streams 使用應用程序ID來設置客戶端ID 。
我還將驗證您的Kafka Streams應用程序正在使用的使用者組ID的偏移量(此使用者組ID也基於您的應用程序ID)。 使用kafka-consumer-groups.sh
工具。 可能是您的Streams應用程序超出了您為該主題生成的所有記錄,可能是因為自動偏移重置設置為最新,或者可能是由於您的問題無法輕易辨別的其他原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.