kafka3.x 简单使用

发布时间 2023-09-11 15:18:23作者: 你就学个JVAV?

*** 保证kafka和zookeeper已经在linux上进行了安装,目录需要改为自己的目录

 

*** kafka2.8之后引入了kraft机制,不用zookeeper也能启动

参数介绍

 --create 创建一个topic
 --topic  [your_topic_name] 创建的topic的信息
 --describe 描述信息
 --bootstrap-server [host_url] 指定的服务器地址
 --from-beginning 表示从什么地方开始获取信息

开启zookeeper

 /mydata/zookeeper/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start

开启kafka

 /mydata/kafka/kafka_2.12-3.5.0/bin/kafka-server-start.sh /mydata/kafka/kafka_2.12-3.5.0/config/server.properties

创建一个topic

 $ bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

显示使用信息和分区计数信息

 $ bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092
 Topic: quickstart-events        TopicId: NPmZHyhbR9y00wMglMH2sg PartitionCount: 1       ReplicationFactor: 1    Configs:
     Topic: quickstart-events Partition: 0    Leader: 0   Replicas: 0 Isr: 0

开启生产者,向其中写入消息

 & bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092
 >This is my first event
 >This is my second event

开启消费者,读取消息

 $ bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092
 This is my first event
 This is my second event