Flume入门笔记

来源:互联网 发布:java带参数的构造函数 编辑:程序博客网 时间:2024/06/04 21:59

在具体介绍本文内容之前,先给大家看一下Hadoop业务的整体开发流程:
这里写图片描述
从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步,从而引出我们本文的主角—Flume。本文将围绕Flume的架构、Flume的应用(日志采集)进行详细的介绍。
(一)Flume架构介绍
1、Flume的概念
这里写图片描述
flume是分布式的日志收集系统,它将各个服务器中的数据收集起来并送到指定的地方去,比如说送到图中的HDFS,简单来说flume就是收集日志的。
2、Event的概念
在这里有必要先介绍一下flume中event的相关概念:flume的核心是把数据从数据源(source)收集过来,在将收集到的数据送到指定的目的地(sink)。为了保证输送的过程一定成功,在送到目的地(sink)之前,会先缓存数据(channel),待数据真正到达目的地(sink)后,flume在删除自己缓存的数据。
在整个数据的传输的过程中,流动的是event,即事务保证是在event级别进行的。那么什么是event呢?—–event将传输的数据进行封装,是flume传输数据的基本单位,如果是文本文件,通常是一行记录,event也是事务的基本单位。event从source,流向channel,再到sink,本身为一个字节数组,并可携带headers(头信息)信息。event代表着一个数据的最小完整单元,从外部数据源来,向外部的目的地去。
为了方便大家理解,给出一张event的数据流向图:
这里写图片描述
一个完整的event包括:event headers、event body、event信息(即文本文件中的单行记录),如下所以:
这里写图片描述
其中event信息就是flume收集到的日记记录。
3、flume架构介绍
flume之所以这么神奇,是源于它自身的一个设计,这个设计就是agent,agent本身是一个Java进程,运行在日志收集节点—所谓日志收集节点就是服务器节点。
agent里面包含3个核心的组件:source—->channel—–>sink,类似生产者、仓库、消费者的架构。
source:source组件是专门用来收集数据的,可以处理各种类型、各种格式的日志数据,包括Avro、Thrift、Exec、JMS、Spooling Directory、Taildir、Twitter、Kafka、NetCat、Sequence、Syslog、Multiport Syslog TCP、Syslog UDP、HTTP、Stress Legacy、Avro Legacy、Thrift Legacy、Custom、Scribe。
channel:source组件把数据收集来以后,临时存放在channel中,即channel组件在agent中是专门用来存放临时数据的——对采集到的数据进行简单的缓存,可以存放在Memory、JDBC、Kafka、File、Spillable Memory、Pseudo Transaction、Custom
sink:sink组件是用于把数据发送到目的地的组件,目的地包括HDFS、Hive、Logger、Avro、Thrift、IRC、File Roll、Null、HBase、AsyncHBase、MorphlineSolr、ElasticSearch、Kite Dataset、Kafka、Custom
4、flume的运行机制
flume的核心就是一个agent,这个agent对外有两个进行交互的地方,一个是接受数据的输入——source,一个是数据的输出sink,sink负责将数据发送到外部指定的目的地。source接收到数据之后,将数据发送给channel,chanel作为一个数据缓冲区会临时存放这些数据,随后sink会将channel中的数据发送到指定的地方—-例如HDFS等,注意:只有在sink将channel中的数据成功发送出去之后,channel才会将临时数据进行删除,这种机制保证了数据传输的可靠性与安全性。
5、flume的广义用法
flume之所以这么神奇—-其原因也在于flume可以支持多级flume的agent,即flume可以前后相继,例如sink可以将数据写到下一个agent的source中,这样的话就可以连成串了,可以整体处理了。flume还支持扇入(fan-in)、扇出(fan-out)。所谓扇入就是source可以接受多个输入,所谓扇出就是sink可以将数据输出多个目的地destination中。
这里写图片描述
这里写图片描述

