环境准备
- 集群规划
hadoop-100 | hadoop-101 | hadoop-102 |
---|---|---|
zookeeper | zookeeper | zookeeper |
kafka | kafka | kafka |
- 下载jar包
下载地址 http://kafka.apache.org/kafka下载1.png
如上图,点downloadkafka下载2.png
kafka_2.11-2.0.1.tgz,前面的数字2.11是scala的版本,2.0.1才是kafka的版本
集群部署
- 解压安装包
[hadoop@hadoop-100 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C /opt/module/ - 修改压缩后的文件名称
[hadoop@hadoop-100 module]$ mv kafka_2.11-0.11.0.0/ kafka - 在/opt/module/kafka目录下创建logs文件夹
[hadoop@hadoop-100 kafka]$ mkdir logs - 修改配置文件
即修改config/server.properties文件
输入以下内容
#broker的全局唯一编号,不能重复
broker.id=0
#删除topic功能使能
delete.topic.enable=true
#处理网络请求的线程数量
num.network.threads=3
#用来处理磁盘IO的现成数量
num.io.threads=8
#发送套接字的缓冲区大小
socket.send.buffer.bytes=102400
#接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400
#请求套接字的缓冲区大小
socket.request.max.bytes=104857600
#kafka运行日志存放的路径
log.dirs=/opt/module/kafka/logs
#topic在当前broker上的分区个数
num.partitions=1
#用来恢复和清理data下数据的线程数量
num.recovery.threads.per.data.dir=1
#segment文件保留的最长时间,超时将被删除
log.retention.hours=168
#配置连接Zookeeper集群地址
zookeeper.connect=hadoop-100:2181,hadoop-101:2181,hadoop-102:2181
- 配置环境变量
vim /etc/profile 输入以下内容
#KAFKA_HOME
export KAFKA_HOME=/opt/module/kafka
export PATH=\$PATH:$KAFKA_HOME/bin
使生效source /etc/profile
- 分发安装包和配置文件并生效
sxync kafka
xsync /etc/profile
source /etc/profile - 修改hadoop-101和hadoop-102的server.properties文件的broker.id
分别修改为1和2 - 启动集群
[hadoop@hadoop-100 bin]$ bin/kafka-server-start.sh config/server.properties &
[hadoop@hadoop-101 bin]$ bin/kafka-server-start.sh config/server.properties &
[hadoop@hadoop-102 bin]$ bin/kafka-server-start.sh config/server.properties & - 停止集群
[hadoop@hadoop-100 kafka]$ bin/kafka-server-stop.sh stop
[hadoop@hadoop-101 kafka]$ bin/kafka-server-stop.sh stop
[hadoop@hadoop-102 kafka]$ bin/kafka-server-stop.sh stop - kafka群控脚本
参考,https://www.jianshu.com/p/44dbc7f0fbb6
Kafka命令行操作
- 查看当前服务器中的所有topic
[hadoop@hadoop-100 ~]$ kafka-topics.sh --zookeeper hadoop-101:2181 --list - 创建topic
[hadoop@hadoop-100 ~]$ kafka-topics.sh --zookeeper hadoop-101:2181 --create --replication-factor 3 --partitions 1 --topic first
--topic 定义topic名
--replication-factor 定义副本数
--partitions 定义分区数
[hadoop@hadoop-100 logs] kafka-topics.sh --zookeeper hadoop-102:2181 --create --replication-factor 4 --partitions 4 --topic first3
当前可用kafka的broker为3,当想创建replication-factor为4时,报错brokers不够时报错.png
- 删除topic
[hadoop@hadoop-100 logs]$ kafka-topics.sh --zookeeper hadoop-101 --delete --topic first1
需要server.properties中设置delete.topic.enable=true否则只是标记删除或者直接重启
删除不存在的topic报错删除不存在的topic报错.png
- 发送消息
[hadoop@hadoop-100 logs]$ kafka-console-producer.sh --broker-list hadoop-101:9092 --topic first
hello
my
name
is
zhao
jun
- 消费消息
[hadoop@hadoop-102 logs]$ kafka-console-consumer.sh --zookeeper hadoop-101:2181 --topic first --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
hello
my
name
is
zhao
jun - 查看某个Topic的详情
[hadoop@hadoop-101 zookeeper-3.4.10]$ kafka-topics.sh --zookeeper hadoop-101 --describe --topic first