Kafka集群的启动:
先启动zookeeper集群:
注:可以编写zookeeper集群启动脚本
/opt/zookeeper-1/bin/zkServer.sh start
/opt/zookeeper-2/bin/zkServer.sh start
/opt/zookeeper-3/bin/zkServer.sh start
使用如下命令判断是否启动成功:
/opt/zookeeper-1/bin/zkServer.sh status
/opt/zookeeper-2/bin/zkServer.sh status
/opt/zookeeper-3/bin/zkServer.sh status
启动kafka集群:
注:可以编写kafka集群启动脚本
#后台启动:
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server1.properties
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server2.properties
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server3.properties
关闭集群:
/opt/kafka/bin/kafka-server-stop.sh stop
Kafka命令行操作:
查看当前服务器中的所有topic:
/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --list
创建topic:
/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --create --replication-factor 3 --partitions 1 --topic first
选项说明:
–topic 定义topic名
–replication-factor 定义副本数
–partitions 定义分区
删除topic:
/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --delete --topic topicname(主题名字)
发送消息(启动生产者):
/opt/kafka/bin/kafka-console-producer.sh --bootstrap-server 127.0.0.1:9092 --topic first
消费消息(启动消费者):
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic first
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --from-beginning --topic first
参数说明:
–from-beginning:会把主题中以往所有数据的数据都读出来
查看某个topic的详情:
/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --describe --topic first
修改分区数:(只能增加不能减少分区数)
/opt/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --alter --partitions 6 --topic first
读取offset:
/opt/kafka/bin/kafka-console-consumer.sh --topic __consumer_offsets --bootstrap-server 127.0.0.1:9092 --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" --consumer.config /opt/kafka/config/consumer.properties --from-beginning