从控制台产生具有空值(墓碑)的Kafka消息

编程入门 行业动态 更新时间:2024-10-22 13:46:19
本文介绍了从控制台产生具有空值(墓碑)的Kafka消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有什么方法可以在kafka-console-producer中产生一个带有空值的消息(即,将其标记为供压缩程序使用逻辑删除的消息)?

Is there any way to produce a message in the kafka-console-producer with a null value (ie. mark it for the compactor to delete it with a tombstone)?

我尝试生成"mykey"和"mykey |".前者产生错误,而后者使值成为空字符串.像这样运行生产者:

I've tried producing "mykey" and "mykey|". The former produces an error and the later makes the value the empty string. Running producer like this:

$KAFKA_HOME/bin/kafka-console-producer --broker-list localhost:9092 --topic mytopic --property "parse.key=true" --property "key.separator=|"

推荐答案

不幸的是,无法使用console-producer来做到这一点

Unfortunately, there is no way to do that using console-producer

这是ConsoleProducer类的代码片段(它如何读取数据). Kafka 0.11.0(不要认为它在不同版本之间进行了重大更改).

this is a code snippet from ConsoleProducer class (how it reads the data). Kafka 0.11.0 (don't think that it was changed significantly between different versions).

override def readMessage() = { lineNumber += 1 print(">") (reader.readLine(), parseKey) match { case (null, _) => null case (line, true) => line.indexOf(keySeparator) match { case -1 => if (ignoreError) new ProducerRecord(topic, line.getBytes(StandardCharsets.UTF_8)) else throw new KafkaException(s"No key found on line $lineNumber: $line") case n => val value = (if (n + keySeparator.size > line.size) "" else line.substring(n + keySeparator.size)).getBytes(StandardCharsets.UTF_8) new ProducerRecord(topic, line.substring(0, n).getBytes(StandardCharsets.UTF_8), value) } case (line, false) => new ProducerRecord(topic, line.getBytes(StandardCharsets.UTF_8)) } }

如您所见,该值始终是一个不可为空的字节数组

as you can see, the value is always an non-nullable array of bytes

更多推荐

从控制台产生具有空值(墓碑)的Kafka消息

本文发布于:2023-11-15 16:57:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   墓碑   消息   Kafka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!