Flume
- 服务:启动后监听某个端口,等待客户端连接,并处理客户端请求。客户端断开连接后,服务程序依然运行。
- 工具:需要使用工具的功能时,启动程序。使用完毕后,工具程序可以直接关闭。
- 框架:一个半成品软件,需要开发人员根据业务逻辑填写核心代码,组成完成的程序,提供工具或者服务的功能。
是什么
- 在数据处理场景中,数据的产生往往分散在时间空间不同的各个服务器上,需要将各个服务器的数据自动化采集到同一个HDFS集群,就需要使用自动化采集工具
- flume是一个hadoop生态中的专用用来进行海量日志,收集、聚合、移动的工具
核心概念
- agent 代理
flume启动的一个Jvm实例,称之为一个Agent - source 源
是agent中的一个组件,负责读取原始数据(文件,其他协议的网络连接)
将原始数据封装为event对象
将event对象发送到channel中 - channel 通道
通过内存或者磁盘文件构建一个用来缓存event的队列 - sink 出口
读取channel中缓存的event对象
拆解出event中的数据并发送到下游组件(HDFS) - event 事件
flume中自定义的对象用于封装数据(比如一行日志文件封装为一个event)
flume安装
-
上传解压
[root@zhaohui01 ~]# cd /opt [root@zhaohui01 opt]# tar zxvf flume-1.9/
-
修改配置文件
- flume-env.sh
JAVA_HOME=/opt/jdk-1.8
3.配置环境变量
echo 'export FLUME_HOME=/opt/flume-1.9' >> /etc/profile
echo 'export PATH=$FLUME_HOME/bin:$PATH' >> /etc/profile
# 更新资源
source /etc/profile
4.在flume安装目录下创建文件夹agents,创建配置文件 agent_test_1.conf
[root@zhaohui01 opt]# cd /opt/flume-1.9/
[root@zhaohui01 flume-1.9]# mkdir agents
[root@zhaohui01 flume-1.9]# cd agents/
[root@zhaohui01 agents]# vim agent_test_1.conf
# example.conf: A single-node Flume configuration
# 设置Agent中组件的名称
# a1是当前agent的名称
# Name the components on this agent
# 设置a1中的source有一个叫做r1
a1.sources = r1
# 设置a1中的sink有一个叫做k1
a1.sinks = k1
# 设置a1中的channel有一个叫做c1
a1.channels = c1
# Describe/configure the source
# 配置source
# a1中的r1的类型是netcat
a1.sources.r1.type = netcat
# 监听的主机
a1.sources.r1.bind = zhaohui01
# 绑定的端口号
a1.sources.r1.port = 44444
# Describe the sink
# logger将接收到数据打印到控制台
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
# 设置channel使用内存作为event的缓存
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操作
- Flume通过解耦的设计将数据收集端和数据存储端以及中间的缓存拆分为了三个模块
- 使用flume进行数据采集时,只需要根据需要选择对应功能source、channel、sink,编写agent的配置文件,将三个模块组合成一个agent,启动程序即可
1)Source
常用source
对比 | exec | spooldir | taildir |
---|---|---|---|
功能 | 通过执行一个shell命令读取数据, 通常配合tail -F file 实现单文件的实时读取 | 监控整个目录,读取目录中所有的文件,不支持递归 | 可以监控多个文件或者文件夹的变化 |
优势 | 性能最优 | 可以监控整个文件夹 | 可以监控文件或者文件夹,并支持记录读取进度 |
劣势 | 只能监控单文件,不支持断点续传 | 不支持断点续传,也不支持读取文件的改动 | - |
1、打印到控制台(sink类型为logger)
开启程序
参数说明:
–conf /opt/flume-1.9/conf/:表示配置文件存储在 conf/目录
–name a1
:表示给 agent 起名为 a1
–conf-file /opt/flume-1.9/agents/agent_test_1.conf :flume 本次启动读取的配置文件是在 job 文件夹下
的 flume-telnet.conf 文件。
-Dflume.root.logger=INFO,console : -D 表 示 flume 运 行 时 动 态 修 改
flume.root.logger 参数属性值,并将控制台日志打印级别设置为 INFO 级别。日志级别包括:log、
info、warn、error。
flume-ng agent --name a1 --conf /opt/flume-1.9/conf/ --conf-file /opt/flume-1.9/agents/agent_test_1.conf -Dflume.root.logger=INFO,console
监听文件
[root@zhaohui01 ~]# tail -F a.txt
还可以配置agent_test_1.conf
# Describe/configure the source
# 配置source
# a1中的r1的类型是netcat
a1.sources.r1.type = exec
# 监听的主机
# 参数-c +0 表示从头检测,不会造成数据丢失
a1.sources.r1.command = tail -F -c +0 /root/a.txt
a1.sinks.k1.type = logger
1、将文件监听结果保存到HDFS(exec)
a1中的r1的类型是exec 检测文件
类型是 spooldir 检测文件夹,文件夹产生新的文件时会自动上传,而且文件不可修改
类型是 taildir 检测多目录多文件,断点续传
# example.conf: A single-node Flume configuration
# 设置Agent中组件的名称
# a1是当前agent的名称
# Name the components on this agent
# 设置a1中的source有一个叫做r1
a1.sources = r1
# 设置a1中的sink有一个叫做k1
a1.sinks = k1
# 设置a1中的channel有一个叫做c1
a1.channels = c1
# Describe/configure the source
# 配置source
# a1中的r1的类型是exec 检测文件
# 类型是 spooldir 检测文件夹,文件夹产生新的文件时会自动上传,而且文件不可修改
# 类型是 taildir 检测多目录多文件,断点续传
a1.sources.r1.type = exec
# 监听的主机
a1.sources.r1.command = tail -F /root/a.txt
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
# logger将接收到数据打印到HDFS
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://zhaohui01:8020/flume/%Y%m%d/%H
# 更改文件类型为数据流
a1.sinks.k1.hdfs.fileType = DataStream
# 输出格式改为文本格式
a1.sinks.k1.hdfs.writeFormat = Text
#上传文件的前缀
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
#积攒多少个 Event 才 flush 到 HDFS 一次
a1.sinks.k1.hdfs.batchSize = 1000
#多久生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 600 #设置每个文件的滚动大小
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
# 设置channel使用内存作为event的缓存
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
在HDFS根目录下创建flume文件夹
[root@zhaohui01 ~]# hdfs dfs -mkdir /flume/
重启程序,测试
缺点:只能读取单文件
2、spoolDir
# spooldir读取同目录中的多个日志文件 将数据发送到logger sink
# 声明组件名称
spooldir2logger.sources = r1
spooldir2logger.sinks = k1
spooldir2logger.channels = c1
# Describe/configure the source
spooldir2logger.sources.r1.type = spooldir
spooldir2logger.sources.r1.spoolDir = /root/log
# Describe the sink
spooldir2logger.sinks.k1.type = logger
# Use a channel which buffers events in memory
spooldir2logger.channels.c1.type = memory
spooldir2logger.channels.c1.capacity = 1000
spooldir2logger.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
spooldir2logger.sources.r1.channels = c1
spooldir2logger.sinks.k1.channel = c1
缺点:读取完监视的文件后会将文件标记为.COMPLETED,所以当Agent程序崩溃后,spooldir没有将文件读完的话,重启agent程序,会将监视的文件从头在读一遍,直到读完后标记为COMPLETED文件,才会读取下一个,读取效率大大降低,而且文件一旦读取完后,文件发生变化也不会在读取
优点:可以读取多文件,保证文件不丢失
3、taildir(重点,常用)
# taildir读取同目录中的多个日志文件 将数据发送到logger sink
# 声明组件名称
taildir2logger.sources = r1
taildir2logger.sinks = k1
taildir2logger.channels = c1
# Describe/configure the source
taildir2logger.sources.r1.type = TAILDIR
taildir2logger.sources.r1.filegroups = g1 g2 g3
taildir2logger.sources.r1.filegroups.g1 = /root/log.txt
taildir2logger.sources.r1.filegroups.g2 = /root/log/.*.txt
taildir2logger.sources.r1.filegroups.g3 = /opt/2.txt
# Describe the sink
taildir2logger.sinks.k1.type = logger
# Use a channel which buffers events in memory
taildir2logger.channels.c1.type = memory
# 设置 memory channel可以保存的最大event数
# 设置依据, 需要估算channel可能缓存的数据量
# 与agent可用Jvm内存
# 可以在flume-ng脚本中配置agent的JVM参数增大内存
# 20000~200000
taildir2logger.channels.c1.capacity = 30000
taildir2logger.channels.c1.transactionCapacity = 3000
# Bind the source and sink to the channel
taildir2logger.sources.r1.channels = c1
taildir2logger.sinks.k1.channel = c1
**优点:**支持监控多目录下的多文件
支持断点续传,因为在监控的文件发生变化时,会在 ~/.flume/taildir_position.json 文件中记录每次读取到的位置信息,当下一次读取时会加载这个文件,接着上次的位置继续读。
例~/.flume/taildir_position.json信息如下
[
{
"inode": 134316113,
"pos": 5,
"file": "/root/log.txt"
},
{
"inode": 134316122,
"pos": 6,
"file": "/root/log/2.txt"
},
{
"inode": 137609466,
"pos": 7,
"file": "/opt/2.txt"
}
]
2)channel
常用channel
对比 | memory | file |
---|---|---|
性能 | 优 | 略弱 |
安全性 | 略弱 | 优 |
3) sink
- logger
通常用于测试,可以方便将数据打印到控制台 - HDFS sink
将数据写入到HDFS集群