一、主题管理:创建主题
### --- 方案一:创建主题
~~~ # 创建主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic topic_x \
--partitions 3 --replication-factor 1
### --- 查看创建的主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
topic_x
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/ | grep topic_x
topic_x-0
topic_x-1
topic_x-2
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/topic_x-0/
00000000000000000000.index 00000000000000000000.log 00000000000000000000.timeindex leader-epoch-checkpoint
### --- 在zookeeper下会有创建的记录
~~~ 创建主题会和zookeeper进行交互
[zk: localhost:2181(CONNECTED) 1] get /myKafka/config/topics/topic_x
{"version":1,"config":{}}
cZxid = 0x4fb
ctime = Wed Sep 22 21:15:15 CST 2021
mZxid = 0x4fb
mtime = Wed Sep 22 21:15:15 CST 2021
pZxid = 0x4fb
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 25
numChildren = 0
### --- 方案二:创建主题
~~~ cleanup.policy:清理
~~~ compact:压缩存储
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic \
topic_y --partitions 2 --replication-factor 1 --config cleanup.policy=compact
~~~ # 查看创建的主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
topic_x
topic_y
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/ | grep topic_y
topic_y-0
topic_y-1
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/topic_y-0/
00000000000000000000.index 00000000000000000000.log 00000000000000000000.timeindex leader-epoch-checkpoint
~~~ # 在zookeeper下会有创建的记录
[zk: localhost:2181(CONNECTED) 2] get /myKafka/config/topics/topic_x
{"version":1,"config":{}}
cZxid = 0x4fb
ctime = Wed Sep 22 21:15:15 CST 2021
mZxid = 0x4fb
mtime = Wed Sep 22 21:15:15 CST 2021
pZxid = 0x4fb
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 25
numChildren = 0
### --- 方案三:创建主题
~~~ --config compression.type:指定压缩格式为gzip
~~~ --config max.message.bytes:指定压缩的大小
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic \
> topic_z --partitions 5 --replication-factor 1 \
> --config compression.type=gzip --config max.message.bytes=512
~~~ # 查看创建的主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
__consumer_offsets
topic_x
topic_y
topic_z
tp_demo_01
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/ | grep topic_z
topic_z-0
topic_z-1
topic_z-2
topic_z-3
topic_z-4
[root@hadoop ~]# ls /opt/yanqi/servers/kafka/kafka-logs/topic_z-0/
00000000000000000000.index 00000000000000000000.log 00000000000000000000.timeindex leader-epoch-checkpoint
~~~ # 在zookeeper下会有创建的记录
[zk: localhost:2181(CONNECTED) 0] get /myKafka/config/topics/topic_z
{"version":1,"config":{"max.message.bytes":"512","compression.type":"gzip"}}
cZxid = 0x51f
ctime = Wed Sep 22 21:37:24 CST 2021
mZxid = 0x51f
mtime = Wed Sep 22 21:37:24 CST 2021
pZxid = 0x51f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 76
numChildren = 0
二、查看主题
### --- 查看主题
~~~ # 列出所有可用的主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
__consumer_offsets
topic_x
topic_y
topic_z
### --- 查看某一个主题的详细信息
~~~ Topic:topic_x 主题名称
~~~ PartitionCount:3 主题有多少个分区
~~~ ReplicationFactor:1 主题有多少个副本
~~~ Partition: 3 分区编号
~~~ Leader: 0 分区的首领子那个broker上,在0号broker上,因为现在只有一个节点
~~~ Replicas: 0 副本在0号broker上
~~~ Isr: 0 同一个副本,在0号分区上
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --describe --topic topic_x
Topic:topic_x PartitionCount:3 ReplicationFactor:1 Configs:
Topic: topic_x Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: topic_x Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: topic_x Partition: 2 Leader: 0 Replicas: 0 Isr: 0
### --- 查看覆盖过默认设置的主题
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --topics-with-overrides --describe
Topic:__consumer_offsets PartitionCount:50 ReplicationFactor:1 Configs:segment.bytes=104857600,cleanup.policy=compact,compression.type=producer
Topic:topic_y PartitionCount:2 ReplicationFactor:1 Configs:cleanup.policy=compact
Topic:topic_z PartitionCount:5 ReplicationFactor:1 Configs:compression.type=gzip,max.message.bytes=512