kafka系列一之常用shell命令
Kafka支持的基本命令位于kafka安装目录的bin目录下。
1、启动Kakfa
Kafka 的运行依赖于 zookeeper,要先启动 zookeeper,可以启动 Kafka 内置的 zookeeper,也可以启动自己安装的zookeeper
# zookeeper启动命令
bin/zkServer.sh start
# 内置zookeeper启动命令
bin/zookeeper-server-start.sh config/zookeeper.properties
启动单节点kafka用于测试:
bin/kafka-server-start.sh config/server.properties
2、查看topic的帮助信息
bin/kafka-topics.sh --help
# --list --describe --create --alter --delete
3、新创建topic
# 创建测试主题
bin/kafka-topics.sh --create --topic users --zookeeper sandbox-hdp.hortonworks.com:2181 --config max.message.bytes=12800000 --config flush.messages=1 --partitions 3 --replication-factor 1
# create:创建topic动作
# topic:新建topic的名称
# zookeeper:kafka连接zk的连接url,该值和server.properties文件中的配置项{zookeeper.connect}一样
# config:当前topic上有效的参数值,参数列表参考文档为: Topic-level configuration
# partitions:当前创建的kafka分区数量,默认为1个
# replication-factor:每个分区的复制因子个数,默认1个
4、查看所有主题
bin/kafka-topics.sh --zookeeper sandbox-hdp.hortonworks.com:2181 --list
5、Topic信息修改
bin/kafka-topics.sh --zookeeper sandbox-hdp.hortonworks.com:2181 --alter --topic users --config max.message.bytes=128000
bin/kafka-topics.sh --zookeeper sandbox-hdp.hortonworks.com:2181 --alter --topic users
--partitions 6
# kafka分区数量只允许增加,不允许减少
6、删除topic
默认情况下kafka的Topic是无法直接删除的,需要进行相关参数配置,有两种方式:
方式一:通过delete命令删除后,手动将本地磁盘以及zk上的相关topic的信息删除即可;
方式二:配置server.properties文件,给定参数delete.topic.enable=true,重启kafka服务,此时执行delete命令表示允许进行Topic的删除。此处采取这种方式。
# 先配置server.properties文件,给定参数delete.topic.enable=true,重启kafka服务
bin/kafka-topics.sh --zookeeper sandbox-hdp.hortonworks.com:2181 --delete --topic users
7、启动kafka消费者
bin/kafka-console-consumer.sh --bootstrap-server sandbox-hdp.hortonworks.com:6667 --topic users --from-beginning
8、核查topic——users中的数据量,可以看到每个分区中的数据量
bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list sandbox-hdp.hortonworks.com:6667 --topic users -time -1 --offsets 1
9、重置offsets (offsets to zero for input topics for application grpUsers), 重置后可以再执行jar包重新向topic中导一次数据,数据不会重复。
bin/kafka-streams-application-reset.sh --zookeeper sanbox-hdp.hortonworks.com:2181
--bootstrap-servers sandbox-hdp.hortonworks.com:6667 --application-id grpUsers --input-topics users
PS:文档中sanbox-hdp.hortonworks.com:2181为虚拟机名与端口号。