flume和kafka整合——采集实时日志落地到hdfs
一、采用架构
flume 采用架构
exec-source + memory-channel + kafka-sink
kafka-source + memory-channel + hdfs-sink
模拟需求:
使用flume实时监听日志文件,并将采集数据传输到kafka,再从kafka采集数据到flume,最后落地到HDFS。
二、 前期准备
2.1 虚拟机配置
操作系统 | 主机名 | IP | flume | kafka |
---|---|---|---|---|
Ubuntu | slave1 | 192.168.0.104 | flume-kafka | 集群部署 |
Ubuntu | slave2 | 192.168.0.105 | 无 | 集群部署 |
Ubuntu | slave3 | 192.168.0.106 | kafka-flume | 集群部署 |
slave1部署flume实时监听日志文件
slave3部署flume实时消费kafka数据
kafka采用3台虚拟机集群部署方式
2.2 启动hadoop集群
因为需要将数据存储到HDFS,需要hadoop集群。这里使用Hadoop-2.7.6版本。不会集群的可以参考这篇:Hadoop-2.7.6 集群部署搭建
2.3 启动zookeeper集群,kafka集群
kafka单节点启动也可以。可以参考这篇:Kafka入门集群部署
三、编写配置文件
3.1 slave1创建flume-kafka.conf
cd /opt/apps/apache-flume-1.9.0-bin/job
vim flume-kafka.conf
在flume-kafka.conf输入
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the sources
# exec即execute执行命令
a1.sources.r1.type = exec
# 要执行的命令
a1.sources.r1.command = tail -F /data/sdk.log
# 执行shell脚本的绝对路径
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = sdk
a1.sinks.k1.kafka.bootstrap.servers = slave1:9092,slave2:9092,slave3:9092
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
这里监控的是/data/sdk.log,并将传输kafka的sdk这个topic
3.2 slave3 创建kafka-flume.conf
cd /opt/apps/apache-flume-1.9.0-bin/job
vim kafka-flume.conf
在kafka-flume.conf输入
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = org.apache.flume.source.kafka.KafkaSource
a1.sources.r1.kafka.bootstrap.servers = slave1:9092,slave2:9092,slave3:9092
a1.sources.r1.kafka.topics = sdk
a1.sources.kafkaSource.kafka.consumer.timeout.ms = 100
a1.sources.r1.batchSize = 20
# Describe the sink
a1.sinks.k1.type = hdfs
# 上传到hdfs的路径
a1.sinks.k1.hdfs.path = hdfs://slave1:9000/sdk/%Y%m%d%H
# 文件前缀
a1.sinks.k1.hdfs.filePrefix = logs-
# 是否按时间新建文件夹
a1.sinks.k1.hdfs.round = true
# 定义多久新建文件夹
a1.sinks.k1.hdfs.roundValue = 1
# 重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
# 是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
# flush到hdfs需要积攒event的数量
a1.sinks.k1.hdfs.batchSize = 50
# 设置文件类型
a1.sinks.k1.hdfs.fileType = DataStream
# 多久生成一个新的文件(单位:秒)
a1.sinks.k1.hdfs.rollInterval = 60
# 接收多少数据生成一个新文件(单位:字节)
a1.sinks.k1.hdfs.rollSize = 134217700
# 文件的滚动与event数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 最小冗余数
a1.sinks.k1.hdfs.minBlockReplicas = 1
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
3.3 创建kafka的topic
cd /opt/apps/kafka_2.12-2.3.0
bin/kafka-topics.sh --zookeeper slave1:2181 --create --replication-factor 2 --partitions 1 --topic sdk
3.4 启动flume配置测试
进入到slave1的flume根目录执行
cd /opt/apps/apache-flume-1.9.0-bin
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-kafka.conf -Dflume.root.logger=INFO,console
进入到slave3的flume根目录执行
cd /opt/apps/apache-flume-1.9.0-bin
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/kafka-flume.conf -Dflume.root.logger=INFO,console
效果:
往实时监控sdk.log写入数据,可以看到数据传输到HDFS上