1.Flume简介

Apache flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统,用于有效地收集、聚合和将大量日志数据从许多不同的源移动到一个集中的数据存储(如文本、HDFS、Hbase等)。

其使用不仅仅限于日志数据聚合。因为数据源是可定制的(内置Avro,ThriftSyslog,Netcat),Flume可以用于传输大量事件数据,包括但不限于网络流量数据、社交媒体生成的数据、电子邮件消息和几乎所有可能的数据源。

其使用不仅仅限于日志数据聚合。因为数据源是可定制的(内置Avro,ThriftSyslog,Netcat),Flume可以用于传输大量事件数据,包括但不限于网络流量数据、社交媒体生成的数据、电子邮件消息和几乎所有可能的数据源。

2.Flume的核心

flume的数据流由事件(Event)贯穿始终。事件是Flume的基本数据单位,它携带日志数据(字节数组形式)并且携带有头信息,这些Event由Agent外部的Source生成,当Source捕获事件后会进行特定的格式化,然后Source会把事件推入(单个或多个)Channel中。你可以把Channel看作是一个缓冲区,它将保存事件直到Sink处理完该事件。Sink负责持久化日志或者把事件推向另一个Source。

2.1flume的可靠性

1)flume的可靠性 当节点出现故障时,日志能够被传送到其他节点上而不会丢失(HA)。Flume提供了三种级别的可靠性保障,从强到弱依次分别为:end-to-end(收到数据agent首先将event写到磁盘上,当数据传送成功后,再删除;如果数据发送失败,可以重新发送。),Store on failure(这也是scribe采用的策略,当数据接收方crash时,将数据写到本地,待恢复后,继续发送),Besteffort(数据发送到接收方后,不会进行确认)。

2)flume的可恢复性 还是靠Channel。推荐使用FileChannel,事件持久化在本地文件系统里(性能较差)。

2.2flume的核心概念

  • Client:Client生产数据,运行在一个独立的线程。
  • Event: 一个数据单元,消息头和消息体组成。(Events可以是日志记录、 avro 对象等。)
  • Flow: Event从源点到达目的点的迁移的抽象。
  • Agent: flume的运行单元,里面必须包含一个或者多个source、channel、sink,运行在单个jvm中。
  • Source: 数据收集组件。(source从Client收集数据,传递给Channel)
  • Channel: 中转Event的一个临时存储,保存由Source组件传递过来的Event。(Channel连接 sources 和 sinks ,这个有点像一个队列。)
  • Sink: 数据下沉,将channel中的数据持久化到对应的文件系统中或者流中(从Channel中读取并移除Event, 将Event传递到FlowPipeline中的下一个Agent)。
  • interceptor : 拦截器,作用于source阶段,用于过滤数据。
  • selectorer : 选择器,作用于source阶段,默认是replicating,也就是复用功能。mutiplxing
  • groupsinks : sink组,用于将多个sink选择sink。

3.flume的组件


常见的组件: source常用组件:exec、avro source 、 spooling dirctory 、kafka 、netcat 、http、自定义 channel常用:memory 、 file 、kafka 、 jdbc sinks常用 : logger 、avro 、 hdfs 、hive 、hbase、kafka等


3.1source

Source是数据的收集端,负责将数据捕获后进行特殊的格式化,将数据封装到事件(event) 里,然后将事件推入Channel中。Flume提供了各种source的实现,包括Avro Source、 Exce Source、 SpoolingDirectory Source、 NetCat Source、 Syslog Source、 Syslog TCP Source、Syslog UDP Source、 HTTP Source、 HDFS Source等等。可以让应用程序同已有的Source直接打交道, 如果内置的Source无法满足需要, Flume还支持自定义Source。分成transtion 和 event 打入到channel之中.

source类型:

flume 套接字sink flume 使用_flume 套接字sink

3.2channel

