Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的框架服务
Flume 基于流式架构,灵活简单,能够实时读取服务器本地磁盘的数据,将数据写入到 HDFS


Source、Channel、Sinkavro、 thrift、 exec、 jms、 spooling directory、 netcat、 taildir、sequence generator、syslog、http、legacyhdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义

将 apache-flume-1.9.0-bin.tar.gz 上传到 linux 的 /opt/software 目录下
解压 apache-flume-1.9.0-bin.tar.gz 到 /opt/module/ 目录下
tar -zxvf apache-flume-1.9.0-bin.tar.gz -C /opt/module/
将 lib 文件夹下的 guava-11.0.2.jar 删除以兼容 Hadoop 3.1.3
rm -rf /opt/module/flume/lib/guava-11.0.2.jar
注意:系统中必须配置 Java 和 Hadoop 的环境变量
需求:使用 Flume 监听一个端口,收集该端口数据,并打印到控制台
思路:
实现:
前期准备
# 安装 netcat 工具
sudo yum install -y nc
# 判断 44444 端口是否被占用
sudo netstat -nlp | grep 44444
Flume 配置
# 在 flume 安装目录下创建 job 文件夹
cd /opt/module/flume
mkdir job
# 在 job 文件夹下创建 Flume Agent 配置文件 flume-netcat-logger.conf 并添加内容
vim job/flume-netcat-logger.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# Describe the sink
a1.sinks.k1.type = logger
# 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
开启 flume 监听端口
# 语法一
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
# 语法二
bin/flume-ng agent -c conf/ -n a1 -f job/flume-netcat-logger.conf
-Dflume.root.logger=INFO,console
--conf/-c:表示配置文件存储在 conf/ 目录--name/-n:表示给 agent 起名为 a1--conf-file/-f:flume 本次启动读取的配置文件是在 job 文件夹下的 flume-telnet.conf 文件-Dflume.root.logger=INFO,console:-D 表示 flume 运行时动态修改 flume.root.logger 参数属性值,并将控制台日志打印级别设置为 INFO 级别。日志级别包括:log、info、warn、error使用 netcat 工具向本机的 44444 端口发送内容
nc localhost 44444
hello
在 Flume 监听页面观察接收数据情况
需求:实时监控 Hive 日志,并上传到 HDFS 中
思路:
实现:
确保系统中配置 Java 和 Hadoop 环境变量
Flume 配置
# 在 job 目录下创建 file-flume-hdfs.conf 文件并添加内容
cd /opt/module/flume
vim job/file-flume-hdfs.conf
# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102:9820/flume/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 60
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k2.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2
对于所有与时间相关的转义序列, Event Header 中必须存在以 “timestamp” 的key (除非 hdfs.useLocalTimeStamp 设置为 true,此方法会使用 TimestampInterceptor 自动添加 timestamp)
运行 Flume
bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
开启 Hadoop 和 Hive 并操作 Hive 产生日志
sbin/start-dfs.sh
sbin/start-yarn.sh
bin/hive
在 HDFS 上查看文件
总结:Exec source 适用于监控一个实时追加的文件,但不能实现断点续传
需求:使用 Flume 监听整个目录的文件,并上传至 HDFS
思路:
.completed,未上传的为 .tmp.completed 的文件传输到 HDFS实现:
Flume 配置
# 在 job 目录下创建配置文件 dir-flume-hdfs.conf 并添加内容
cd /opt/module/flume
vim job/dir-flume-hdfs.conf
# Name the components on this agent
a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /opt/module/flume/upload
a3.sources.r3.fileSuffix = .COMPLETED
a3.sources.r3.fileHeader = true
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:9820/flume/upload/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3
启动 Flume
bin/flume-ng agent -n a3 -c conf/ -f job/dir-flume-hdfs.conf
在被监控的目录下创建新文件
touch hello.txt
查看 HDFS 上的数据
总结:Spooldir Source 适合用于同步新文件,但不适合对实时追加日志的文件进行监听并同步
需求:使用 Flume 监听整个目录的实时追加文件,并上传至 HDFS
实现:
Flume 配置
# 在 job 目录下创建 taildir-flume-hdfs.conf 配置文件并添加内容
cd /opt/module/flume
vim job/taildir-flume-hdfs.conf
a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = TAILDIR
a3.sources.r3.positionFile = /opt/module/flume/tail_dir.json
a3.sources.r3.filegroups = f1 f2
a3.sources.r3.filegroups.f1 = /opt/module/flume/files/.*file.*
a3.sources.r3.filegroups.f2 = /opt/module/flume/files2/.*log.*
# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:9820/flume/upload2/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3
启动 Flume
bin/flume-ng agent -n a3 -c conf/ -f job/taildir-flume-hdfs.conf
向被监控的目录中添加文件并追加内容
echo hello >> file1.txt
echo flume >> file2.txt
查看 HDFS 上的数据
说明:
Taildir Source 维护了一个 json 格式的 position File,其会定期的往 position File 中更新每个文件读取到的最新的位置,因此能够实现断点续传
Position File 的格式:
{"inode":2496272,"pos":12,"file":"/opt/module/flume/files/file1.txt"}
{"inode":2496275,"pos":12,"file":"/opt/module/flume/files/file2.txt"}
Linux 中储存文件元数据的区域就叫做 inode,每个 inode 都有一个号码,操作系统用 inode 号码来识别不同的文件,Unix/Linux 系统内部不使用文件名,而使用 inode 号码来识别文件
问题:
总结:Taildir Source 适合用于监听多个实时追加的文件,并且能够实现断点续传