在大数据部门经常使用Kafka集群,有的时候大数据部门可能在Kafka中的Topic数据保存时间不需要很长,一旦被消费后就不需要一直保留。默认Topic存储时间为7day,个别的Topic或者某台Kafka集群需要修改Topic数据保存的一个周期,调整为3天或者1天
目前修改Topic 有两种方法
以上两种方式均进行演示
相关版本信息
- [root@web-03 ~]# /opt/kafka/bin/kafka-topics.sh --version
- 3.4.0 (Commit:2e1947d240607d53)
-
- [root@web-03 ~]# cat /etc/redhat-release
- CentOS Linux release 7.4.1708 (Core)
-
- [root@web-03 ~]# uname -r
- 3.10.0-693.el7.x86_64
修改配置文件
- [root@web-03 ~]# vim /opt/kafka/config/server.properties
-
- # The minimum age of a log file to be eligible for deletion due to age
- log.retention.hours=12
-
- #单位为小时
如果是集群需要集群内其它机器也修改
修改完配置文件重启,配置文件是全局生效
首先我们先查看目前的Topic
- [root@web-03 ~]# /opt/kafka/bin/kafka-topics.sh --list --bootstrap-server 192.168.31.71:9092
- tw222
- two
使用describe参数查看详细Topic数值
- [root@web-03 ~]# /opt/kafka/bin/kafka-topics.sh --bootstrap-server 192.168.31.71:9092 --describe --topic tw222
- Topic: tw222 TopicId: qS-h_V78ShyKC6pN1cuCfw PartitionCount: 2 ReplicationFactor: 1 Configs: segment.bytes=1073741824
- Topic: tw222 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
- Topic: tw222 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
- [root@web-03 ~]#
使用kafka-configs.sh修改Topic名称为tw222
- [root@web-03 ~]# /opt/kafka/bin/kafka-configs.sh --bootstrap-server 192.168.31.71:9092 --alter --entity-name tw222 --entity-type topics --add-config retention.ms=86400000
- #下面为输出结果
- Completed updating config for topic tw222.
-
- #--entity-name 指定Topic名称 tw222
- #--entity-type topics 类型为Topic
- #retention.ms=数据保留时间
毫秒计算器可以访问下面的地址,由abcdocker维护
在线时间换算,年月周天换算,时间单位换算工具-abcdocker在线工具系统

接下来我们查看效果
- [root@web-03 ~]# /opt/kafka/bin/kafka-topics.sh --bootstrap-server 192.168.31.71:9092 --describe --topic tw222
- Topic: tw222 TopicId: qS-h_V78ShyKC6pN1cuCfw PartitionCount: 2 ReplicationFactor: 1 Configs: segment.bytes=1073741824,retention.ms=86400000
- Topic: tw222 Partition: 0 Leader: 0 Replicas: 0 Isr: 0
- Topic: tw222 Partition: 1 Leader: 0 Replicas: 0 Isr: 0
-
- #retention.ms=86400000这里表示已经为Topic添加了过期时间
