一、 Flume概述
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。
二、 Flume基础架构
Flume数据传输的基本单元,以Event的形式将数据从源头送至目的地。Event由Header和Body两部分组成,Header用来存放该event的一些属性,为K-V结构,Body用来存放该条数据,形式为字节数组。
Source:采集源,负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据。(注:Flume只能采集日志,主要以.txt文本格式为主,不能采集png、jpg、视频等)
Channel:是位于Source和Sink之间的缓冲区(agent内部的数据传输通道),用于从source将数据传递到sink;
Sink:下沉地,不断地轮询Channel中的事件且批量地溢出它们,并将这些事件批量的写入存储或索引系统。(当采用Avro Sink时可以将事件发送到另一个Flume Agent中。
注:具体的Source、Channel、Sink的选用请参照官方文档:Flume 1.10.1 User Guide — Apache Flume
三、Flume安装及部署
1、安装地址
Flume官网地址:http://flume.apache.org/
2、安装及部署
(1)将apache-flume-1.9.0-bin.tar.gz上传到linux的/opt/software目录下
(2)解压apache-flume-1.9.0-bin.tar.gz到/opt/module/目录下
[cys@hadoop01 software]$ tar -zxvf /opt/software/apache-flume-1.9.0-bin.tar.gz -C /opt/module/
(3)修改apache-flume-1.9.0-bin的名称为flume
[cys@hadoop01 module]$ mv /opt/module/apache-flume-1.9.0-bin /opt/module/flume
(4)将lib文件夹下的guava-11.0.2.jar删除以兼容Hadoop 3.1.3
[cys@hadoop01 lib]$ rm /opt/module/flume/lib/guava-11.0.2.jar
(5)修改conf下的log4j.properties确定日志打印的位置
#console表示同时将日志输出到控制台
flume.root.logger=INFO,LOGFILE,console
#固定日志输出的位置
flume.log.dir=/opt/module/flume/logs
#日志文件的名称
flume.log.file=flume.log
四、Flume入门案例(监控端口数据官方案例)
1、案例需求:
使用Flume监听一个端口,收集该端口数据,并打印到控制台。
2、实现步骤:
(1)安装netcat工具
[cys@hadoop01 software]$ sudo yum install -y nc
(2)判断44444端口是否被占用
[cys@hadoop01 flume]$ sudo netstat -nlp | grep 44444
(3)在conf文件夹下创建Flume Agent配置文件nc-flume-log.conf。
[cys@hadoop01 conf]$ vim nc-flume-log.conf
(4)在nc-flume-log.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
(5)先开启flume监听端口
#第一种写法:
[cys@hadoop01 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file conf/nc-flume-log.conf -Dflume.root.logger=INFO,console
#第二种写法:
[cys@hadoop01 flume]$ bin/flume-ng agent -c conf/ -n a1 -f conf/nc-flume-log.conf -Dflume.root.logger=INFO,console
参数说明:
--conf/-c:表示配置文件存储在conf/目录
--name/-n:表示给agent起名为a1
--conf-file/-f:flume本次启动读取的配置文件是在conf文件夹下的nc-flume-log.conf文件。
-Dflume.root.logger=INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。日志参数已经在配置文件中修改了,不再需要重复输入。
(6)使用netcat工具向本机的44444端口发送内容
[cys@hadoop01 ~]$ nc localhost 44444
hello
fafa
10
(7)在Flume监听页面观察接收数据情况
2021-07-15 13:51:00,236 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)] Event: { headers:{} body: hello fafa 10 }
(8)event打印的源码介绍
#LoggerSink的process方法:
if (event != null) {
if (logger.isInfoEnabled()) {
logger.info("Event: " + EventHelper.dumpEvent(event, maxBytesToLog));
}
}
dumpEvent方法返回值:buffer是固定长度的字符串,前端是16进制表示的字符的阿斯卡码值。
return "{ headers:" + event.getHeaders() + " body:" + buffer + " }";