# kafka安装目录
[root@fan102 kafka]# pwd
/opt/cloudera/parcels/KAFKA/lib/kafka
# 创建topic 主题名:topic_test 分区数:3 副本数:2
[root@fan102 kafka]# ./bin/kafka-topics.sh --create --zookeeper fan102:2181 --topic topic_test --partitions 3 --replication-factor 2
1.1.2结果展示
1.1.2.1 日志
[root@fan102 kafka]# ./bin/kafka-topics.sh --zookeeper fan102:2181 --list
1.3 某个topic详细描述
[root@fan102 kafka]# ./bin/kafka-topics.sh --describe --zookeeper fan102:2181 --topic topic_test
1.4.1 修改topic保存是时长 retention.ms这个参数是毫秒数
[root@fan102 kafka]# kafka-topics.sh --zookeeper node1:2181 -topic xxxx --alter --config retention.ms=259200000
1.4.2 或者修改配置文件server.properties 中 log.retention.hours
# 生产者
[root@fan102 ~]# kafka-console-producer --broker-list fan102:9092 --topic topic_test --producer.config /etc/kafka/conf/consumer.properties
# 消费者
[root@fan102 ~]# kafka-console-consumer --bootstrap-server fan102:9092 --topic topic_test --from-beginning --consumer.config /etc/kafka/conf/consumer.properties
输入内容
>hello
>moto
3 导出消息文件
[root@fan102 kafka]# ./bin/kafka-run-class.sh kafka.tools.DumpLogSegments --files /var/local/kafka/data/topic_test-0/00000000000000000110.log --print-data-log > 00000000000000000110.txt
3.2 结果
# 导出的.txt文件内容
Dumping /var/local/kafka/data/topic_test-0/00000000000000000110.log
Starting offset: 110
baseOffset: 110 lastOffset: 110 count: 1 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 26 isTransactional: false isControl: false position: 0 CreateTime: 1595936255468 size: 71 magic: 2 compresscodec: NONE crc: 2806001368 isvalid: true
| offset: 110 CreateTime: 1595936255468 keysize: -1 valuesize: 3 sequence: -1 headerKeys: [] payload: hello
baseOffset: 111 lastOffset: 111 count: 1 baseSequence: -1 lastSequence: -1 producerId: -1 producerEpoch: -1 partitionLeaderEpoch: 26 isTransactional: false isControl: false position: 71 CreateTime: 1595936258382 size: 73 magic: 2 compresscodec: NONE crc: 3650597 isvalid: true
| offset: 111 CreateTime: 1595936258382 keysize: -1 valuesize: 5 sequence: -1 headerKeys: [] payload: moto
4 ZK下查看topic
[root@fan102 ~]# cd /opt/cloudera/parcels/CDH/lib/zookeeper/bin
[root@fan102 ~]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[cluster, controller, brokers, zookeeper, hadoop-ha, admin, isr_change_notification, log_dir_event_notification, controller_epoch, consumers, hive_zookeeper_namespace_hive, latest_producer_id_block, config]
[zk: localhost:2181(CONNECTED) 1] ls /brokers
[ids, topics, seqid]
[zk: localhost:2181(CONNECTED) 2] ls /brokers/ids
[66, 67, 65]
[zk: localhost:2181(CONNECTED) 3] ls /brokers/topics
[topic_test, test, __consumer_offsets, topic_start]
5 删除topic
5.1 标记删除
[root@fan102 kafka]# ./bin/kafka-topics --delete --zookeeper fan102:2181 --topic topic_test
如果kafaka启动时加载的配置文件中server.properties没有配置"delete.topic.enable=true",那么此时的删除并不是真正的删除,而是把topic标记为删除:marked for deletion。
5.2 彻底删除topic方式
5.2.1 删除kafka存储目录(原生的Hadoop集群目录为server.properties文件中log.dirs所指定的位置,CDH版本的位置为/var/local/kafka/data/)相关topic目录
5.2.2.1 如果配置了delete.topic.enable=true直接通过命令删除,如果命令删除不掉,直接通过zookeeper-client 删除掉broker下的topic即可。
5.2.2.2 从Zookeeper中删除
[root@fan102 ~]# cd /opt/cloudera/parcels/CDH/lib/zookeeper/bin
[root@fan102 ~]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/topics
[topic_test, test, __consumer_offsets,topic_start]
[zk: localhost:2181(CONNECTED) 1] rmr /brokers/topics/topic_test
+++++++++++++++++++++++++++++++++++++++++
+ 如有问题可+Q:1602701980 共同探讨 +
+++++++++++++++++++++++++++++++++++++++++