09、Kafka ------ 通过修改保存时间来删除消息(retention.ms 配置)

目录

通过修改保存时间来删除消息

★ 删除指定主题的消息

Kafka并没有提供直接删除特定主题下消息的方法,只能是强制让消息过期之后,再来删除消息。

因此需要指定如下两个配置:

  1. 控制将指定主题下消息的保存时间设为一个很短时间: retention.ms(为特定主题设置)

  2. 控制 Kafka 尽快去检查消息是否过期(默认是5分钟检查一次)。 log.retention.check.interval.ms

可通过如下两个属性来强制删除指定主题的消息

retention.ms属性:将该属性设为一个极短的时间,比如1000。

log.retention.check.interval.ms:设置Kafka轮询检查的时间间隔,比如将该该属性设为300000。

这意味着每5分钟,Kafka会对指定主题的消息执行一次检查,检查消息是否过期(超过retention.ms属性设置的时间值),如果过期则删除这些消息。

演示

官网显示:Kafka 默认是5分钟检查一次主题下是否有消息过期

1、修改kafka检查过期消息的时间间隔

为了方便演示,我这里改成120秒检查一次,演示完要记得注释掉这个配置,不然这么频繁检查会很卡。

配置修改后要重启下kafka。

2、修改主题下消息的过期时间

然后修改test2主题下的消息的保存时间,改成1秒,就是消息存在一秒后就过期。

▲ 将test2主题下的消息的保存时间设为1秒

复制代码
 kafka-configs --bootstrap-server localhost:9092 ^
 --alter ^
 --entity-name test2 ^
 --entity-type topics ^
 --add-config retention.ms=1000

3、查看修改是否生效

修改完消息的过期时间,那么就查看下是否生效:

命令查看:

使用 kafka-configs.bat 命令的 --describe 子命令

------该命令可查看所有对象的信息

复制代码
 kafka-configs --bootstrap-server localhost:9092 ^
 --describe ^
 --entity-name test2 ^
 --entity-type topics

主题下消息的保存(过期)时间已经修改成1秒了

CMAK界面查看:

4、先查看下主题下有没有消息

查看端口为9092的kafka节点,主题是test2,查看内容是从beginning开始就存在的消息:

复制代码
 kafka-console-consumer --bootstrap-server localhost:9092 ^
 --topic test2 ^
 --from-beginning ^
 --property print.timestamp=true ^
 --property print.key=true ^
 --property print.offset=true ^
 --property print.partition=true

此时这个test2主题下没有任何消息,为方便演示,先添加几条消息进去

5、添加几条消息看效果

添加带 key 的消息,相当于生产者发送消息到指定的主题里面去:

复制代码
kafka-console-producer.bat ^
--bootstrap-server localhost:9092 ^
--topic test2 ^
--property parse.key=true

然后马上查看,发现消息发送成功,test2主题下有这些消息:为了方便,我命令再拷贝一份:

复制代码
 kafka-console-consumer --bootstrap-server localhost:9092 ^
 --topic test2 ^
 --from-beginning ^
 --property print.timestamp=true ^
 --property print.key=true ^
 --property print.offset=true ^
 --property print.partition=true

再检查一遍消息的存活时间,是存活1秒没错

复制代码
 kafka-configs --bootstrap-server localhost:9092 ^
 --describe ^
 --entity-name test2 ^
 --entity-type topics

6、查看消息是否被删除

kafka每个120就会去检查是否有过期的消息,有的话就把过期的消息删除掉,我又把消息的过期时间设置为1秒,所以看过期的消息在120后,是否会被kafka检查到并删除掉:

接下来就等120秒,发现过期消息已经被成功删除了:

查看 test2 主题下,从beginning 一开始到现在的所有消息:

复制代码
 kafka-console-consumer --bootstrap-server localhost:9092 ^
 --topic test2 ^
 --from-beginning ^
 --property print.timestamp=true ^
 --property print.key=true ^
 --property print.offset=true ^
 --property print.partition=true

演示完,把这个设置给注释掉,恢复成默认的,默认是每5分钟检查一次。

★ 恢复主题的retention.ms配置

若要指定主题的retention.ms配置依然使用默认值,则只要使用kafka-configs.bat命令的 --delete-config 选项删除该配置即可。例如如下命令:

因为上面同通过 --add-config retention.ms=1000 把消息的过期时间设置为1秒,这个设置肯定是不合理的,所以需要把这个设置给删除掉。

1、先查看没修改前的test2主题的配置信息:

复制代码
 kafka-configs --bootstrap-server localhost:9092 ^
 --describe ^
 --entity-name test2 ^
 --entity-type topics

2、 将test2主题下的消息的保存时间删除。

(--delete-config retention.ms 就是删除 retention.ms 这个配置属性)

--alter ^ 表示修改的意思

复制代码
kafka-configs.bat --alter ^
--bootstrap-server localhost:9092 ^
--entity-type topics ^
--entity-name test2 ^
--delete-config retention.ms

3、再查看修改后的test2主题的配置信息:

命令行查看:

复制代码
 kafka-configs --bootstrap-server localhost:9092 ^
 --describe ^
 --entity-name test2 ^
 --entity-type topics

发现 retention.ms 这个配置属性 已经被成功删除了,表示消息的过期时间恢复成默认的168小时了。

这个就是默认的消息过期时间:

CMAK 查看:

retention.ms 这个配置属性恢复成默认的了

相关推荐
wanhengidc35 分钟前
服务器托管对企业的作用
大数据·运维·服务器·分布式·智能手机
Code知行合壹36 分钟前
Spark使用总结
大数据·分布式·spark
Swift社区38 分钟前
分布式能力不是功能,而是一种架构约束
分布式·架构
0xDevNull1 小时前
Apache Kafka 完全指南
分布式·kafka
zb200641202 小时前
RabbitMQ 客户端 连接、发送、接收处理消息
分布式·rabbitmq·ruby
半桶水专家3 小时前
Kafka JMX详解
分布式·kafka
渔民小镇3 小时前
告别 if-else 地狱 —— JSR380 参数验证在 ionet 中的应用
java·服务器·分布式·游戏
智慧科技的海洋3 小时前
微电网智慧平台:破解能源困局的分布式能源革命
分布式·能源
深蓝轨迹3 小时前
Redisson 分布式锁复习总结
分布式
yashuk4 小时前
RabbitMQ高级特性----生产者确认机制
分布式·rabbitmq