(二)flume应用—日志采集
对于flume的原理其实很容易理解,我们更应该掌握flume的具体使用方法,flume提供了大量内置的Source、Channel和Sink类型。而且不同类型的Source、Channel和Sink可以自由组合—–组合方式基于用户设置的配置文件,非常灵活。比如:Channel可以把事件暂存在内存里,也可以持久化到本地硬盘上。Sink可以把日志写入HDFS, HBase,甚至是另外一个Source等等。下面我将用具体的案例详述flume的具体用法。
其实flume的用法很简单—-书写一个配置文件,在配置文件当中描述source、channel与sink的具体实现,而后运行一个agent实例,在运行agent实例的过程中会读取配置文件的内容,这样flume就会采集到数据。
配置文件的编写原则:
1>从整体上描述代理agent中sources、sinks、channels所涉及到的组件

# example.conf: A single-node Flume configuration# Name the components on this agenta1.sources = r1a1.sinks = k1a1.channels = c1

2>详细描述agent中每一个source、sink与channel的具体实现:即在描述source的时候,需要
指定source到底是什么类型的,即这个source是接受文件的、还是接受http的、还是接受thrift
的;对于sink也是同理,需要指定结果是输出到HDFS中,还是Hbase中啊等等;对于channel
需要指定是内存啊,还是数据库啊,还是文件啊等等。

# Describe/configure the sourcea1.sources.r1.type = netcata1.sources.r1.bind = localhosta1.sources.r1.port = 44444# Describe the sinka1.sinks.k1.type = logger# Use a channel which buffers events in memorya1.channels.c1.type = memorya1.channels.c1.capacity = 1000a1.channels.c1.transactionCapacity = 1003>通过channel将source与sink连接起来

Bind the source and sink to the channel

a1.sources.r1.channels = c1a1.sinks.k1.channel = c1

4>启动agent的shell操作:

$ bin/flume-ng agent --conf conf --conf-file example.conf --name a1 -Dflume.root.logger=INFO,console

参数说明: -n 指定agent名称(与配置文件中代理的名字相同)
-c 指定flume中配置文件的目录
-f 指定配置文件
-Dflume.root.logger=DEBUG,console 设置日志等级

具体案例:
案例1: exec Source:监听一个指定的文件,即只要应用程序向这个文件里面写数据,这个source组件就可以获取到信息。 其中 Sink:logger Channel:memory

flume官网中exec Source描述:
Property Name Default Description
channels –
type – The component type name, needs to be exec
command – 需要执行的命令

a) 编写配置文件:

a.sinks=k1a.sources=s1 s2a.channels=r1a.sources.s1.type=execa.sources.s1.command=tail -F /root/a.loga.sources.s2.type=execa.sources.s2.command=tail -F /root/b.loga.sinks.k1.type=loggera.channels.r1.type=memorya.channels.r1.capacity=100a.channels.r1.transactionCapacity=100a.sources.s2.channels=r1a.sources.s1.channels=r1a.sinks.k1.channel=r1

b) 启动flume agent a1 服务端

./flume-ng agent --conf conf --conf-file ../conf/flume_conf.conf --name a -Dflume.root.logger=INFO,console

c) 使用telnet发送数据

[root@SZB-L0032016 ~]# echo "the big data">>./a.log[root@SZB-L0032016 ~]# echo "the big data">>./a.log

d) 在控制台上查看flume收集到的日志数据:
这里写图片描述

**案例2:**exec Source:监听一个指定的文件,即只要应用程序向这个文件里面写数据,这个source组件就可以获取到信息。 其中 Sink:hdfs Channel:file (相比于案例1的两个变化)
flume官网中HDFS Sink的描述:
Name Default Description
channel –
type – The component type name, needs to be hdfs
hdfs.path – HDFS的路径
hdfs.filePrefix 生成文件的路径
hdfs.fileSuffix – 文件的后缀
hdfs.inUsePrefix 正在使用的时候临时文件的路径
hdfs.inUseSuffix 正在使用的时候临时文件的后缀
hdfs.rollInterval 30 当前写入文件的滚动间隔,每个30S生成一个文件,0表示不基于时间间隔滚动
hdfs.rollSize 1024 基于文件大小滚动,单位是字节 0表示不基于大小滚动
hdfs.rollCount 10 写入的事件数触发文件的滚动,0表示不滚动
hdfs.idleTimeout 0 Timeout after which inactive files get closed (0 = disable automatic closing of idle files)
hdfs.batchSize 100 事件写入HDFS的数量
hdfs.codeC – 压缩 格式: gzip, bzip2, lzo, lzop, snappy
hdfs.fileType SequenceFile 文件格式
hdfs.maxOpenFiles 5000 允许文件打开的次数
a) 编写配置文件:

