Kafka 概览
主要版本
- Apache Kafka
- Confluent Kafka
Apache Kafka 使用
Start Zookeeper
shbin/zookeeper-server-start.sh config/zookeeper.properties
Zookeeper: 2181
Start Kafka Server
shbin/kafka-server-start.sh config/server.properties
Kafka Server / Broker: 9092
Create a Topic
shbin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic hjfrun-topic --partitions 3 --replication-factor 1 // Created topic hjfrun-topic.
查看当前所有 topics
shbin/kafka-topics.sh --bootstrap-server localhost:9092 --list
shhjfrun-topic jason-topic
查看某个 topic
shbin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic hjfrun-topic
shTopic: hjfrun-topic TopicId: lRxa4EWUR9OcE4Vr56H_zw PartitionCount: 3 ReplicationFactor: 1 Configs: Topic: hjfrun-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0 Topic: hjfrun-topic Partition: 1 Leader: 0 Replicas: 0 Isr: 0 Topic: hjfrun-topic Partition: 2 Leader: 0 Replicas: 0 Isr: 0
创建 consumer
shbin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic hjfrun-topic --from-beginning
创建 producer 发布单条消息
shbin/kafka-console-producer.sh --broker-list localhost:9092 --topic hjfrun-topic
console 演示效果Offset Explorer 查看结果
通过文件发布多条消息
shbin/kafka-console-producer.sh --broker-list localhost:9092 --topic hjfrun-topic < /Users/jianfeng/Downloads/Australia.csv
Confluent Kafka 使用
Start Zookeeper
shbin/zookeeper-server-start etc/kafka/zookeeper.properties
Zookeeper: 2181
Start Kafka Server
shbin/kafka-server-start etc/kafka/server.properties
Kafka Server / Broker: 9092
Create a Topic
shbin/kafka-topics --bootstrap-server localhost:9092 --create --topic confluent-hjfrun-topic --partitions 3 --replication-factor 1 // Created topic confluent-hjfrun-topic. bin/kafka-topics --bootstrap-server localhost:9092 --create --topic confluent-jason-topic --partitions 3 --replication-factor 1 ─╯ // Created topic confluent-jason-topic.
查看所有 topics
shbin/kafka-topics --bootstrap-server localhost:9092 --list
描述 topics
shbin/kafka-topics --bootstrap-server localhost:9092 --describe --topic confluent-hjfrun-topic Topic: confluent-hjfrun-topic TopicId: H_vWMTqOT32DSqEMcUSLvQ PartitionCount: 3 ReplicationFactor: 1 Configs: Topic: confluent-hjfrun-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0 Topic: confluent-hjfrun-topic Partition: 1 Leader: 0 Replicas: 0 Isr: 0 Topic: confluent-hjfrun-topic Partition: 2 Leader: 0 Replicas: 0 Isr: 0
创建 consumer
shbin/kafka-console-consumer --bootstrap-server localhost:9092 --topic confluent-hjfrun-topic --from-beginning
创建 producer 发布消息
shbin/kafka-console-producer --broker-list localhost:9092 --topic confluent-hjfrun-topic
发送 csv 数据到 kafka
shbin/kafka-console-producer --broker-list localhost:9092 --topic confluent-hjfrun-topic < /Users/jianfeng/Downloads/Australia.csv
Apache Kafka without ZooKeeper
Generate a Cluster UUID
shKAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
Format Log Directories
shbin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
Start the Kafka Server
shbin/kafka-server-start.sh config/kraft/server.properties
查看 meta data
shbin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --status ClusterId: mVgo5LTKRLqO9WnQGFXZJw LeaderId: 1 LeaderEpoch: 1 HighWatermark: 1330 MaxFollowerLag: 0 MaxFollowerLagTimeMs: 0 CurrentVoters: [1] CurrentObservers: [] bin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --replication NodeId LogEndOffset Lag LastFetchTimestamp LastCaughtUpTimestamp Status 1 2410 0 1714658720447 1714658720447 Leader