topic创建,查询删除
./bin/kafka-topics.sh --help
创建topic
其中 --bootstrap-server --create 和 --topic 是三个必须提供的参数。
// kafka安装目录
./bin/kafka-topics.sh --bootstrap-server 192.167.0.1:9092,192.167.0.2:9092 --create --replication-factor 2 --partition 2 --topic user-test
replication-factor:用来设置主题的副本数
partition:用来设置主题的分区数
查询topic列表
其中 --list 是必须提供的参数。
// kafka安装目录
./bin/kafka-topics.sh --bootstrap-server 192.167.0.1:9092,192.167.0.2:9092 --list
删除topic
其中 --bootstrap-server --delete 和 --topic 是三个必须提供的参数。
// kafka安装目录
./bin/kafka-topics.sh --bootstrap-server 192.167.0.1:9092,192.167.0.2:9092 --delete --topic user-test
注意:需要在Broker的配置文件server.properties中配置 delete.topic.enable=true 才能真正删除主题。
topic生产
// 查看命令相关参数
./bin/kafka-console-producer.sh --help
其中 --broker-list 和 --topic 是两个必须提供的参数。
常用命令如下。
// 使用标准输入方式
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
// 从文件读取:
./bin/kafka-console-producer.sh --broker-list 192.167.0.1:9092,192.167.0.2:9092 --topic test < file-input.txt
消费topic
// 查看命令相关参数
./bin/kafka-console-consumer.sh --help
其中 --bootstrap-server 和 --topic 是两个必须提供的参数。
// kafka安装目录
./bin/kafka-console-consumer.sh --bootstrap-server 192.167.0.1:9092,192.167.0.2:9092 --topic user-test --from-beginning
from-beginning: 从未消费数据中最开始的地方消费
查询topic最新offset值
// 查看命令相关参数
./bin/kafka-run-class.sh --help
其中 --broker-list 和 --topic 是两个必须提供的参数。
// kafka安装目录
./bin/kafka-run-class.sh kafka.tools.GetOffShell --broker-list 192.167.0.1:9092,192.167.0.2:9092 --topic user-test
查询结果[topic:partition:offset]:
test-user:0:1:890
test-user:1:900
由此可以看出,test-user的topic分2个分区,每个分区中offset对应值为多少。