【Apache Kafka】Kafka学习笔记

 

0x00 基本信息

一、Kafka架构图

二、环境信息

  • 服务器IP:xx.xx.xx.xx
  • 操做系统:CentOS7.2_x64
  • Kafka版本:kafka_2.12-1.1.0
  • Zookeeper版本:zookeeper-3.4.12

0x01 让Kafka运行起来

一、先启动Zookeeper

$ cd /opt/kafka/zookeeper-3.4.12/bin
$ zkServer.sh start

确认zookeeper是否成功启动:bootstrap

$ netstat -an | grep 2181
tcp6 0 0 :::2181 :::* LISTEN

返回以上结果说明成功启动,接下来就能够启动kafka服务并使用具体的kafka命令来进行相应操做了。服务器

二、启动kafka服务

$ cd /opt/kafka/kafka_2.12-1.1.0/bin
$ ./kafka-server-start.sh -daemon /opt/kafka/kafka_2.12-1.1.0/config/server.properties

确认kafka服务是否正常启动:架构

$ ps -ef | grep kafka

返回以上结果说明成功启动。tcp

0x02 Kafka指令

利用kafka指令进行具体的操做。spa

一、查看当前已有的topic

$ cd /opt/kafka/kafka_2.12-1.1.0/bin
$ ./kafka-topics.sh --list --zookeeper localhost:2181

二、建立topic test-topic的生产者

$ ./kafka-console-producer.sh --broker-list PLAINTEXT://xx.xx.xx.xx:9092 --topic test-topic

三、建立topic test-topic的消费者

./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test-topic --from-beginning

这样,在2中建立的生产者命令行中发送的消息,3中建立的消费者就能够及时接收到。
注意:上面截图中提示旧的消费者建立方式将会废弃,建议使用bootstrap-server这种新的方式,以下:操作系统

./kafka-console-consumer.sh --bootstrap-server xx.xx.xx.xx:9092 --topic test-topic --from-beginning

四、建立新topic

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic my-test-topic

五、查看某个topic的详细信息

./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test-topic

六、修改已有topic

# 已经存在topic my-test-topic,且该topic以前分区数为1,如今咱们将该topic的分区数修改为2
./kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-test-topic --partitions 2

七、删除某个topic

./kafka-topics.sh --zookeeper localhost:2181 --delete --topic my-test-topic

 本文到此结束。.net

相关文章
相关标签/搜索