如何查看kafka标头

时间:2019-03-15 10:33:16

标签: apache-kafka kafka-producer-api

我们正在使用标头向Kafka发送带有标头的消息 org.apache.kafka.clients.producer.ProducerRecord

                Popup noticePopup = new Popup("Notice", 1500, 900);
                JPanel noticePanel = new JPanel();
                noticePanel.setPreferredSize(new Dimension(1500,1000));
                List<List> ListNote = controller.Medicament.consultationNotice(medocSelectStr);
                String noticeThis =null;
                for(int n=0;n<ListNote.size();n++) {
                    noticeThis = (String) ListNote.get(n).get(1);
                }
                JTextArea noticeArea = new JTextArea(noticeThis);
                noticePanel.add(noticeArea);

                JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                noticePopup.getContentPane().add(scroll);

我如何真正使用命令查看这些标题。 kafka-console-consumer.sh仅显示有效负载,没有标题。

4 个答案:

答案 0 :(得分:5)

从 kafka-2.7.0 开始,您可以通过提供属性 print.headers=true

在控制台消费者中启用打印标题

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic quickstart-events --property print.key=true --property print.headers=true --property print.timestamp=true

答案 1 :(得分:4)

您可以使用出色的kafkacat工具。

示例命令:

kafkacat -b kafka-broker:9092 -t my_topic_name -C \
  -f '\nKey (%K bytes): %k
  Value (%S bytes): %s
  Timestamp: %T
  Partition: %p
  Offset: %o
  Headers: %h\n'

示例输出:

Key (-1 bytes):
  Value (13 bytes): {foo:"bar 5"}
  Timestamp: 1548350164096
  Partition: 0
  Offset: 34
  Headers: __connect.errors.topic=test_topic_json,__connect.errors.partition=0,__connect.errors.offset=94,__connect.errors.connector.name=file_sink_03,__connect.errors.task.id=0,__connect.errors.stage=VALU
E_CONVERTER,__connect.errors.class.name=org.apache.kafka.connect.json.JsonConverter,__connect.errors.exception.class.name=org.apache.kafka.connect.errors.DataException,__connect.errors.exception.message=Co
nverting byte[] to Kafka Connect data failed due to serialization error: ,__connect.errors.exception.stacktrace=org.apache.kafka.connect.errors.DataException: Converting byte[] to Kafka Connect data failed
 due to serialization error:

答案 2 :(得分:1)

通过kafka-console-consumer.sh脚本:

exec $(dirname $0)/kafka-run-class.sh kafka.tools.ConsoleConsumer "$@"

src:https://github.com/apache/kafka/blob/2.1.1/bin/kafka-console-consumer.sh

kafka.tools.ConsoleConsumer中,标头提供给格式化程序,但现有的格式化程序均未使用它:

formatter.writeTo(new ConsumerRecord(msg.topic, msg.partition, msg.offset, msg.timestamp,
                                     msg.timestampType, 0, 0, 0, msg.key, msg.value, msg.headers),
                                     output)

src:https://github.com/apache/kafka/blob/2.1.1/core/src/main/scala/kafka/tools/ConsoleConsumer.scala

在上述链接的底部,您可以看到现有的格式化程序。

如果要打印标题,则需要实现自己的kafka.common.MessageFormatter,尤其是其写入方法:

def writeTo(consumerRecord: ConsumerRecord[Array[Byte], Array[Byte]], output: PrintStream): Unit

然后使用提供自己的格式化程序的--formatter运行控制台使用者(它也应该出现在类路径中)。

另一种更简单,更快捷的方法是使用KafkaConsumer实现自己的小程序并在调试中检查标头。

答案 3 :(得分:0)

您也可以使用kafkactl。例如。输出为yaml:

kafkactl consume my-topic --print-headers -o yaml

示例输出:

partition: 1
offset: 22
headers:
  key1: value1
  key2: value2
value: my-value

免责声明:我是这个项目的贡献者