第1章 Flume概述
1.1 Flume定义
Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。
1.2 Flume基础架构
Flume组成架构
下面我们来详细介绍一下Flume架构中的组件:
1.2.1 Agent
Agent是一个JVM进程,它以事件的形式将数据从源头送至目的。
Agent主要有3个部分组成,Source、Channel、Sink。
1.2.2 Source
Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avro
、thrift、exec
、jms、spooling directory
、netcat
、sequence generator、syslog、http、legacy。
1.2.3 Sink
Sink不断地轮询Channel中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent。
Sink组件目的地包括hdfs
、logger
、avro
、thrift、ipc、file
、HBase
、solr、自定义。
1.2.4 Channel
Channel是位于Source和Sink之间的缓冲区。因此,Channel允许Source和Sink运作在不同的速率上。Channel是线程安全的,可以同时处理几个Source的写入操作和几个Sink的读取操作。
Flume自带两种Channel:Memory Channel
和File Channel
。
Memory Channel是内存中的队列。Memory Channel在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么Memory Channel就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。
File Channel将所有事件写到磁盘。因此在程序关闭或机器宕机的情况下不会丢失数据。
1.2.5 Event
传输单元,Flume数据传输的基本单元,以Event的形式将数据从源头送至目的地。Event由Header
和Body
两部分组成,Header用来存放该event的一些属性,为K-V结构,Body用来存放该条数据,形式为字节数组。
1.2.6 Interceptors
在Flume中允许使用拦截器对传输中的event进行拦截和处理!拦截器必须实现org.apache.flume.interceptor.Interceptor接口。拦截器可以根据开发者的设定修改甚至删除event!Flume同时支持拦截器链,即由多个拦截器组合而成!通过指定拦截器链中拦截器的顺序,event将按照顺序依次被拦截器进行处理!
1.2.7 Channel Selectors
Channel Selectors用于source组件将event传输给多个channel的场景。常用的有replicating(默认)和multiplexing两种类型。replicating负责将event复制到多个channel,而multiplexing则根据event的属性和配置的参数进行匹配,匹配成功则发送到指定的channel!
1.2.8 Sink Processors
用户可以将多个sink组成一个整体(sink组),Sink Processors可用于提供组内的所有sink的负载平衡功能,或在时间故障的情况下实现从一个sink到另一个sink的故障转移。
第2章 Flume快速入门
2.1 Flume安装部署
2.1.1 安装地址
1) Flume官网地址
http://flume.apache.org/
2)文档查看地址
http://flume.apache.org/FlumeUserGuide.html
3)下载地址
http://archive.apache.org/dist/flume/
2.1.2 安装部署
1)将flume-ng-1.6.0-cdh5.14.0.tar.gz
上传到linux的/opt/目录下
2)解压flume-ng-1.6.0-cdh5.14.0.tar.gz
到/opt/目录下
3)修改flume-ng-1.6.0-cdh5.14.0.tar.gz
的名称为flume
4)将flume/conf下的flume-env.sh.template文件修改为flume-env.sh
,并配置flume-env.sh文件
mv flume-env.sh.template flume-env.sh
vi flume-env.sh
export JAVA_HOME=/opt/jdk1.8.0_221
2.2 Flume入门案例
2.2.1 监控端口数据官方案例
1)案例需求:
使用Flume监听一个端口,收集该端口数据,并打印到控制台。
2)需求分析:
3)实现步骤:
1.安装netcat工具
sudo yum install -y nc
2.判断44444端口是否被占用
sudo netstat -tunlp | grep 44444
3.创建Flume Agent配置文件flume-netcat-logger.conf
在flume目录下创建job文件夹并进入job文件夹。
[root@hadoop100 flume]# mkdir job
[root@hadoop100 flume]# cd job/
在job文件夹下创建Flume Agent配置文件flume-netcat-logger.conf。
vi flume-netcat-logger.conf
在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 = 10000
a1.channels.c1.transactionCapacity = 1000
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
注:配置文件来源于官方手册http://flume.apache.org/FlumeUserGuide.html
4. 先开启flume监听端口
第一种写法:
bin/flume-ng agent -n a1 -c ./conf/ -f ./job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
第二种写法:
bin/flume-ng agent --conf conf/ --name a1 --conf-file 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。
5.使用netcat工具向本机的44444端口发送内容
telnet localhost 44444
6.在Flume监听页面观察接收数据情况
2.2.2 实时监控单个文件
1)案例需求:实时监控Hive日志,并上传到HDFS中
2)需求分析:
3)实现步骤:
1.Flume要想将数据输出到HDFS,必须持有Hadoop相关jar包
将
commons-configuration-1.6.jar、
hadoop-auth-2.7.2.jar、
hadoop-common-2.7.2.jar、
hadoop-hdfs-2.7.2.jar、
commons-io-2.4.jar、
htrace-core-3.1.0-incubating.jar
拷贝到/opt/flume/lib文件夹下。
2.创建flume-file-hdfs.conf文件
创建文件
vi flume-file-hdfs.conf
注:要想读取Linux系统中的文件,就得按照Linux命令的规则执行命令。由于Hive日志在Linux系统中所以读取文件的类型选择:exec即execute执行的意思。表示执行Linux命令来读取文件。
添加如下内容
# 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/hive/logs/hive.log
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/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.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 = 10000
a2.channels.c2.transactionCapacity = 1000
# 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)。
a2.sinks.k2.hdfs.useLocalTimeStamp = true
3.运行Flume
bin/flume-ng agent -n a2 -c ./conf/ -f ./job/flume-file-hdfs.conf -Dflume.root.logger=INFO,console
4.开启Hadoop和Hive并操作Hive产生日志
5.在HDFS上查看文件
2.2.3 监控多个新文件
1)案例需求:使用Flume监听整个目录的文件,并上传至HDFS
2)需求分析:
3)实现步骤:
1.创建配置文件flume-dir-hdfs.conf
创建一个文件
vi flume-dir-hdfs.conf
添加如下内容
a3.sources=r3
a3.sinks=k3
a3.channels=c3
a3.sources.r3.type=spooldir
a3.sources.r3.spoolDir=/opt/flume/upload
a3.sources.r3.fileSuffix=.COMPLETED
#忽略所有以.tmp结尾的文件,不上传
a3.sources.r3.ignorePattern=\\S*\\.tmp
a3.sinks.k3.type=hdfs
a3.sinks.k3.hdfs.path=hdfs://192.168.83.100:9000/flume/upload/%Y-%m-%d
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix=upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round=true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue=1
#重新定义时间单位
a3.sinks.k3.hdfs.rountUnit=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=30
#设置每个文件的滚动大小大概是128M
a3.sinks.k3.rollSize=134217700
#文件的滚动与Event数量无关
a3.sinks.k3.hdfs.rollCount=0
a3.channels.c3.type=memory
a3.channels.c3.capacity=10000
a3.channels.c3.transactionCapacity=1000
a3.sources.r3.channels=c3
a3.sinks.k3.channel=c3
2.启动监控文件夹命令
bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf
说明:在使用Spooling Directory Source时
不要在监控目录中创建并持续修改文件
上传完成的文件会以.COMPLETED结尾
被监控文件夹每500毫秒扫描一次文件变动
3.向upload文件夹中添加文件
在/opt/flume目录下创建upload文件夹
mkdir upload
向upload文件夹中添加文件
touch hello.txt
4.查看HDFS上的数据
5.等待1s,再次查询upload文件夹
[root@hadoop100 upload]# touch hello.txt
[root@hadoop100 upload]# ls
hello.txt.COMPLETED
2.2.4 实时监控多个文件
Exec source适用于监控一个实时追加的文件,但不能保证数据不丢失;Spooldir Source能够保证数据不丢失,且能够实现断点续传,但延迟较高,不能实时监控;而Taildir Source既能够实现断点续传,又可以保证数据不丢失,还能够进行实时监控。
1)案例需求:使用Flume监听整个目录的实时追加文件,并上传至HDFS
2)需求分析:
3)实现步骤:
1.创建配置文件flume-taildir-hdfs.conf
创建一个文件
[root@hadoop100 flume]# vi ./job/flume-taildr-hdfs.conf
添加如下内容
a3.sources=r3
a3.sinks=k3
a3.channels=c3
a3.sources.r3.type=TAILDIR
a3.sources.r3.positionFile=/opt/flume/tail_dir.json
a3.sources.r3.filegroups=f1
a3.sources.r3.filegroups.f1=/opt/flume/files/file.*
a3.sinks.k3.type=hdfs
a3.sinks.k3.hdfs.path=hdfs://192.168.83.100:9000/flume/upload/%Y-%m-%d
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix=upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round=true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue=1
#重新定义时间单位
a3.sinks.k3.hdfs.rountUnit=hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLo