1.创建主题(当消息生产者向一个主题发布消息时,如果主题不存在,会自动创建一个)
##创建3个分区 两个副本 该主题名为test
##kafka-topics.sh 为kafka/bin 下的可执行文件
##zookeeper参数是必传参数,用于配置kafka集群与zookeeper链接地址。并不要求传所有的连接地址。为了容错,把所有的都写上(KAFKA会自动寻找可运行的节点)
##partitions参数用于设置主题的分区数,该配置为必传配置。KAFKA通过分区策略,将一个主题的消息分散到多个分区并分别保存到不同的代理上,一次来提高消息处理的吞吐量
#KAFKA的生产者和消费者可以采用多线程并行对主题消息进行处理,而每个线程处理的是一个分区的数据,因此分区实际上是KAFKA并行处理的基本单位。分区越多一定程度上会提高消息处理的吞吐量。然而KAFKA消息是一追加的形式存储在文件中,这就意味着分区越多需要打开的句柄就越多。会带来一定的开销
##--replication-factor也是必传配置。设置主题的副本数。副本会被分布在不同节点上,副本不能超过节点数,否则主题会创建失败。
##创建主题的时候还可以覆盖默认配置
##命令行后面追加 --config max.message.bytes=404800 设置该主题max.message.bytes为404800字节
kafka-topics.sh --create --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --replication-factor 2 --partitions 3 --topic test
2.删除主题
##只是标记为删除
kafka-topics.sh --delete --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topic test
3.查看主题
1.查看所有主题
1.kafka-topics.sh --list --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
2.kafka-topics.sh --describe --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
2.查看正在同步的主题
kafka-topics.sh --describe --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --under-replicated-partitions
3.查看没有leader的分区
kafka-topics.sh --describe --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --unavailable-parititions
4.查看主题覆盖配置
kafka-topics.sh --describe --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topics-with-overrides
5.修改主题
##5.1 修改主题配置
##5.1.1查看主题当前已覆盖的配置
kafka-topics.sh --describe --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topics-with-overrides --topic test1
##5.1.2修改配置
kafka-topics.sh --alter --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topic test1 --config max.message.bytes=204800
输出信息:
max.message.bytes=204800
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
WARNING: Altering topic configuration from this script has been deprecated and may be removed in future releases.
Going forward, please use kafka-configs.sh for this functionality
Updated config for topic test1
##5.1.3覆盖主题‘test1’的segment.bytes 大小为200MB
kafka-topics.sh --alter --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topic test1 --config segment.bytes=204800
输出信息:
segment.bytes=204800
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
WARNING: Altering topic configuration from this script has been deprecated and may be removed in future releases.
Going forward, please use kafka-configs.sh for this functionality
Updated config for topic test1.
##5.1.4 删除segment.bytes 并使其恢复为默认值
kafka-topics.sh --alter --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topic test1 --delete-config segment.bytes
##5.2 增加分区(KAFKA只能为主题增加分区,不能减少分区)
kafka-topics.sh --alter --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --topic test1 --parititions 5