参考:
https://www.orchome.com/6
https://www.cnblogs.com/xiaodf/p/6093261.html#1
https://my.oschina.net/Howard2016/blog/861521
基本命令
进入kafka安装路径:
[root@data1 kafka_2.10-0.10.2.1]# pwd
/tools/kafka_2.10-0.10.2.1
启动kafka:
sh bin/kafka-server-start.sh config/server.properties &
启动后控制台会自动打印启动日志(注:命令后边加“&”,ctrl + c 就不会结束kafka进程)
[root@data1 kafka_2.10-0.10.2.1]# sh bin/kafka-server-start.sh config/server.properties &
[1] 9586
[root@data1 kafka_2.10-0.10.2.1]#
[root@data1 kafka_2.10-0.10.2.1]#
[root@data1 kafka_2.10-0.10.2.1]#
[root@data1 kafka_2.10-0.10.2.1]# [2020-05-09 09:41:39,767] INFO KafkaConfig values:
advertised.host.name = null
advertised.listeners = PLAINTEXT://192.168.0.201:9092
advertised.port = null
authorizer.class.name =
auto.create.topics.enable = true
auto.leader.rebalance.enable = true
background.threads = 10
broker.id = 0
broker.id.generation.enable = true
broker.rack = null
compression.type = producer
connections.max.idle.ms = 600000
Topic
-
创建主题(topic),创建一个名为“test”的Topic,只有一个分区和一个备份:
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
或者,除了手工创建topic外,你也可以配置你的broker,当发布一个不存在的topic时自动创建topic -
查看所有topic列表:
sh bin/kafka-topics.sh --list --zookeeper 192.168.0.201:2181
[root@data1 kafka_2.10-0.10.2.1]# sh bin/kafka-topics.sh --list --zookeeper 192.168.0.201:2181
ETL-001
ETL-002
ETL-003
__consumer_offsets
dashboard
baseline
globalLog
[root@data1 kafka_2.10-0.10.2.1]#
- 查看topic详情:
[root@data1 kafka_2.10-0.10.2.1]# sh bin/kafka-topics.sh --describe --zookeeper 192.168.0.201:2181 --topic dashboard
Topic:dashboard PartitionCount:5 ReplicationFactor:2 Configs:
Topic: dashboard Partition: 0 Leader: 0 Replicas: 0,1 Isr: 1,0
Topic: dashboard Partition: 1 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: dashboard Partition: 2 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: dashboard Partition: 3 Leader: 3 Replicas: 3,0 Isr: 0,3
Topic: dashboard Partition: 4 Leader: 0 Replicas: 0,2 Isr: 2,0
[root@data1 kafka_2.10-0.10.2.1]#
-
consumer topic所有数据:
sh bin/kafka-console-consumer.sh --bootstrap-server 192.168.0.201:9092 --topic [topicName] --from-beginning -
consumer topic数据(相当于tail -f命令):
sh bin/kafka-console-consumer.sh --bootstrap-server 192.168.0.201:9092 --topic [topicName] -
发送消息(注:kafka集群生产消息与单点命令一样)
Kafka提供了一个命令行的工具,可以从输入文件或者命令行中读取消息并发送给Kafka集群。每一行是一条消息。
运行producer(生产者),然后在控制台输入几条消息到服务器(注:直接回车输入空也是一条消息,但没有内容)。
bin/kafka-console-producer.sh --broker-list 192.168.0.201:9092 --topic test
This is a message
This is another message
网友评论