【kafka】保留topic,删除kafka中的数据的方法
背景:topic中如果数据格式有问题,消费时采用消费后自动提交offset的方式,会出现消费端程序一直无法消费而一直报错的情况。
解决办法:将该topic的数据保留时间设置小一点,如10s。
kafka是采用轮训的方式,轮训到这个topic发现10秒前的数据都是删掉。时间由server.properties里面的log.retention.check.interval.ms选项为主
操作过程:
- 本地windows版本:(zk为默认的2181端口)
-
查看名为test的topic 其数据保留时间策略
kafka-configs.bat --zookeeper localhost:2181 --describe --entity-type topics --entity-name test
-
修改数据保留时间为1s
kafka-configs.bat --zookeeper localhost:2181 --entity-type topics --entity-name test --alter --add-config retention.ms=1000
-
当数据都被清空后,恢复默认的数据保留时间策略
kafka-configs.bat --zookeeper localhost:2181 --entity-type topics --entity-name test --alter --delete-config retention.ms
-
检验名为test的topic 其数据保留时间策略
kafka-configs.bat --zookeeper localhost:2181 --describe --entity-type topics --entity-name test
按道理是无需重启kafka的,但是修改保留时间的过程中,kafka的启动进程会报错终止,这个时候再启动下kafka就好
- linux版本:
- 修改数据保留时间为1s
./kafka-configs.sh --zookeeper zk-1:2181 --entity-type topics --entity-name test --alter --add-config retention.ms=1000
- 当数据都被清空后,恢复默认的数据保留时间策略
./kafka-configs.sh --zookeeper zk-1:2181 --entity-type topics --entity-name test --alter --delete-config retention.ms
- 检验名为test的topic 其数据保留时间策略
./kafka-configs.sh --zookeeper zk-1:2181 --describe --entity-type topics --entity-name test
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?