Kafka 默认将消息保留 168 小时,即 7 天。如果你想强制 kafka 清除主题,你可以通过多种方式做到这一点。让我们详细了解一下。
1.使用 kafka-configs.sh 命令
暂时将保留政策更改为 1 秒。
kafka-configs.sh --zookeeper localhost:2181 --alter --entity-type topics --add-config retention.ms=1000 --entity-name text_topic
您可以通过运行以下命令来检查保留策略的当前值。
kafka-configs.sh --zookeeper localhost:2181 --entity-type topics --describe --entity-name text_topic
Configs for topic 'text_topic' are retention.ms=1000
等待 1 秒并删除保留策略配置,这会将其设置回默认值。
kafka-configs.sh --zookeeper localhost:2181 --alter --entity-type topics --delete-config retention.ms --entity-name text_topic
2。删除主题并重新创建
在我们删除现有主题之前,首先获取当前主题的分区和副本,因为您需要这些来重新创建主题。您可以通过运行主题的描述来获取此信息
kafka-topics.sh --zookeeper localhost:2181 --describe --topic text_topic
Topic:text_topic PartitionCount:3 ReplicationFactor:3 Configs:
Topic: text_topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
删除主题。
kafka-topics.sh --zookeeper localhost:2181 --delete --topic text_topic
使用复制和分区详细信息重新创建主题。
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic text_topic
3.手动从 kafka 日志中删除数据。
- 从所有节点停止 zookeeper 和 kafka。
- 清除所有节点的 kafka 日志。 kafka 将其日志文件存储在
/tmp/kafka-logs/MyTopic-0 其中 /tmp/kafka-logs 由
log.dirattribute
- 重启 zookeeper 和 kafka。
希望这会有所帮助!!