kafka四个核心概念:producer、consumer、broker、topic
-
启动zk
下载zk zookeeper-3.4.5-cdh5.7.0.tar.gz
解压:tar -zxvf zookeeper-3.4.5-cdh5.7.0.tar.gz -C /home/hadoop/app
修改zk conf/zoo.cfg文件:
dataDir=/home/hadoop/app/tmp/zk
clientPort=2181
启动zk ./zkServer.sh start -
kafka单节点单broker部署及使用
- 下载
下载 kafka_2.11-0.9.0.0.tgz
解压 tar -zxvf kafka_2.11-0.9.0.0.tgz -C /home/hadoop/app - 配置修改
vim server.properties修改如下配置
log.dirs=/home/hadoop/app/tmp/kafka-logs
zookeeper.connect=hadoop000:2181
host.name=hadoop000 - 启动kafka
kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties &
启动成功后jps会看到一个Kafka进程 - 创建topic
创建topic:
kafka-topics.sh --create --zookeeper hadoop000:2181 --replication-factor 1 --partitions 1 --topic hello_topic
查看所有topic:
kafka-topics.sh --list --zookeeper hadoop000:2181 - 发送和消费消息
创建生产者:
kafka-console-producer.sh --broker-list hadoop000:9092 --topic hello_topic
创建消费者:
kafka-console-consumer.sh --zookeeper hadoop000:2181 --topic hello_topic
-
单节点多broker部署及使用
-
配置修改
server-1.properties:
log.dirs=/home/hadoop/app/tmp/kafka-logs-1
listeners=PLAINTEXT://:9093
broker.id=1
zookeeper.connect=hadoop000:2181
host.name=hadoop000server-2.properties:
log.dirs=/home/hadoop/app/tmp/kafka-logs-2
listeners=PLAINTEXT://:9094
broker.id=2
zookeeper.connect=hadoop000:2181
host.name=hadoop000server-3.properties:
log.dirs=/home/hadoop/app/tmp/kafka-logs-3
listeners=PLAINTEXT://:9095
broker.id=3
zookeeper.connect=hadoop000:2181
host.name=hadoop000 -
启动
kafka-server-start.sh -daemon $KAFKA_HOME/config/server-1.properties &
kafka-server-start.sh -daemon $KAFKA_HOME/config/server-2.properties &
kafka-server-start.sh -daemon $KAFKA_HOME/config/server-3.properties &
启动成功后jps会看到3个Kafka进程
-
kafka常用命令
- 创建topic(--zookeeper 指定zk,--replication-factor 指定副本数量,--partitions指定分片数,--topic指定topic名称):
kafka-topics.sh --create --zookeeper hadoop000:2181 --replication-factor 1 --partitions 1 --topic streamingtopic - 查询有哪些topic(--zookeeper指定zk)
kafka-topics.sh --list --zookeeper hadoop000:2181 - 查询topic详细信息(--zookeeper指定zk,--topic指定topic名称)
kafka-topics.sh --describe --zookeeper hadoop000:2181 --topic streamingtopic - 创建生产者(--broker-list指定broker,多个broker逗号分隔)
kafka-console-producer.sh --broker-list hadoop000:9093,hadoop000:9094,hadoop000:9095 --topic streamingtopic - 创建消费者(--zookeeper指定zk,--from-beginning指定从第一条开始消费,可选)
kafka-console-consumer.sh --zookeeper hadoop000:2181 --topic streamingtopic --from-beginning - 删除topic
kafka-topics.sh --delete --zookeeper hadoop000:2181 --topic streamingtopic
----这样的操作并没有彻底删除掉topic,仍然存在,仅仅是标志为delete,用户不能创建相同名称的topic。 - 彻底删除topic:
/kafka-topics.sh --delete --zookeeper hadoop000:2181 --topic webchatf_topic
----先把topic标记为已删除
进入到zookeeper目录下
./bin/zkCli.sh ----进入到zk客户端
rmr /brokers/topics/hadoop000----即可彻底删除topic
- 创建topic(--zookeeper 指定zk,--replication-factor 指定副本数量,--partitions指定分片数,--topic指定topic名称):
网友评论