简体   繁体   English

如何从kafka主题通过密钥获取消息

[英]How to get message by key from kafka topic

I try to get message by key from kafka.我尝试从 kafka 通过密钥获取消息。 I found the only one solution is to use StateStore but I think it might be not a good idea.我发现唯一的解决方案是使用 StateStore,但我认为这可能不是一个好主意。 How to get message by key from kafka topic?如何从kafka主题通过密钥获取消息? Is it a good idea to use StateStore for this operation?使用 StateStore 进行此操作是个好主意吗?

Every record written to Kafka can optionally have a key (but it doesn't have to!), the key can be accessed a number of ways:写入 Kafka 的每条记录都可以选择有一个密钥(但不是必须的!),可以通过多种方式访问​​密钥:

Console consumer:控制台消费者:

$ kafka-console-consumer --bootstrap-server <servername>:9092 --topic topicname --from-beginning --property print.key=true --property key.separator=:

kafkacat :卡夫卡猫

$ kafkacat -b <servername>:9092 -C -t topicname -o beginning -K :

Java Consumer API : Java消费者API

ConsumerRecord#key()

Kafka isn't indexed by key though, it's indexed by offset, and optionally by timestamp . Kafka 不是按 key 索引的,它是按 offset 索引的,也可以按timestamp索引。 If you need to lookup a key then you'll need to materialize the data to a system that's been designed to lookup by key: relational database, key value store, or some index.如果您需要查找一个键,那么您需要将数据具体化到一个旨在按键查找的系统:关系数据库、键值存储或某些索引。 You can do this pretty easily with Kafka Connect, or if you'd like to build it into your service you could use the interactive queries feature of Kafka Streams .您可以使用 Kafka Connect 轻松完成此操作,或者如果您想将其构建到您的服务中,您可以使用Kafka Streams交互式查询功能

You can't "get messages by key from Kafka".您不能“从 Kafka 中通过密钥获取消息”。

One solution, if practical, would be to have as many partitions as keys and always route messages for a key to the same partition.如果可行,一种解决方案是拥有与键一样多的分区,并且始终将键的消息路由到同一分区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM