kafka安装部署及命令行使用
1、集群规划
hadoop102 | hadoop103 | hadoop104 |
---|---|---|
zk | zk | zk |
kafka | kafka | kafka |
2、jar包下载
http://kafka.apache.org/downloads.html
3、集群部署
3.1 解压安装包
[tate@hadoop102 software]$ tar -zxvf kafka_2.11-0.11.0.0.tgz -C
/opt/module/
3.2 修改解压后的文件名称
[tate@hadoop102 module]$ mv kafka_2.11-0.11.0.0/ kafka
3.3 在/opt/module/kafka目录下创建logs文件夹
[tate@hadoop102 kafka]$ mkdir /opt/module/kafka/logs
3.4 修改配置文件
[tate@hadoop102 kafka]$ cd config/
[tate@hadoop102 config]$ vim 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=hadoop102:2181,hadoop103:2181,hadoop104:2181
3.5 配置环境变量
[tate@hadoop102 module]$ sudo vim /etc/profile
#KAFKA_HOME
export KAFKA_HOME=/opt/module/kafka
export PATH=$PATH:$KAFKA_HOME/bin
[tate@hadoop102 module]$ source /etc/profile
3.6 分发安装包
[tate@hadoop102 module]$ scp -r kafka/ hadoop103:`pwd`
注意:分发之后记得配置其他机器的环境变量
3.7 修改broker.id
分别在hadoop103和hadoop104上修改配置文件/opt/module/kafka/config/server.properties中的broker.id=1、broker.id=2
注意:broker.id不得重复
3.8 启动集群
依次在hadoop102、hadoop103、hadoop104节点上启动kafka
[tate@hadoop102 kafka]$ bin/kafka-server-start.sh -daemon
config/server.properties
[tate@hadoop103 kafka]$ bin/kafka-server-start.sh-daemon
config/server.properties
[tate@hadoop104 kafka]$ bin/kafka-server-start.sh-daemon
config/server.properties
3.9 关闭集群
[tate@hadoop102 kafka]$ bin/kafka-server-stop.sh stop
[tate@hadoop103 kafka]$ bin/kafka-server-stop.sh stop
[tate@hadoop104 kafka]$ bin/kafka-server-stop.sh stop
3.10 kafka群起脚本
#!/bin/bash
case $1 in
"start"){
for i in hadoop102 hadoop103 hadoop104
do
echo " --------启动 $i Kafka-------"
ssh $i "/opt/module/kafka/bin/kafka-server-start.sh -daemon /opt/module/kafka/config/server.properties"
done
};;
"stop"){
for i in hadoop102 hadoop103 hadoop104
do
echo " --------停止 $i Kafka-------"
ssh $i "/opt/module/kafka/bin/kafka-server-stop.sh stop"
done
};;
esac
4 Kafka命令行操作
4.0 帮助命令
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --help
4.1 查看当前服务器中的所有topic
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --list
或
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list
多节点使用: hadoop102:9092,hadoop103:9092,hadoop104:9092
或:hadoop102:2181,hadoop103:2181,hadoop104:2181
注意:zookeeper只写一个节点,当节点宕机,则不能提供服务;写多个节点,一个节点宕机,其他节点提供服务。后面为了方便只写单节点。
4.2 创建topic
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --create --replication-factor 3 --partitions 1 --topic first
或
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --create --replication-factor 3 --partitions 1 --topic first
选项说明:
–topic 定义topic名
–replication-factor 定义副本数
–partitions 定义分区数
4.3 查看某个Topic的详情
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic first
或
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --describe --topic first
查看
[tate@hadoop102 bin]$ ./kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic first
Topic: first PartitionCount: 1 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: first Partition: 0 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
从上面的结果中,可以看出,topic的分区数量,以及每个分区的副本数量、每个副本所在的broker节点和每个分区的leader副本所在的broker节点、每个分区的ISR副本列表。
ISR:in sync replicas 同步副本
OSR: out of sync replicas 失去同步的副本(数据与leader之间的差距超过了配置的阙值)
4.4 删除topic
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --delete --topic first
或
[tate@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --delete --topic first
需要server.properties中设置delete.topic.enable=true否则只是标记删除。
4.5 修改分区数
[tate@hadoop102 kafka]$bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --alter --topic first --partitions 6
或
[tate@hadoop102 kafka]$bin/kafka-topics.sh --zookeeper hadoop102:2181 --alter --topic first --partitions 6
查看
[tate@hadoop102 bin]$ ./kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic first
Topic: first PartitionCount: 6 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: first Partition: 0 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
Topic: first Partition: 1 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
Topic: first Partition: 2 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
Topic: first Partition: 3 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
Topic: first Partition: 4 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2
Topic: first Partition: 5 Leader: 2 Replicas: 2,1,0 Isr: 2,1,0
kafka中分区数只能增加不能减少,原因是:减少分区,代价太大(数据的转移,日志段拼接合并)
4.6 发送消息
[tate@hadoop102 kafka]$ bin/kafka-console-producer.sh --broker-list hadoop102:9092 --topic first
>hello world
>spark flink
4.7 消费消息
[tate@hadoop102kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first
或
[tate@hadoop102kafka]$ bin/kafka-console-consumer.sh --zookeeper hadoop102:2181--topic first
或
[tate@hadoop102kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --from-beginning --topic first
–from-beginning:会把主题中以往所有的数据都读取出来。