在运行时更改 kafka 保留期

changing kafka retention period during runtime

使用 Kafka 0.8.1.1,如何在 运行 时更改日志保留时间? documentation 说 属性 是 log.retention.hours,但试图使用 kafka-topics.sh 更改它 returns 这个错误

$ bin/kafka-topics.sh --zookeeper zk.yoursite.com --alter --topic as-access --config topic.log.retention.hours=24
Error while executing topic command requirement failed: Unknown configuration "topic.log.retention.hours".
java.lang.IllegalArgumentException: requirement failed: Unknown configuration "topic.log.retention.hours".
    at scala.Predef$.require(Predef.scala:145)
    at kafka.log.LogConfig$$anonfun$validateNames.apply(LogConfig.scala:138)
    at kafka.log.LogConfig$$anonfun$validateNames.apply(LogConfig.scala:137)
    at scala.collection.Iterator$class.foreach(Iterator.scala:631)
    at scala.collection.JavaConversions$JEnumerationWrapper.foreach(JavaConversions.scala:479)
    at kafka.log.LogConfig$.validateNames(LogConfig.scala:137)
    at kafka.log.LogConfig$.validate(LogConfig.scala:145)
    at kafka.admin.TopicCommand$.parseTopicConfigsToBeAdded(TopicCommand.scala:171)
    at kafka.admin.TopicCommand$$anonfun$alterTopic.apply(TopicCommand.scala:95)
    at kafka.admin.TopicCommand$$anonfun$alterTopic.apply(TopicCommand.scala:93)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:57)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:43)
    at kafka.admin.TopicCommand$.alterTopic(TopicCommand.scala:93)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:52)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)

正确的配置键是retention.ms

$ bin/kafka-topics.sh --zookeeper zk.prod.yoursite.com --alter --topic as-access --config retention.ms=86400000
Updated config for topic "my-topic".

log.retention.hours 是代理的 属性 ,在创建主题时用作默认值。当您使用 kafka-topics.sh 更改当前 运行 主题的配置时,您应该指定主题级别 属性.

日志保留时间的主题级别 属性 是 retention.ms

来自 Kafka 0.8.1 文档中的 Topic-level configuration

  • 属性: retention.ms
  • 默认:7 天
  • 服务器默认 属性:log.retention.minutes
  • 说明:如果我们使用“删除"保留政策。这表示消费者必须多快读取数据的 SLA。

所以正确的命令取决于版本。直到 0.8.2 (although docs still show its use up to 0.10.1) use kafka-topics.sh --alter and after 0.10.2 (or perhaps from 0.9.0 以后)使用 kafka-configs.sh --alter

$ bin/kafka-topics.sh --zookeeper zk.yoursite.com --alter --topic as-access --config retention.ms=86400000
 

您可以使用以下命令检查配置是否正确应用。

$ bin/kafka-topics.sh --describe --zookeeper zk.yoursite.com --topic as-access

然后你会看到类似下面的内容。

Topic:as-access  PartitionCount:3  ReplicationFactor:3  Configs:retention.ms=86400000

从 Kafka 0.10.2.0 开始,以下是更改主题配置的正确方法:

bin/kafka-configs.sh --zookeeper <zk_host> --alter --entity-type topics --entity-name test_topic --add-config retention.ms=86400000

bin/kafka-topics.sh 已弃用主题配置更改操作。

WARNING: Altering topic configuration from this script has been deprecated and may be removed in future releases.
     Going forward, please use kafka-configs.sh for this functionality`

我在kafka confluent V4.0.0apache kafka V 1.0.0 and 1.0.1

中测试并使用了这个命令
/opt/kafka/confluent-4.0.0/bin/kafka-configs --zookeeper XX.XX.XX.XX:2181 --entity-type topics --entity-name test --alter --add-config  retention.ms=55000

test是主题名称。

我认为它在其他版本中也很好用