中转Event的一个临时存储,保存由Source组件传递过来的Event。(Channel连接Source和Sink的组件,可以将它看做一个数据的缓冲区(数据队列),它可以将事件暂存到内存中也可以持久化到本地磁盘上, 直到Sink处理完该事件。介绍两个较为常用的Channel, MemoryChannel和FileChannel(MemoryChannel可以实现高速的吞吐, 但是无法保证数据完整性;MemoryRecoverChannel在官方文档的建议上已经建义使用FileChannel来替换。FileChannel保证数据的完整性与一致性。在具体配置不现的FileChannel时,建议FileChannel设置的目录和程序日志文件保存的目录设成不同的磁盘,以便提高效率。)。

flume 套接字sink flume 使用_应用案例_02

1,useDualCheckpoints(是否需要备份检查点)2,compressBackupCheckpoint(是否压缩备份节点)3,checkpointDir(检查点目录,默认在${user.home}目录下)4,dataDirs(数据节点目录)5,capacity(获取配置的容量)6,keepAlive(超时时间,就是如果channel中没有数据最长等待时间)7,transactionCapacity(事务的最大容量)注意:capacity的值一定要大于transactionCapacity,不然会报错。

3.3sink

Sink: 从Channel中读取并移除Event,将Event传递到FlowPipeline中的下一个Agent(如果有的话)(Sink从Channel收集数据,运行在一个独立线程。),Sink从Channel中取出事件,然后将数据发到别处,可以向文件系统、数据库、 hadoop存数据, 也可以是其他agent的Source。在日志数据较少时,可以将数据存储在文件系统中,并且设定一定的时间间隔保存数据。

 

3.4Flume拦截器、数据流以及可靠性介绍

Flume拦截器  

当我们需要对数据进行过滤时,除了我们在Source、 Channel和Sink进行代码修改之外,Flume为我们提供了拦截器,拦截器也是chain形式的。拦截器的位置在Source和Channel之间,当我们为Source指定拦截器后,我们在拦截器中会得到event,根据需求我们可以对event进行保留还是抛弃,抛弃的数据不会进入Channel中。

Flume数据流 Flume 的核心是把数据从数据源收集过来,再送到目的地。为了保证输送一定成功,在送到目的地之前,会先缓存数据,待数据真正到达目的地后,删除自己缓存的数据。Flume 传输的数据的基本单位是 Event,如果是文本文件,通常是一行记录,这也是事务的基本单位。 Event 从 Source,流向 Channel,再到 Sink,本身为一个 byte 数组,并可携带 headers 信息。 Event 代表着一个数据流的最小完整单元,从外部数据源来,向外部的目的地去。值得注意的是,Flume提供了大量内置的Source、Channel和Sink类型。不同类型的Source,Channel和Sink可以自由组合。组合方式基于用户设置的配置文件,非常灵活。比如:Channel可以把事件暂存在内存里,也可以持久化到本地硬盘上。Sink可以把日志写入HDFS, HBase,甚至是另外一个Source等等。Flume支持用户建立多级流,也就是说,多个agent可以协同工作,并且支持Fan-in、Fan-out、Contextual Routing、Backup Routes,这也正是Flume强大之处。

 

Flume可靠性

Flume 使用事务性的方式保证传送Event整个过程的可靠性。 Sink 必须在Event 被存入 Channel 后,或者已经被传达到下一站agent里,又或者已经被存入外部数据目的地之后,才能把 Event 从 Channel 中 remove 掉。这样数据流里的 event 无论是在一个 agent 里还是多个 agent 之间流转,都能保证可靠,因为以上的事务保证了 event 会被成功存储起来。比如 Flume支持在本地保存一份文件 channel 作为备份,而memory channel 将event存在内存 queue 里,速度快,但丢失的话无法恢复。

4.Flume使用场景

flume 套接字sink flume 使用_flume 套接字sink_03

4.1多个agent顺序连接

可以将多个Agent顺序连接起来,将最初的数据源经过收集,存储到最终的存储系统中。这是最简单的情况,一般情况下,应该控制这种顺序连接的Agent 的数量,因为数据流经的路径变长了,如果不考虑failover的话,出现故障将影响整个Flow上的Agent收集服务。

flume 套接字sink flume 使用_应用案例_04

4.2多个Agent的数据汇聚到同一个Agent

这种情况应用的场景比较多,比如要收集Web网站的用户行为日志, Web网站为了可用性使用的负载集群模式,每个节点都产生用户行为日志,可以为每 个节点都配置一个Agent来单独收集日志数据,然后多个Agent将数据最终汇聚到一个用来存储数据存储系统,如HDFS上。

flume 套接字sink flume 使用_flume_05

4.3多级流

什么是多级流?结合在云开发中的应用来举个例子,当syslog, java, nginx、 tomcat等混合在一起的日志流开始流入一个agent后,可以agent中将混杂的日志流分开,然后给每种日志建立一个自己的传输通道。

flume 套接字sink flume 使用_详细_06

4.3load balance功能

下图Agent1是一个路由节点,负责将Channel暂存的Event均衡到对应的多个Sink组件上,而每个Sink组件分别连接到一个独立的Agent上 。

flume 套接字sink flume 使用_详细_07

 

5.案例

5.1:exec + memory + ?

案例1:exec + memory + logger

监控一个文件到logger
vi ./conf/first.conf
#定义source|channel|sink组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#配置r1的属性
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/flume/test
#配置sinks的属性
a1.sinks.k1.type = logger
#配置channel的属性
a1.channels.c1.type = memory      #指定channel的类型为内存
a1.channels.c1.capacity = 10000   #存储事件的最大数量
a1.channels.c1.transactionCapacity = 10000       #接受的最大数量
#绑定source与sink于channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
测试:
flume-ng agent -c conf/  -f conf/first.conf -n a1 -Dflume.root.logger=INFO,console
echo '123156465' >> /home/flume/test

flume 套接字sink flume 使用_详细_08

案例2:exec + memory + hdfs

监控一个文件到hdfs
上传到hdfs
vi ./conf/e2h.conf
#定义source|channel|sink组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#配置r1的属性
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/flume/test
#配置sinks的属性
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/events/dt=%y-%m-%d
a1.sinks.k1.hdfs.filePrefix = events-      #指定sink写入HDFS文件的前缀名
a1.sinks.k1.hdfs.round = true              #是否开启时间戳的四舍五入
a1.sinks.k1.hdfs.roundValue = 10           #舍弃十分钟,也就是该目录每十分钟生成一个
a1.sinks.k1.hdfs.fileType=DataStream      #文件格式默认SequenceFile,--DateStream不会压缩输出文件
a1.sinks.k1.hdfs.writeFormat=Text         #向DFS文件里写的格式
a1.sinks.k1.hdfs.roundUnit = minute         #四舍五入的最小单位
a1.sinks.k1.hdfs.useLocalTimeStamp=true
#配置channel的属性
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 10000
#绑定source与sink于channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
测试:
flume-ng agent -c conf/  -f conf/e2h.conf -n a1 -Dflume.root.logger=INFO,console

5.2:spooldir + memory + ?

案例1:spooldir + memory + logger

监控目录
vi ./conf/spooldir
a1.sources=r1
a1.channels=c1
a1.sinks=s1
a1.sources.r1.type=spoolDir         
a1.sources.r1.spoolDir=/home/flume  #自己定义的spooldir的文件目录
a1.sources.r1.fileHeader=true               #是否在event的Header中添加文件名
a1.sources.r1.fileHeaderKey=file
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000
a1.sinks.s1.type = logger
a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
启动agent:
flume-ng agent -c ./conf/ -f ./conf/spooldir.conf -n a1 -Dflume.root.logger=INFO,console
测试:
向目录下添加文件

flume 套接字sink flume 使用_详细_09

5.3:syslogtcp + memory + ?

案例1、 syslogtcp + memory + logger

监控tcp
vi ./conf/syslogtcp.conf
a1.sources=r1
a1.channels=c1
a1.sinks=s1
a1.sources.r1.type=syslogtcp
a1.sources.r1.port=6666
a1.sources.r1.host=master
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000
a1.sinks.s1.type = logger
a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
启动agent:
flume-ng agent -c ./conf/ -f ./conf/syslogtcp.conf -n a1 -Dflume.root.logger=INFO,console
测试:
echo "hello qianfeng" | nc master 6666

flume 套接字sink flume 使用_数据_10

案例2、Multiport Syslog TCP Source

通过监听多个端口开始先数据转换成Event
a1.sources = r1  #定义的source
a1.channels = c1 #定义channel
a1.sources.r1.type = multiport_syslogtcp  #指定source类型为多端口监控
a1.sources.r1.channels = c1
a1.sources.r1.host = 0.0.0.0 #指定监控端口的所在的机器
a1.sources.r1.ports = 10001 10002 10003  #监控多个端口用空格开
a1.sources.r1.portHeader = port  #监控的是端口

flume 套接字sink flume 使用_flume_11

案例3、Syslog UDP Source

通过监听UDP协议的端口来实现数据转换成Event
a1.sources = r1
a1.channels = c1
a1.sources.r1.type = syslogudp #指定source类型为syslogUdp
a1.sources.r1.port = 5140  #指定监控的端口
a1.sources.r1.host = localhost
a1.sources.r1.channels = c1

5.4:http + memory + ?

 

案例1、http + memory + logger

监控http 
vi ./conf/http.conf
a1.sources=r1
a1.channels=c1
a1.sinks=s1
a1.sources.r1.type=org.apache.flume.source.http.HTTPSource
a1.sources.r1.port=6667
a1.sources.r1.bind=master
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000
a1.sinks.s1.type = logger
a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
启动agent:
flume-ng agent -c ./conf/ -f ./conf/http.conf -n a1 -Dflume.root.logger=INFO,console
测试:
curl -X POST -d '[{"headers":{"time":"2017-06-13"},"body":"this is http"}]' http://master:6667

flume 套接字sink flume 使用_应用案例_12

5.5:exec + file + ?

案例1、 exec + file + hdfs

监控文件 管道落地有检查点  速度慢但是准确性有保证 写入到hdfs
vi ./conf/file.conf
a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata

a1.channels.c1.type=file
a1.channels.c1.checkpointDir=/home/flumedata/checkpoint
a1.channels.c1.dataDirs=/home/flumedata/data

a1.sinks.s1.type = hdfs
a1.sinks.s1.hdfs.path = hdfs://hd/flumedata/events/%y-%m-%d/%H%M/%S
a1.sinks.s1.hdfs.filePrefix = master-
a1.sinks.s1.hdfs.fileSuffix=.log
a1.sinks.s1.hdfs.inUseSuffix=.tmp
a1.sinks.s1.hdfs.rollInterval=2
a1.sinks.s1.hdfs.rollSize=1024
a1.sinks.s1.hdfs.fileType=DataStream
a1.sinks.s1.hdfs.writeFormat=Text
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundValue = 1
a1.sinks.s1.hdfs.roundUnit = second
a1.sinks.s1.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
启动agent:
flume-ng agent -c ./conf/ -f ./conf/file.conf -n a1 -Dflume.root.logger=INFO,console
测试:
echo 'addtest txt' >> /home/flumedata/exedata

5.6:Kafka Source

tier1.sources.source1.type = org.apache.flume.source.kafka.KafkaSource #指定source的类型为kafka
tier1.sources.source1.channels = channel1
tier1.sources.source1.batchSize = 5000 #在一个批处理中写入通道的最大消息数
tier1.sources.source1.batchDurationMillis = 2000 #在将批处理写入通道之前的最大时间(以ms为单位)
tier1.sources.source1.kafka.bootstrap.servers = localhost:9092 #kafka地址
tier1.sources.source1.kafka.topics = test1, test2 #指定消费哪些主题,多个用逗号隔开
tier1.sources.source1.kafka.consumer.group.id = custom.g.id

tier1.sources.source1.type = org.apache.flume.source.kafka.KafkaSource #指定source的类型为kafka
tier1.sources.source1.channels = channel1
tier1.sources.source1.kafka.bootstrap.servers = localhost:9092 #kafka地址
tier1.sources.source1.kafka.topics.regex = ^topic[0-9]$ #正则匹配消费哪些主题
# the default kafka.consumer.group.id=flume is used

flume 套接字sink flume 使用_应用案例_13

5.6:更多Source 源及配置可以参考这里

传送门

6.flume拦截器

拦截器的种类介绍


1、Timestamp Interceptor(时间戳拦截器) flume中一个最经常使用的拦截器 ,该拦截器的作用是将时间戳插入到flume的事件报头中。如果不使用任何拦截器,flume接受到的只有message。时间戳拦截器的配置。 参数 默认值 描述 type   类型名称timestamp,也可以使用类名的全路径 preserveExisting false 如果设置为true,若事件中报头已经存在,不会替换时间戳报头的值 2、Host Interceptor(主机拦截器) 主机拦截器插入服务器的ip地址或者主机名,agent将这些内容插入到事件的报头中。时间报头中的key使用hostHeader配置,默认是host。主机拦截器的配置参数 默认值 描述 type   类型名称host hostHeader host 事件投的key useIP true 如果设置为false,host键插入主机名 preserveExisting false 如果设置为true,若事件中报头已经存在,不会替换host报头的值 3、静态拦截器(Static Interceptor) 静态拦截器的作用是将k/v插入到事件的报头中。配置如下参数 默认值 描述 type   类型名称static key key 事件头的key value value key对应的value值 preserveExisting true 如果设置为true,若事件中报头已经存在该key,不会替换value的值source连接到静态拦截器的配置: 4、正则过滤拦截器(Regex Filtering Interceptor) 在日志采集的时候,可能有一些数据是我们不需要的,这样添加过滤拦截器,可以过滤掉不需要的日志,也可以根据需要收集满足正则条件的日志。参数默认值描述 type 类型名称REGEX_FILTER regex .* 匹配除“\n”之外的任何个字符 excludeEvents false 默认收集匹配到的事件。如果为true,则会删除匹配到的event,收集未匹配到的。


2.1:案例1、静态拦截器

静态拦截器的作用是将k/v插入到事件的报头中
vi ./conf/ts1.conf
a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata
a1.sources.r1.interceptors = i1 i2 i3
a1.sources.r1.interceptors.i1.type = timestamp
a1.sources.r1.interceptors.i1.preserveExisting=true
a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i2.hostHeader = hostname
a1.sources.r1.interceptors.i2.preserveExisting=true
a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = city
a1.sources.r1.interceptors.i3.value = NEW_YORK

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = hdfs
a1.sinks.s1.hdfs.path = hdfs://hd/flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s1.hdfs.filePrefix = %{hostname}-
a1.sinks.s1.hdfs.fileSuffix=.log
a1.sinks.s1.hdfs.inUseSuffix=.tmp
a1.sinks.s1.hdfs.rollInterval=2
a1.sinks.s1.hdfs.rollSize=1024
a1.sinks.s1.hdfs.fileType=DataStream
a1.sinks.s1.hdfs.writeFormat=Text
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundValue = 1
a1.sinks.s1.hdfs.roundUnit = second
a1.sinks.s1.hdfs.useLocalTimeStamp=false

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

启动agent:
flume-ng agent -c ./conf/ -f ./conf/ts1.conf -n a1 -Dflume.root.logger=INFO,console
测试:

2.2:案例2、正则拦截器

根据正则表达式过滤监控内容
1~9开头的
vi ./conf/rex.conf
a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = regex_filter
a1.sources.r1.interceptors.i1.regex=^[0-9].*$
a1.sources.r1.interceptors.i1.excludeEvents=false


a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

启动agent:
flume-ng agent -c ./conf/ -f ./conf/rex.conf -n a1 -Dflume.root.logger=INFO,console
测试:

7.flume选择器

3.1:案例1、复制选择器

读监控文件复制到两个或多个上(logger和hdfs)
vi ./conf/rep.conf
a1.sources=r1
a1.channels=c1 c2
a1.sinks=s1 s2

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata
a1.sources.r1.selector.type = replicating
a1.sources.r1.selector.optional = c2

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=100
a1.channels.c2.keep-alive=3
a1.channels.c2.byteCapacityBufferPercentage = 20
a1.channels.c2.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sinks.s2.type = hdfs
a1.sinks.s2.hdfs.path = hdfs://hd/flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s2.hdfs.filePrefix = event-
a1.sinks.s2.hdfs.fileSuffix=.log
a1.sinks.s2.hdfs.inUseSuffix=.tmp
a1.sinks.s2.hdfs.rollInterval=2
a1.sinks.s2.hdfs.rollSize=1024
a1.sinks.s2.hdfs.fileType=DataStream
a1.sinks.s2.hdfs.writeFormat=Text
a1.sinks.s2.hdfs.round = true
a1.sinks.s2.hdfs.roundValue = 1
a1.sinks.s2.hdfs.roundUnit = second
a1.sinks.s2.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1 c2
a1.sinks.s1.channel=c1
a1.sinks.s2.channel=c2

3.2:案例4、复分选择器

根据条件分发到两个或者多个上(logger和hdfs)
vi ./conf/mul.conf
a1.sources=r1
a1.channels=c1 c2
a1.sinks=s1 s2

a1.sources.r1.type=org.apache.flume.source.http.HTTPSource
a1.sources.r1.port=6668
a1.sources.r1.bind=master
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = status
a1.sources.r1.selector.mapping.CZ = c1
a1.sources.r1.selector.mapping.US = c2
a1.sources.r1.selector.default = c1

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=100
a1.channels.c2.keep-alive=3
a1.channels.c2.byteCapacityBufferPercentage = 20
a1.channels.c2.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sinks.s2.type = hdfs
a1.sinks.s2.hdfs.path =hdfs://hd/flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s2.hdfs.filePrefix = event-
a1.sinks.s2.hdfs.fileSuffix=.log
a1.sinks.s2.hdfs.inUseSuffix=.tmp
a1.sinks.s2.hdfs.rollInterval=2
a1.sinks.s2.hdfs.rollSize=1024
a1.sinks.s2.hdfs.fileType=DataStream
a1.sinks.s2.hdfs.writeFormat=Text
a1.sinks.s2.hdfs.round = true
a1.sinks.s2.hdfs.roundValue = 1
a1.sinks.s2.hdfs.roundUnit = second
a1.sinks.s2.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1 c2
a1.sinks.s1.channel=c1
a1.sinks.s2.channel=c2

测试数据:
flume-ng agent -c ./conf/ -f ./conf/mul -n a1 -Dflume.root.logger=INFO,console

curl -X POST -d '[{"headers":{"status":"2017-06-13"},"body":"this is default"}]' http://master:6669
curl -X POST -d '[{"headers":{"status":"CZ"},"body":"this is CZ"}]' http://master:6669
curl -X POST -d '[{"headers":{"status":"US"},"body":"this is US"}]' http://master:6669          结果要到hdfs上看
curl -X POST -d '[{"headers":{"status":"ss"},"body":"this is ss"}]' http://master:6669

8.flume的集群搭建

两个或多个集中到一个上
创建三个文件 分别存放配置文件。vi hdflume.conf         conf下
master监控得到的内容发送到hdp02  hdp01监控到内容也给hdp02
-----------------------------------------------------------------------------------------------
#master的配置:
a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=syslogtcp
a1.sources.r1.port=6669
a1.sources.r1.host=master

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type =avro
a1.sinks.s1.hostname=hdp02
a1.sinks.s1.port=6669

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
-------------------------------------------------------------------------------------------
#hdp01的配置:
a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=syslogtcp
a1.sources.r1.port=6669
a1.sources.r1.host=hdp01

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type =avro
a1.sinks.s1.hostname=hdp02
a1.sinks.s1.port=6669

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
------------------------------------------------------------------------------------------
#hdp02的配置:
agent.sources=r1
agent.channels=c1
agent.sinks=s1

agent.sources.r1.type=avro
agent.sources.r1.port=6669
agent.sources.r1.bind=hdp02

agent.channels.c1.type=memory
agent.channels.c1.capacity=1000
agent.channels.c1.transactionCapacity=100
agent.channels.c1.keep-alive=3
agent.channels.c1.byteCapacityBufferPercentage = 20
agent.channels.c1.byteCapacity = 800000

agent.sinks.s1.type =logger

agent.sources.r1.channels=c1
agent.sinks.s1.channel=c1
 
先启动hdp02的agent: flume-1.6.0下
flume-ng agent -c ./conf/ -f ./conf/hdflume -n agent -Dflume.root.logger=INFO,console 
然后再启动master和hdp01的agent:
flume-ng agent -c ./conf/ -f ./conf/hdflume -n a1 -Dflume.root.logger=INFO,console 
flume-ng agent -c ./conf/ -f ./conf/hdflume -n a1 -Dflume.root.logger=INFO,console 

####然后测试:
在master或者dhp01上 
echo "hello qianfeng" | nc master 6669
echo "hello qianfeng" | nc hdp01 6669


 


master节点监控目录,监控到变化后由hdp01或者hdp02具体实现传输操作(HA,一个宕掉,另一个顶替)
监控目录,上传到hdfs。
-----------------------------------------------------------------------------------------
#master的配置
#agent1 name
agent1.channels = c1
agent1.sources = r1
agent1.sinks = k1 k2
#
##set 设置组,保证后面两台flume在一个组下
agent1.sinkgroups = g1
#
##set 设置管道
agent1.channels.c1.type = memory
agent1.channels.c1.capacity = 1000
agent1.channels.c1.transactionCapacity = 100
#监控文件
agent1.sources.r1.channels = c1
#监控文件夹
agent1.sources.r1.type = TAILDIR
#元数据保存位置
agent1.sources.r1.positionFile = /mytest/flume_log/taildir_position.json  #临时落地文件位置
agent1.sources.r1.filegroups = f1
agent1.sources.r1.filegroups.f1 = /root/jsondata/.*    #监控目录
agent1.sources.r1.fileHeader = true
#
agent1.sources.r1.interceptors = i1 i2
agent1.sources.r1.interceptors.i1.type = static
agent1.sources.r1.interceptors.i1.key = Type
agent1.sources.r1.interceptors.i1.value = LOGIN
agent1.sources.r1.interceptors.i2.type = timestamp
#
## set sink1
agent1.sinks.k1.channel = c1
agent1.sinks.k1.type = avro
agent1.sinks.k1.hostname = hdp01
agent1.sinks.k1.port = 52095
#
## set sink2
agent1.sinks.k2.channel = c1
agent1.sinks.k2.type = avro
agent1.sinks.k2.hostname = hdp02
agent1.sinks.k2.port = 52095
#
##set sink group
agent1.sinkgroups.g1.sinks = k1 k2
#
##set failover
agent1.sinkgroups.g1.processor.type = failover
agent1.sinkgroups.g1.processor.priority.k1 = 10
agent1.sinkgroups.g1.processor.priority.k2 = 1
agent1.sinkgroups.g1.processor.maxpenalty = 10000
---------------------------------------------------------------------------------------------
#hdp01的配置
#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = hdp01
a1.sources.r1.port = 52095
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
a1.sources.r1.interceptors.i1.key = Collector
a1.sources.r1.interceptors.i1.value = hdp01
a1.sources.r1.channels = c1
#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path=/flume/failover/bdp_day=%Y%m%d
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=TEXT
a1.sinks.k1.hdfs.rollInterval=0
a1.sinks.k1.hdfs.rollCount=0
a1.sinks.k1.hdfs.rollSize=104857600
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d
a1.sinks.k1.hdfs.fileSuffix=.log
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 24
a1.sinks.k1.hdfs.roundUnit = hour
a1.sinks.k1.hdfs.useLocalTimeStamp=true
---------------------------------------------------------------------------------------------
#hdp02的配置
#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#
## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = hdp02
a1.sources.r1.port = 52095
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
a1.sources.r1.interceptors.i1.key = Collector
a1.sources.r1.interceptors.i1.value = hdp02
a1.sources.r1.channels = c1
#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path=/flume/failover/bdp_day=%Y%m%d
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=TEXT
a1.sinks.k1.hdfs.rollInterval=0
a1.sinks.k1.hdfs.rollCount=0
a1.sinks.k1.hdfs.rollSize=104857600
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d
a1.sinks.k1.hdfs.fileSuffix=.log
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 24
a1.sinks.k1.hdfs.roundUnit = hour
a1.sinks.k1.hdfs.useLocalTimeStamp=true

启动:
先启动hdp01和hdp02的agent: flume-1.9.0下
flume-ng agent -c ./conf/ -f ./conf/hdflume -n a1 -Dflume.root.logger=INFO,console 
flume-ng agent -c ./conf/ -f ./conf/hdflume -n a1 -Dflume.root.logger=INFO,console 
然后再启动master的agent:
flume-ng agent -c ./conf/ -f ./conf/hdflume -n agent1 -Dflume.root.logger=INFO,console 
测试:
向master的监控目录上传文件。观看hdp01或者hdp02是否上传文件。