a.sinks=k1a.sources=s1 s2a.channels=r1a.sources.s1.type=execa.sources.s1.command=tail -F /root/a.loga.sources.s2.type=execa.sources.s2.command=tail -F /root/b.loga.sinks.k1.type=hdfsa.sinks.k1.hdfs.path = hdfs://10.20.24.231:8020/flumea.sinks.k1.hdfs.writeFormat = Texta.sinks.k1.hdfs.fileType = DataStreama.sinks.k1.hdfs.rollInterval = 10a.sinks.k1.hdfs.rollSize = 0a.sinks.k1.hdfs.rollCount = 0a.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%Sa.sinks.k1.hdfs.useLocalTimeStamp = truea.channels.r1.type=filea.channels.r1.checkpointDir=/root/flume/checkpointa.channels.r1.dataDirs=/root/flume/dataa.sources.s2.channels=r1a.sources.s1.channels=r1a.sinks.k1.channel=r1

b) 启动flume agent a 服务端

./flume-ng  agent -n a -c ../conf  -f ../conf/flume_hdfs.conf   -Dflume.root.logger=DEBUG,console

c) 向文件追加内容

[root@SZB-L0032016 ~]# echo "test big data">./a.log

d) 在HDFS中查看flume收集到的日志数据:
这里写图片描述

**案例3:**Spooling Directory Source:监听一个指定的目录,即只要应用程序向这个指定的目录中添加新的文件,source组件就可以获取到该信息,并解析该文件的内容,然后写入到channle。写入完成后,标记该文件已完成或者删除该文件。其中 Sink:logger Channel:memory
flume官网中Spooling Directory Source描述:
Property Name Default Description
channels –
type – The component type name, needs to be spooldir.
spoolDir – Spooling Directory Source监听的目录
fileSuffix .COMPLETED 文件内容写入到channel之后,标记该文件
deletePolicy never 文件内容写入到channel之后的删除策略: never or immediate
fileHeader false Whether to add a header storing the absolute path filename.
ignorePattern ^$ Regular expression specifying which files to ignore (skip)
interceptors – 指定传输中event的head(头信息),常用timestamp

Spooling Directory Source的两个注意事项:
①If a file is written to after being placed into the spooling directory, Flume will print an error to its log file and stop processing.
即:拷贝到spool目录下的文件不可以再打开编辑
②If a file name is reused at a later time, Flume will print an error to its log file and stop processing.
即:不能将具有相同文件名字的文件拷贝到这个目录下
a) 编写配置文件:

a.sinks=k1a.sources=s1 s2a.channels=r1a.sources.s1.type = spooldira.sources.s1.spoolDir = /root/flume/spoola.sources.s1.fileHeader = truea.sources.s1.interceptors = i1a.sources.s1.interceptors.i1.type = timestampa.sinks.k1.type=loggera.channels.r1.type=memorya.channels.r1.capacity=100a.channels.r1.transactionCapacity=100a.sources.s2.channels=r1a.sources.s1.channels=r1a.sinks.k1.channel=r1

b) 启动flume agent a1 服务端

./flume-ng  agent -n a  -c ../conf  -f ../conf/flume_spool.conf   -Dflume.root.logger=DEBUG,console

b) 使用cp命令向Spooling Directory 中发送数据

[root@SZB-L0032016 flume]# cp ~/a.log ./spool/

c) 在控制台上查看flume收集到的日志数据
这里写图片描述
从控制台显示的结果可以看出event的头信息中包含了时间戳信息。
同时我们查看一下Spooling Directory中的datafile信息—-文件内容写入到channel之后,该文件被标记了:

[root@SZB-L0032016 spool]# lltotal 4-rw-r--r-- 1 root root 14 Dec 13 09:38 a.log.COMPLETED

参考文章http://blog.csdn.net/a2011480169/article/details/51544664

0 0
原创粉丝点击