apache-kafka - 即使在保留时间/大小之后,数据仍然保留在 Kafka 主题中

标签 apache-kafka

我们设置了log retention hours到 1 小时如下(之前设置为 72H)
使用下面的Kafka命令行工具,我们设置kafka retention.ms1H .我们的目标是清除主题中早于 1H 的数据 - test_topic ,所以我们使用了以下命令:

kafka-configs.sh --alter \
  --zookeeper localhost:2181  \
  --entity-type topics \
  --entity-name topic_test \
  --add-config retention.ms=3600000
并且
kafka-topics.sh --zookeeper localhost:2181 --alter \
  --topic topic_test \
  --config retention.ms=3600000
两个命令都运行没有错误。
但问题在于 Kafka 数据比 1H 还旧并且仍然存在!
实际上没有数据从主题中删除 topic_test分区。我们有 HDP Kafka 集群版本 1.0x 和 ambari
我们不明白为什么关于主题的数据 - topic_test还留着?并且即使在我们按照已经描述的方式运行两个 cli 之后也没有减少
以下 kafka cli 有什么问题?
kafka-configs.sh --alter --zookeeper localhost:2181  --entity-type topics  --entity-name topic_test --add-config retention.ms=3600000

kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic_test --config retention.ms=3600000
来自卡夫卡 server.log我们可以看到以下内容
2020-07-28 14:47:27,394] INFO Processing override for entityPath: topics/topic_test with config: Map(retention.bytes -> 2165441552, retention.ms -> 3600000) (kafka.server.DynamicConfigManager)
[2020-07-28 14:47:27,397] WARN retention.ms for topic topic_test is set to 3600000. It is smaller than message.timestamp.difference.max.ms's value 9223372036854775807. This may result in frequent log rolling. (kafka.server.TopicConfigHandler)
引用 - https://ronnieroller.com/kafka/cheat-sheet

最佳答案

日志清理器仅适用于非事件(有时也称为“旧”或“干净”)段。只要所有数据都适合事件(“脏”、“不干净”)段,其大小由 segment.bytes 定义。大小限制将不会发生清洁。
配置cleanup.policy被描述为:

A string that is either "delete" or "compact" or both. This string designates the retention policy to use on old log segments. The default policy ("delete") will discard old segments when their retention time or size limit has been reached. The "compact" setting will enable log compaction on the topic.


此外,segment.bytes是:

This configuration controls the segment file size for the log. Retention and cleaning is always done a file at a time so a larger segment size means fewer files but less granular control over retention.


配置segment.ms也可用于引导删除:

This configuration controls the period of time after which Kafka will force the log to roll even if the segment file isn't full to ensure that retention can delete or compact old data.


由于它默认为一周,您可能希望减少它以满足您的需要。
因此,如果要将主题的保留设置为例如您可以设置一小时:
cleanup.policy=delete
retention.ms=3600000
segment.ms=3600000
file.delete.delay.ms=1 (The time to wait before deleting a file from the filesystem)
segment.bytes=1024
注意:我不是指 retention.bytes . segment.bytes如上所述,这是一个非常不同的事情。另外,请注意 log.retention.hours是集群范围的配置。因此,如果您计划为不同的主题设置不同的保留时间,这将解决它。

关于apache-kafka - 即使在保留时间/大小之后,数据仍然保留在 Kafka 主题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63137084/

相关文章:

docker - Apache Kafka 3.X 集群 : requires already started zookeeper: Exception: Unable to parse PLAINTEXT://127. 0.0.1:到代理端点 -

apache-spark - Spark Streaming + Kafka : SparkException: Couldn't find leader offsets for Set

python - Kafka producer.send 从不发送消息

java - Kafka主题分区和Spark执行器映射

go - 动态添加Kafka主题以从中使用,而无需重新启动GoLang应用程序

apache-kafka - 无法访问kafka.serializer.StringDecoder

java - 使用spring kafka时如何在生产者中压缩数据

apache-spark - 即使将 "auto.offset.reset"设置为 "latest"后也会出现错误 OffsetOutOfRangeException

java - Spring Cloud Stream 手动轮询器 Kafka

apache-kafka - 每个主题的 Kafka 保留.bytes 和全局 log.retention.bytes 不起作用