带有kafka-avro-console-consumer的LoggingMessageFormatter

时间:2018-07-17 23:16:21

标签: apache-kafka log4j avro kafka-consumer-api confluent-schema-registry

我正在尝试使用log4j格式的kafka-avro-console-consumer在kafka主题上打印avro消息。

为此,我使用以下kafka-avro-console-consumer命令:

 bin/kafka-avro-console-consumer --bootstrap-server localhost:9092 --topic avro-test -property print.key=true --formatter kafka.tools.LoggingMessageFormatter 

我已经通过以下命令导出了KAFKA_OPTS:

 export $KAFKA_OPTS= -Dlog4j.configuration=file:/path/to/file/kafka-console-consumer-log4j.properties

现在,如果我使用以下命令运行常规的kafka-console-consumer:

bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic avro-test -property print.key=true --formatter kafka.tools.LoggingMessageFormatter

我能够产生启用log4j的输出:

[2018-07-17 19:09:40,514] INFO [Consumer clientId=consumer-1, groupId=console-consumer-10597] (Re-)joining group (org.apache.kafka.clients.consumer.internals.AbstractCoordinator)
[2018-07-17 19:09:40,522] INFO [Consumer clientId=consumer-1, groupId=console-consumer-10597] Successfully joined group with generation 1 (org.apache.kafka.clients.consumer.internals.AbstractCoordinator)
[2018-07-17 19:09:40,523] INFO [Consumer clientId=consumer-1, groupId=console-consumer-10597] Setting newly assigned partitions [avro-test-0] (org.apache.kafka.clients.consumer.internals.ConsumerCoordinator)
[2018-07-17 19:09:40,531] INFO [Consumer clientId=consumer-1, groupId=console-consumer-10597] Resetting offset for partition avro-test-0 to offset 23. (org.apache.kafka.clients.consumer.internals.Fetcher)

但是,如果我通过以下命令使用avro使用者,则此格式化选项不会生效:

 bin/kafka-avro-console-consumer --bootstrap-server localhost:9092 --topic avro-test -property print.key=true --formatter kafka.tools.LoggingMessageFormatter 

它只是求助于默认格式化程序。

这里可能会缺少我吗?

1 个答案:

答案 0 :(得分:0)

我认为,如果您覆盖--formatter,将不会再收到Avro消息,因为kafka.tools.LoggingMessageFormatter无法理解如何反序列化Avro

Ref - source code

DEFAULT_AVRO_FORMATTER="--formatter io.confluent.kafka.formatter.AvroMessageFormatter"

...

for OPTION in "$@"
do 
  case $OPTION in
    --formatter)
DEFAULT_AVRO_FORMATTER=""

...

exec $(dirname $0)/schema-registry-run-class kafka.tools.ConsoleConsumer $DEFAULT_AVRO_FORMATTER ...

因此,它应按预期运行kafka.tools.ConsoleConsumer --formatter kafka.tools.LoggingMessageFormatter,因为未分配默认值,并且schema-registry-run-class定义了KAFKA_OPTS,但是您不必在该行上有空格或美元符号

export KAFKA_OPTS='-Dlog4j.configuration=file:/path/to/file/kafka-console-consumer-log4j.properties'
bin/kafka-avro-console-consumer ...