• 查看topic信息 ```shell ./kafka-topics.sh —describe —zookeeper zk:2181 —topic topicName

25/03/08 16:05:29 INFO zkclient.ZkClient: zookeeper state changed (SyncConnected) Topic:topicName PartitionCount:3 ReplicationFactor:3 Configs: Topic: topicName Partition: 0 Leader: 52 Replicas: 52,54,53 Isr: 52,54,53 Topic: topicName Partition: 1 Leader: 53 Replicas: 53,52,54 Isr: 53,52,54 Topic: topicName Partition: 2 Leader: 54 Replicas: 54,53,52 Isr: 54,53,52

  1. 此时topic数据为kafka默认的配置
  2. - 修改已有的topic数据保存时间(某一个)
  3. ```shell
  4. ./kafka-topics.sh --zookeeper zk:2181 -topic topicName --alter --config retention.ms=2678400000
  5. 19/04/09 16:07:29 INFO zkclient.ZkClient: zookeeper state changed (SyncConnected)
  6. Topic:topicName PartitionCount:3 ReplicationFactor:3 Configs:retention.ms=2678400000
  7. Topic: topicName Partition: 0 Leader: 52 Replicas: 52,54,53 Isr: 52,54,53
  8. Topic: topicName Partition: 1 Leader: 53 Replicas: 53,52,54 Isr: 53,52,54
  9. Topic: topicName Partition: 2 Leader: 54 Replicas: 54,53,52 Isr: 54,53,52

可以看到Configs后面有个retention.ms=2678400000参数,改参数是我们修改的参数
注:kafka-topics.sh修改配置已被弃用,后续版本会删除,建议需要统一使用kafka-configs.sh来修改参数。

kafka-configs.sh使用

  • 查看topic修改的配置信息 ```shell

    ./kafka-configs.sh —describe —zookeeper zk:2181 —entity-type topics —entity-name topicName

Configs for topic ‘taopicName’ are retention.ms=2678400000

如果使用默认配置,则显示 Configs for topic ‘topicName’ are

  1. - 修改配置
  2. ```shell
  3. ./kafka-configs.sh --zookeeper zk:2181 --alter --entity-name topicName --entity-type topics --add-config retention.ms=60000

配置完成后,所有超过时间的数据会先被标记为deleted,稍后清空全部标记为deleted的文件

-rw-r—r— 1 kafka kafka 1073620774 11月 16 08:53 00000000000601378207.log.deleted -rw-r—r— 1 kafka kafka 156852 11月 16 08:53 00000000000601378207.timeindex.deleted -rw-r—r— 1 kafka kafka 108432 11月 16 08:57 00000000000601456930.index.deleted -rw-r—r— 1 kafka kafka 1073728104 11月 16 08:57 00000000000601456930.log.deleted -rw-r—r— 1 kafka kafka 157356 11月 16 08:57 00000000000601456930.timeindex.deleted -rw-r—r— 1 kafka kafka 111472 11月 16 09:00 00000000000601538370.index.deleted -rw-r—r— 1 kafka kafka 1073383897 11月 16 09:00 00000000000601538370.log.deleted -rw-r—r— 1 kafka kafka 161004 11月 16 09:00 00000000000601538370.timeindex.deleted -rw-r—r— 1 kafka kafka 111544 11月 16 09:04 00000000000601614505.index.deleted -rw-r—r— 1 kafka kafka 1073645607 11月 16 09:04 00000000000601614505.log.deleted -rw-r—r— 1 kafka kafka 161304 11月 16 09:04 00000000000601614505.timeindex.deleted -rw-r—r— 1 kafka kafka 113240 11月 16 09:08 00000000000601697084.index.deleted -rw-r—r— 1 kafka kafka 1073650121 11月 16 09:08 00000000000601697084.log.deleted -rw-r—r— 1 kafka kafka 164424 11月 16 09:08 00000000000601697084.timeindex.deleted -rw-r—r— 1 kafka kafka 113816 11月 16 09:12 00000000000601786671.index -rw-r—r— 1 kafka kafka 1073660828 11月 16 09:12 00000000000601786671.log -rw-r—r— 1 kafka kafka 164388 11月 16 09:12 00000000000601786671.timeindex

查看Kafka日志发现日志删除

  1. 09:11:30,784 INFO kafka.log.TimeIndex: Deleting index /kafka/data/log_lnk_data_flow-0/00000000000601378207.timeindex.deleted
  2. 09:11:30,785 INFO kafka.log.TimeIndex: Deleting index /kafka/data/log_lnk_data_flow-0/00000000000601456930.timeindex.deleted
  3. 09:11:30,786 INFO kafka.log.TimeIndex: Deleting index /kafka/data/log_lnk_data_flow-0/00000000000601301258.timeindex.deleted