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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52185737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:22:02  来源:igfitidea点击:

How to get message by key from kafka topic

javaapache-kafkaspring-kafka

提问by ?ван Гладуш

I try to get message by key from kafka. I found only one solution is use StateStore but I think it's can be not good idea. How to get message by key from kafka topic? Is it good idea to use StateStore for this operation?

我尝试从 kafka 通过密钥获取消息。我发现只有一种解决方案是使用 StateStore,但我认为这不是一个好主意。如何从kafka主题通过密钥获取消息?使用 StateStore 进行此操作是个好主意吗?

采纳答案by Chris Matta

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. 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 不是按 key 索引的,它是按 offset 索引的,也可以按timestamp索引。如果您需要查找一个键,那么您需要将数据具体化到一个旨在按键查找的系统:关系数据库、键值存储或某些索引。您可以使用 Kafka Connect 轻松完成此操作,或者如果您想将其构建到您的服务中,您可以使用Kafka Streams交互式查询功能

回答by Gary Russell

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.

如果可行,一种解决方案是拥有与键一样多的分区,并且始终将键的消息路由到同一分区。