$>flume-ng avro-client -H localhost -p 4141 -F /xxx(文件)
上面出错改成这个
$>flume-ng agent -z s101:2181,s102:2181 -p /flume --name a1 -Dflume.root.logger=INFO,console
通过这个连接:
$>flume-ng avro-client -H localhost -p 4141 -F /xxx(文件)
可以用火狐的下载的poster插件发送,将数据写在下方,点post发送。(在火狐Tools:Add-ons)
它没有端口
、
自定义source源:
打成jar包,复制到Flume/lib下。
sink:
报错:
修改成:
开启服务:
连接:
hive sink:
定义源后打成jar包,放到flume/lib
avro sink:
hbase sink:
AsyncHbase sink:
拦截器 依赖是下面那个:
----------------------------------------------------------------------------------------------------------------------------
添加依赖:
在cmd中输入services.msc开启网卡服务界面
没有事务,报错,
-----------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
1 .背景
flume是由cloudera软件公司产出的可分布式日志收集系统,后与2009年被捐赠了apache软件基金会,为hadoop相关组件之一。尤其近几年随着flume的不断被完善以及升级版本的逐一推出,特别是flume-ng;同时flume内部的各种组件不断丰富,用户在开发的过程中使用的便利性得到很大的改善,现已成为apache top项目之一.
2 .概述
1. 什么是flume?
flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方(比如文本、HDFS、Hbase等)的能力 。
flume的数据流由事件(Event)贯穿始终。事件是Flume的基本数据单位,它携带日志数据(字节数组形式)并且携带有头信息,这些Event由Agent外部的Source生成,当Source捕获事件后会进行特定的格式化,然后Source会把事件推入(单个或多个)Channel中。你可以把Channel看作是一个缓冲区,它将保存事件直到Sink处理完该事件。Sink负责持久化日志或者把事件推向另一个Source。
apache Flume 是一个从可以收集例如日志,事件等数据资源,并将这些数量庞大的数据从各项数据资源中集中起来存储的工具/服务,或者数集中机制。flume具有高可用,分布式,配置工具,其设计的原理也是基于将数据流,如日志数据从各种网站服务器上汇集起来存储到HDFS,HBase等集中存储器中。其结构如下图所示:
Flume初始的发行版本目前被统称为 Flume OG(original generation),属于 cloudera。但随着 FLume 功能的扩展,Flume OG 代码工程臃肿、核心组件设计不合理、核心配置不标准等缺点暴露出来,尤其是在 Flume OG 的最后一个发行版本 0.94.0 中,日志传输不稳定的现象尤为严重,为了解决这些问题,2011 年 10 月 22 号,cloudera 完成了 Flume-728,对 Flume 进行了里程碑式的改动:重构核心组件、核心配置以及代码架构,重构后的版本统称为 Flume NG(next generation);改动的另一原因是将 Flume 纳入 apache 旗下,clouderaFlume 改名为 Apache Flume。IBM 的这篇文章:《 Flume NG:Flume 发展史上的第一次革命 》,从基本组件以及用户体验的角度阐述 Flume OG 到 Flume NG 发生的革命性变化。本文就不再赘述各种细枝末节了,不过这里还是简要提下 Flume NG (1.x.x)的主要变化:
sources和sinks 使用channels进行链接
两个主要channel 。1, in-memory channel 非持久性支持,速度快。2 , JDBC-based channel 持久性支持。
不再区分逻辑和物理node,所有物理节点统称为 “agents”,每个agents 都能运行0个或多个sources 和sinks
不再需要master节点和对zookeeper的依赖,配置文件简单化。
插件化,一部分面对用户,工具或系统开发人员。
使用Thrift、Avro Flume sources 可以从flume0.9.4 发送 events 到flume 1.x
注:本文所使用的 Flume 版本为 flume-1.4.0-cdh4.7.0,不需要额外的安装过程,解压缩即可用。
Flume-og和Flume-ng的区别:
主要区别如下:
1. Flume-og中采用master结构,为了保证数据的一致性,引入zookeeper进行管理。Flume-ng中取消了集中master机制和zookeeper管理机制,变成了一个纯粹的传输工具。
2. Flume-ng中采用不同的线程进行数据的读写操作;在Flume-og中,读数据和写数据是由同一个线程操作的,如果写出比较慢的话,可能会阻塞flume的接收数据的能力。
2.应用场景
比如我们在做一个电子商务网站,然后我们想从消费用户中访问点特定的节点区域来分析消费者的行为或者购买意图. 这样我们就可以更加快速的将他想要的推送到界面上,实现这一点,我们需要将获取到的她访问的页面以及点击的产品数据等日志数据信息收集并移交给Hadoop平台上去分析.而Flume正是帮我们做到这一点。现在流行的内容推送,比如广告定点投放以及新闻私人定制也是基于次,不过不一定是使用FLume,毕竟优秀的产品很多,比如facebook的Scribe,还有Apache新出的另一个明星项目chukwa,还有淘宝Time Tunnel。
3.Flume的优势
1. Flume可以将应用产生的数据存储到任何集中存储器中,比如HDFS,HBase
2. 当收集数据的速度超过将写入数据的时候,也就是当收集信息遇到峰值时,这时候收集的信息非常大,甚至超过了系统的写入数据能力,这时候,Flume会在数据生产者和数据收容器间做出调整,保证其能够在两者之间提供一共平稳的数据.
3. 提供上下文路由特征
4. Flume的管道是基于事务,保证了数据在传送和接收时的一致性.
5. Flume是可靠的,容错性高的,可升级的,易管理的,并且可定制的。
4. Flume具有的特征:
1. Flume可以高效率的将多个网站服务器中收集的日志信息存入HDFS/HBase中
2. 使用Flume,我们可以将从多个服务器中获取的数据迅速的移交给Hadoop中
3. 除了日志信息,Flume同时也可以用来接入收集规模宏大的社交网络节点事件数据,比如facebook,twitter,电商网站如亚马逊,flipkart等
4. 支持各种接入资源数据的类型以及接出数据类型
5. 支持多路径流量,多管道接入流量,多管道接出流量,上下文路由等
6. 可以被水平扩展
3.Flume的结构
1. flume的外部结构:
如上图所示,数据发生器(如:facebook,twitter)产生的数据被被单个的运行在数据发生器所在服务器上的agent所收集,之后数据收容器从各个agent上汇集数据并将采集到的数据存入到HDFS或者HBase中
2.Flume 事件
事件作为Flume内部数据传输的最基本单元.它是由一个转载数据的字节数组(该数据组是从数据源接入点传入,并传输给传输器,也就是HDFS/HBase)和一个可选头部构成.
典型的Flume事件如下面结构所示:
我们在将event在私人定制插件时比如:flume-hbase-sink插件是,获取的就是event然后对其解析,并依据情况做过滤等,然后在传输给HBase或者HDFS.
3.FlumeAgent
我们在了解了Flume的外部结构之后,知道了Flume内部有一个或者多个Agent,然而对于每一个Agent来说,它就是一共独立的守护进程(JVM),它从客户端哪儿接收收集,或者从其他的 Agent哪儿接收,然后迅速的将获取的数据传给下一个目的节点sink,或者agent. 如下图所示flume的基本模型
Agent主要由:source,channel,sink三个组件组成.
组件 | 功能 |
Agent | 使用JVM 运行Flume。每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks。 |
Client | 生产数据,运行在一个独立的线程。 |
Source | 从Client收集数据,传递给Channel。 |
Sink | 从Channel收集数据,运行在一个独立线程。 |
Channel | 连接 sources 和 sinks ,这个有点像一个队列。 |
Events | 可以是日志记录、 avro 对象等。 |
Source:
从数据发生器接收数据,并将接收的数据以Flume的event格式传递给一个或者多个通道channal,Flume提供多种数据接收的方式,比如Avro,Thrift,twitter1%等
Channel:
channal是一种短暂的存储容器,它将从source处接收到的event格式的数据缓存起来,直到它们被sinks消费掉,它在source和sink间起着一共桥梁的作用,channal是一个完整的事务,这一点保证了数据在收发的时候的一致性. 并且它可以和任意数量的source和sink链接. 支持的类型有: JDBC channel , File System channel, Memort channel等.
sink:
sink将数据存储到集中存储器比如Hbase和HDFS,它从channals消费数据(events)并将其传递给目标地. 目标地可能是另一个sink,也可能HDFS,HBase.
它的组合形式举例:
Flume中以Agent为基本单位,一个agent可以包括source、channel、sink,三种组件都可以有多个。其中source组件主要功能是接收外部数据,并将数据传递到channel中;sink组件主要功能是发送flume接收到的数据到目的地;channel的主要作用就是数据传输和保存的一个作用。Flume主要分为三类结构:单agent结构、多agent链式结构和多路复用agent结构。
单agent结构:
多agent链式结构:
多路复用agent结构:
以上介绍的flume的主要组件,下面介绍一下Flume插件:
1.Interceptors拦截器
用于source和channel之间,用来更改或者检查Flume的events数据
2. 管道选择器 channels Selectors
在多管道是被用来选择使用那一条管道来传递数据(events). 管道选择器又分为如下两种:
默认管道选择器: 每一个管道传递的都是相同的events
多路复用通道选择器: 依据每一个event的头部header的地址选择管道.
3.sink线程
用于激活被选择的sinks群中特定的sink,用于负载均衡.
1.2 高可靠性
作为生产环境运行的软件,高可靠性是必须的。
从单agent来看,Flume使用基于事务的数据传递方式来保证事件传递的可靠性。Source和Sink被封装进一个事务。事件被存放在Channel中直到该事件被处理,Channel中的事件才会被移除。这是Flume提供的点到点的可靠机制。
从多级流来看,前一个agent的sink和后一个agent的source同样有它们的事务来保障数据的可靠性。
1.3 可恢复性
还是靠Channel。推荐使用FileChannel,事件持久化在本地文件系统里(性能较差)。
2、Flume整体架构介绍
Flume架构整体上看就是 source -->c hannel --> sink 的三层架构,类似生成者和消费者的架构,他们之间通过queue(channel)传输,解耦。
Source:完成对日志数据的收集,分成 transtion和 event 打入到channel之中。
Channel:主要提供一个队列的功能,对source提供中的数据进行简单的缓存。
Sink:取出Channel中的数据,进行相应的存储文件系统,数据库,或者提交到远程服务器。
对现有程序改动最小的使用方式是使用是直接读取程序原来记录的日志文件,基本可以实现无缝接入,不需要对现有程序进行任何改动。
对于直接读取文件Source, 主要有两种方式:
2.1Exec source
可通过写Unix command的方式组织数据,最常用的就是tail -F [file]。
可以实现实时传输,但在flume不运行和脚本错误时,会丢数据,也不支持断点续传功能。因为没有记录上次文件读到的位置,从而没办法知道,下次再读时,从什么地方开始读。特别是在日志文件一直在增加的时候。flume的source挂了。等flume的source再次开启的这段时间内,增加的日志内容,就没办法被source读取到了。不过flume有一个execStream的扩展,可以自己写一个监控日志增加情况,把增加的日志,通过自己写的工具把增加的内容,传送给flume的node。再传送给sink的node。要是能在tail类的source中能支持,在node挂掉这段时间的内容,等下次node开启后在继续传送,那就更完美了。
2.2 Spooling DirectorySource
SpoolSource:是监测配置的目录下新增的文件,并将文件中的数据读取出来,可实现准实时。需要注意两点:1、拷贝到spool目录下的文件不可以再打开编辑。2、spool目录下不可包含相应的子目录。在实际使用的过程中,可以结合log4j使用,使用log4j的时候,将log4j的文件分割机制设为1分钟一次,将文件拷贝到spool的监控目录。log4j有一个TimeRolling的插件,可以把log4j分割的文件到spool目录。基本实现了实时的监控。Flume在传完文件之后,将会修改文件的后缀,变为.COMPLETED(后缀也可以在配置文件中灵活指定)
ExecSource,SpoolSource对比:ExecSource可以实现对日志的实时收集,但是存在Flume不运行或者指令执行出错时,将无法收集到日志数据,无法何证日志数据的完整性。SpoolSource虽然无法实现实时的收集数据,但是可以使用以分钟的方式分割文件,趋近于实时。如果应用无法实现以分钟切割日志文件的话,可以两种收集方式结合使用。
Channel有多种方式:有MemoryChannel, JDBCChannel, MemoryRecoverChannel, FileChannel。MemoryChannel可以实现高速的吞吐,但是无法保证数据的完整性。MemoryRecoverChannel在官方文档的建议上已经建义使用FileChannel来替换。FileChannel保证数据的完整性与一致性。在具体配置FileChannel时,建议FileChannel设置的目录和程序日志文件保存的目录设成不同的磁盘,以便提高效率。
Sink在设置存储数据时,可以向文件系统中,数据库中,hadoop中储数据,在日志数据较少时,可以将数据存储在文件系中,并且设定一定的时间间隔保存数据。在日志数据较多时,可以将相应的日志数据存储到Hadoop中,便于日后进行相应的数据分析。
安装flume:
1. 下载apache-flume-1.6.0-bin.tar.gz
2. tar -xzvf apache-flume-1.6.0-bin.tar.gz
3. 配置环境变量
[/etc/environment]
FLUME_HOME=
PATH="..:"
4. 验证安装
$>flume-ng version
5. 配置flume
[flume/conf/flume]
配置flume:
进入flume的conf配置包中,使用命令touch flume.conf,然后cp flume-conf.properties.template flume.conf
使vim/gedit flume.conf 编辑配置文件,需要说明的的是,Flume conf文件用的是Java版的property文件的key-value键值对模式.
在Flume配置文件中,我们需要
1. 需要命名当前使用的Agent的名称.
2. 命名Agent下的source的名字.
3. 命名Agent下的channal的名字.
4. 命名Agent下的sink的名字.
5. 将source和sink通过channal绑定起来.
一般来说,在Flume中会存在着多个Agent,所以我们需要给它们分别取一个名字来区分它们,注意名字不要相同,名字保持唯一!
例如:
#Agent取名为 agent_name
#source 取名为 source_name ,一次类推
agent_name.source = source_name
agent_name.channels = channel_name
agent_name.sinks = sink_name
上图对应的是单个Agent,单个sink,单个channel情况,如下图
如果我们需要在一个Agent上配置n个sink,m个channel(n>1, m>1),
那么只需要这样配置即可:
#Agent取名为 agent_name
#source 取名为 source_name ,一次类推
agent_name.source = source_name ,source_name1
agent_name.channels = channel_name,channel_name1
agent_name.sinks = sink_name,sink_name1
上面的配置就表示一个Agent中有两个 source,sink,channel的情况,如图所示
以上是对多sink,channel,source情况,对于 多个Agent,只需要给每个Agent取一个独一无二的名字即可!
Flume支持各种各样的sources,sinks,channels,它们支持的类型如下:
Sources | Channels | Sinks |
· Avro Source · Thrift Source · Exec Source · JMS Source · Spooling Directory Source · Twitter 1% firehose Source · Kafka Source · NetCat Source · Sequence Generator Source · Syslog Sources · Syslog TCP Source · Multiport Syslog TCP Source · Syslog UDP Source · HTTP Source · Stress Source · Legacy Sources · Thrift Legacy Source · Custom Source · Scribe Source | · Memory Channel · JDBC Channel · Kafka Channel · File Channel · Spillable Memory Channel · Pseudo Transaction Channel | · HDFS Sink · Hive Sink · Logger Sink · Avro Sink · Thrift Sink · IRC Sink · File Roll Sink · Null Sink · HBaseSink · AsyncHBaseSink · MorphlineSolrSink · ElasticSearchSink · Kite Dataset Sink · Kafka Sink |
以上的类型,你可以根据自己的需求来搭配组合使用,当然如果你愿意,你可以为所欲为的搭配.比如我们使用Avro source类型,采用Memorychannel,使用HDFS sink存储,那我们的配置可以接着上的配置这样写
#Agent取名为 agent_name
#source 取名为 source_name ,一次类推
agent_name.source = Avro
agent_name.channels = MemoryChannel
agent_name.sinks = HDFS
当你命名好Agent的组成部分后,你还需要对Agent的组成sources, sinks, channles去一一描述. 下面我们来逐一的细说;
a1.sources=r1,r2
a1.sinks=s1,s2
a1.channels=c1,c2
#source-r1
a1.sources.r1.type=
a1.sources.r1.xxx=
a1.sources.r1.yyy=
#sink-s1
a1.sinks.s1.type=
a1.sinks.s1.xxx=
#channel-c1
a1.channels.c1.type=
a1.channels.c1.xxx=
#binding,source可以配置多个channel
a1.sources.r1.channels=c1
#sink只能配置一个channel
a1.sinks.s1.channel=c1
Flume案例1:
使用netcatsource监听客户端的请求,使用memory channel作为数据的传输通道,使用logger sink打印监听到的信息。
步骤:
1. 在conf文件夹中建立test1.conf,里面是agent的配置。
2. 启动flume-ng agent --conf ./conf/--conf-file ./conf/test.conf --name a1 -Dflume.root.logger=INFO,console。
3. 使用telenet命令连接机器,命令:telnet 192.168.134.100 44444
或nclocalhost 44444
4. 输入信息查看是否成功。
>cpflume-conf.properties.template test1.properties
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 inmemory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to thechannel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2.运行flume agent
#--conf :配置目录
#--conf-file : 配置文件
#--name : 代理名称
#-D : 指定额外的参数
>flume-ng agent --conf conf --conf-file example.conf --name a1-Dflume.root.logger=INFO,console
案例2:
Avro
Avro可以发送一个给定的文件给Flume,Avro 源使用AVRO RPC机制。
a)创建agent配置文件
>cp flume-conf.properties.templateavro.properties
root@m1:/home/hadoop#vi/home/hadoop/flume-1.5.0-bin/conf/avro.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 4141
# Describe the sink
a1.sinks.k1.type = logger
# Use a channel which buffers events inmemory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to thechannel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2.启动
$>flume-ngagent --conf . --conf-file avro_r.conf -n a1 -Dflume.root.logger=INFO,console
2. 通过flume-ng avro-client命令发送avro数据消息
$>echo "hello world" >log.00
$>flume-ng avro-client -H localhost-p 4141 -F /home/neworigin/log.00
案例3:Spool
Spool监测配置的目录下新增的文件,并将文件中的数据读取出来。需要注意两点:
1) 拷贝到spool目录下的文件不可以再打开编辑。
2) spool目录下不可包含相应的子目录
a)创建agent配置文件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.channels = c1
a1.sources.r1.spoolDir =/home/neworigin/logs
a1.sources.r1.fileHeader = true
# Describe the sink
a1.sinks.k1.type = logger
# Use a channel which buffers events inmemory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to thechannel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
b)启动flume agent a1
>flume-ngagent --conf . --conf-file spool.conf -n a1 -Dflume.root.logger=INFO,console
c)追加文件到/home/neworigin/logs目录
> echo"spool test1" > /home/neworigin/logs/spool_text.log
案例4:Exec
EXEC执行一个给定的命令获得输出的源,如果要使用tail命令,必选使得file足够大才能看到输出内容
a)创建agent配置文件
>cp flume-conf.properties.templateexec_tail.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= exec
a1.sources.r1.channels= c1
a1.sources.r1.command= tail -F /home/neworigin/logs/log_exec_tail
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
b) 启动flume agent a1
>flume-ng agent --conf . --conf-file sexec_tail.conf -n a1-Dflume.root.logger=INFO,console
c)生成足够多的内容在文件里
>for i in {1..100};do echo "exec tail$i">> /home/neworigin/logs/log_ done
案例5:Syslogtcp
Syslogtcp监听TCP的端口做为数据源
a)创建agent配置文件
>cpflume-conf.properties.template syslog_tcp.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.host= localhost
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
b)启动flume agent a1
>flume-ngagent --conf . --conf-file syslog_tcp.conf -n a1-Dflume.root.logger=INFO,console
c)测试产生syslog
> echo"hello idoall.org syslog" | nc localhost 5140
案例6:JSONHandler
a)创建agent配置文件
>cpflume-conf.properties.template post_json.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= org.apache.flume.source.http.HTTPSource
a1.sources.r1.port= 8888
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
b)启动flume agent a1
>flume-ngagent --conf . --conf-file post_json.conf -n a1-Dflume.root.logger=INFO,console
c)生成JSON 格式的POSTrequest
>curl -X POST-d '[{ "headers" :{"a" : "a1","b" :"b1"},"body" : "idoall.org_body"}]'http://localhost:8888
6)案例6:Hadoop sink
a)创建agent配置文件
[hdfs_sink.conf]
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.host= localhost
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= hdfs
a1.sinks.k1.channel= c1
a1.sinks.k1.hdfs.path= hdfs://s100:9000/user/flume/syslogtcp
a1.sinks.k1.hdfs.filePrefix= Syslog
a1.sinks.k1.hdfs.round= true
a1.sinks.k1.hdfs.roundValue= 10
a1.sinks.k1.hdfs.roundUnit= minute
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
复制代码
b)启动flume agent a1
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/hdfs_sink.conf -n a1-Dflume.root.logger=INFO,console
c)测试产生syslog
root@m1:/home/hadoop#echo "hello idoall flume -> hadoop testing one" | nc localhost5140
e)在m1上再打开一个窗口,去hadoop上检查文件是否生成
案例7:File Roll Sink
a)创建agent配置文件
[file_roll.conf]
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5555
a1.sources.r1.host= localhost
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= file_roll
a1.sinks.k1.sink.directory= /home/hadoop/flume-1.5.0-bin/logs
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
b)启动flume agent a1
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/file_roll.conf -n a1-Dflume.root.logger=INFO,console
c)测试产生log
>echo"hello idoall.org syslog" | nc localhost 5555
> echo"hello idoall.org syslog 2" | nc localhost 5555
d)查看/home/hadoop/flume-1.5.0-bin/logs下是否生成文件,默认每30秒生成一个新文件
案例8:Replicating Channel Selector
Flume支持Fan out流从一个源到多个通道。有两种模式的Fan out,分别是复制和复用。在复制的情况下,流的事件被发送到所有的配置通道。在复用的情况下,事件被发送到可用的渠道中的一个子集。Fan out流需要指定源和Fan out通道的规则。
这次我们需要用到m1,m2两台机器
a)在m1创建replicating_Channel_Selector配置文件
[replicating_Channel_Selector.conf]
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1c2
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.host= localhost
a1.sources.r1.channels= c1 c2
a1.sources.r1.selector.type= replicating
# Describe thesink
a1.sinks.k1.type= avro
a1.sinks.k1.channel= c1
a1.sinks.k1.hostname= m1
a1.sinks.k1.port= 5555
a1.sinks.k2.type= avro
a1.sinks.k2.channel= c2
a1.sinks.k2.hostname= m2
a1.sinks.k2.port= 5555
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
a1.channels.c2.type= memory
a1.channels.c2.capacity= 1000
a1.channels.c2.transactionCapacity= 100
b)在m1创建replicating_Channel_Selector_avro配置文件
[replicating_Channel_Selector_avro.conf]
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= avro
a1.sources.r1.channels= c1
a1.sources.r1.bind= 0.0.0.0
a1.sources.r1.port= 5555
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
c)在m1上将2个配置文件复制到m2上一份
d)打开4个窗口,在m1和m2上同时启动两个flume agent
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector_avro.conf-n a1 -Dflume.root.logger=INFO,console
>lume-ngagent -c . -f/home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector.conf -n a1-Dflume.root.logger=INFO,console
e)然后在m1或m2的任意一台机器上,测试产生syslog
> echo"hello idoall.org syslog" | nc localhost 5140
f)在m1和m2的sink窗口,分别可以看到以下信息,这说明信息得到了同步:
14/08/1014:08:18 INFO ipc.NettyServer: Connection to /192.168.1.51:46844 disconnected.
14/08/1014:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 =>/192.168.1.50:5555] OPEN
14/08/1014:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 =>/192.168.1.50:5555] BOUND: /192.168.1.50:5555
14/08/1014:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 =>/192.168.1.50:5555] CONNECTED: /192.168.1.50:35873
14/08/1014:08:59 INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 =>/192.168.1.50:5555] OPEN
14/08/1014:08:59 INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 =>/192.168.1.50:5555] BOUND: /192.168.1.50:5555
14/08/10 14:08:59INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 =>/192.168.1.50:5555] CONNECTED: /192.168.1.51:46858
14/08/1014:09:20 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 68 65 6C 6C 6F 20 69 64 6F 61 6C6C 2E 6F 72 67 hello idoall.org }
案例9:Multiplexing Channel Selector
a)在m1创建Multiplexing_Channel_Selector配置文件
[Multiplexing_Channel_Selector.conf]
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1c2
#Describe/configure the source
a1.sources.r1.type= org.apache.flume.source.http.HTTPSource
a1.sources.r1.port= 5140
a1.sources.r1.channels= c1 c2
a1.sources.r1.selector.type= multiplexing
a1.sources.r1.selector.header= type
#映射允许每个值通道可以重叠。默认值可以包含任意数量的通道。
a1.sources.r1.selector.mapping.baidu= c1
a1.sources.r1.selector.mapping.ali= c2
a1.sources.r1.selector.default= c1
# Describe thesink
a1.sinks.k1.type= avro
a1.sinks.k1.channel= c1
a1.sinks.k1.hostname= m1
a1.sinks.k1.port= 5555
a1.sinks.k2.type= avro
a1.sinks.k2.channel= c2
a1.sinks.k2.hostname= m2
a1.sinks.k2.port= 5555
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
a1.channels.c2.type= memory
a1.channels.c2.capacity= 1000
a1.channels.c2.transactionCapacity= 100
b)在m1创建Multiplexing_Channel_Selector_avro配置文件
[Multiplexing_Channel_Selector_avro.conf]
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= avro
a1.sources.r1.channels= c1
a1.sources.r1.bind= 0.0.0.0
a1.sources.r1.port= 5555
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
c)将2个配置文件复制到m2上一份
d)打开4个窗口,在m1和m2上同时启动两个flume agent
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector_avro.conf-n a1 -Dflume.root.logger=INFO,console
>flume-ngagent -c . -f/home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector.conf -n a1-Dflume.root.logger=INFO,console
e)然后在m1或m2的任意一台机器上,测试产生syslog
>curl -X POST-d '[{ "headers" :{"type" :"baidu"},"body" : "idoall_TEST1"}]'http://localhost:5140 && curl -X POST -d '[{ "headers":{"type" : "ali"},"body" :"idoall_TEST2"}]' http://localhost:5140 && curl -X POST -d'[{ "headers" :{"type" : "qq"},"body" :"idoall_TEST3"}]' http://localhost:5140
f)在m1的sink窗口,可以看到以下信息:
14/08/1014:32:21 INFO node.Application: Starting Sink k1
14/08/1014:32:21 INFO node.Application: Starting Source r1
14/08/1014:32:21 INFO source.AvroSource: Starting Avro source r1: { bindAddress:0.0.0.0, port: 5555 }...
14/08/1014:32:21 INFO instrumentation.MonitoredCounterGroup: Monitored counter groupfor type: SOURCE, name: r1: Successfully registered new MBean.
14/08/1014:32:21 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE,name: r1 started
14/08/1014:32:21 INFO source.AvroSource: Avro source r1 started.
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 =>/192.168.1.50:5555] OPEN
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 =>/192.168.1.50:5555] BOUND: /192.168.1.50:5555
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 =>/192.168.1.50:5555] CONNECTED: /192.168.1.50:35916
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 =>/192.168.1.50:5555] OPEN
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 =>/192.168.1.50:5555] BOUND: /192.168.1.50:5555
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 =>/192.168.1.50:5555] CONNECTED: /192.168.1.51:46945
14/08/1014:34:11 INFO sink.LoggerSink: Event: { headers:{type=baidu} body: 69 64 6F 616C 6C 5F 54 45 53 54 31 idoall_TEST1 }
14/08/1014:34:57 INFO sink.LoggerSink: Event: { headers:{type=qq} body: 69 64 6F 61 6C6C 5F 54 45 53 54 33 idoall_TEST3 }
g)在m2的sink窗口,可以看到以下信息:
14/08/1014:32:27 INFO node.Application: Starting Sink k1
14/08/1014:32:27 INFO node.Application: Starting Source r1
14/08/1014:32:27 INFO source.AvroSource: Starting Avro source r1: { bindAddress:0.0.0.0, port: 5555 }...
14/08/1014:32:27 INFO instrumentation.MonitoredCounterGroup: Monitored counter groupfor type: SOURCE, name: r1: Successfully registered new MBean.
14/08/1014:32:27 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE,name: r1 started
14/08/1014:32:27 INFO source.AvroSource: Avro source r1 started.
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 =>/192.168.1.51:5555] OPEN
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 =>/192.168.1.51:5555] BOUND: /192.168.1.51:5555
14/08/1014:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 =>/192.168.1.51:5555] CONNECTED: /192.168.1.50:38104
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 =>/192.168.1.51:5555] OPEN
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 =>/192.168.1.51:5555] BOUND: /192.168.1.51:5555
14/08/1014:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 =>/192.168.1.51:5555] CONNECTED: /192.168.1.51:48599
14/08/1014:34:33 INFO sink.LoggerSink: Event: { headers:{type=ali} body: 69 64 6F 61 6C6C 5F 54 45 53 54 32 idoall_TEST2 }
可以看到,根据header中不同的条件分布到不同的channel上
案例10:Flume Sink Processors
failover的机器是一直发送给其中一个sink,当这个sink不可用的时候,自动发送到下一个sink。
a)在m1创建Flume_Sink_Processors配置文件
[Flume_Sink_Processors.conf]
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1c2
#这个是配置failover的关键,需要有一个sink group
a1.sinkgroups =g1
a1.sinkgroups.g1.sinks= k1 k2
#处理的类型是failover
a1.sinkgroups.g1.processor.type= failover
#优先级,数字越大优先级越高,每个sink的优先级必须不相同
a1.sinkgroups.g1.processor.priority.k1= 5
a1.sinkgroups.g1.processor.priority.k2= 10
#设置为10秒,当然可以根据你的实际状况更改成更快或者很慢
a1.sinkgroups.g1.processor.maxpenalty= 10000
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.channels= c1 c2
a1.sources.r1.selector.type= replicating
# Describe thesink
a1.sinks.k1.type= avro
a1.sinks.k1.channel= c1
a1.sinks.k1.hostname= m1
a1.sinks.k1.port= 5555
a1.sinks.k2.type= avro
a1.sinks.k2.channel= c2
a1.sinks.k2.hostname= m2
a1.sinks.k2.port= 5555
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
a1.channels.c2.type= memory
a1.channels.c2.capacity= 1000
a1.channels.c2.transactionCapacity= 100
复制代码
b)在m1创建Flume_Sink_Processors_avro配置文件
root@m1:/home/hadoop#vi /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= avro
a1.sources.r1.channels= c1
a1.sources.r1.bind= 0.0.0.0
a1.sources.r1.port= 5555
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
c)将2个配置文件复制到m2上一份
d)打开4个窗口,在m1和m2上同时启动两个flume agent
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf-n a1 -Dflume.root.logger=INFO,console
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors.conf -na1 -Dflume.root.logger=INFO,console
e)然后在m1或m2的任意一台机器上,测试产生log
>echo"idoall.org test1 failover" | nc localhost 5140
f)因为m2的优先级高,所以在m2的sink窗口,可以看到以下信息,而m1没有:
14/08/1015:02:46 INFO ipc.NettyServer: Connection to /192.168.1.51:48692 disconnected.
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 =>/192.168.1.51:5555] OPEN
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 =>/192.168.1.51:5555] BOUND: /192.168.1.51:5555
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 =>/192.168.1.51:5555] CONNECTED: /192.168.1.51:48704
14/08/1015:03:26 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 31 idoall.org test1 }
g)这时我们停止掉m2机器上的sink(ctrl+c),再次输出测试数据:
> echo"idoall.org test2 failover" | nc localhost 5140
h)可以在m1的sink窗口,看到读取到了刚才发送的两条测试数据:
14/08/1015:02:46 INFO ipc.NettyServer: Connection to /192.168.1.51:47036 disconnected.
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 =>/192.168.1.50:5555] OPEN
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 =>/192.168.1.50:5555] BOUND: /192.168.1.50:5555
14/08/1015:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 =>/192.168.1.50:5555] CONNECTED: /192.168.1.51:47048
14/08/1015:07:56 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 31 idoall.org test1 }
14/08/1015:07:56 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 32 idoall.org test2 }
i)我们再在m2的sink窗口中,启动sink:
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf-n a1 -Dflume.root.logger=INFO,console
j)输入两批测试数据:
> echo"idoall.org test3 failover" | nc localhost 5140 && echo"idoall.org test4 failover" | nc localhost 5140
k)在m2的sink窗口,我们可以看到以下信息,因为优先级的关系,log消息会再次落到m2上:
14/08/1015:09:47 INFO node.Application: Starting Sink k1
14/08/1015:09:47 INFO node.Application: Starting Source r1
14/08/1015:09:47 INFO source.AvroSource: Starting Avro source r1: { bindAddress:0.0.0.0, port: 5555 }...
14/08/1015:09:47 INFO instrumentation.MonitoredCounterGroup: Monitored counter groupfor type: SOURCE, name: r1: Successfully registered new MBean.
14/08/1015:09:47 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE,name: r1 started
14/08/1015:09:47 INFO source.AvroSource: Avro source r1 started.
14/08/1015:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 =>/192.168.1.51:5555] OPEN
14/08/1015:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 =>/192.168.1.51:5555] BOUND: /192.168.1.51:5555
14/08/1015:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 =>/192.168.1.51:5555] CONNECTED: /192.168.1.51:48741
14/08/1015:09:57 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 32 idoall.org test2 }
14/08/1015:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 =>/192.168.1.51:5555] OPEN
14/08/1015:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 =>/192.168.1.51:5555] BOUND: /192.168.1.51:5555
14/08/1015:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 =>/192.168.1.51:5555] CONNECTED: /192.168.1.50:38166
14/08/1015:10:43 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 33 idoall.org test3 }
14/08/1015:10:43 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 34 idoall.org test4 }
案例11:Load balancing Sink Processor
load balance type和failover不同的地方是,load balance有两个配置,一个是轮询,一个是随机。两种情况下如果被选择的sink不可用,就会自动尝试发送到下一个可用的sink上面。
a)在m1创建Load_balancing_Sink_Processors配置文件
[Load_balancing_Sink_Processors.conf]
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1
#这个是配置Load balancing的关键,需要有一个sink group
a1.sinkgroups =g1
a1.sinkgroups.g1.sinks= k1 k2
a1.sinkgroups.g1.processor.type= load_balance
a1.sinkgroups.g1.processor.backoff= true
a1.sinkgroups.g1.processor.selector= round_robin
#Describe/configure the source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= avro
a1.sinks.k1.channel= c1
a1.sinks.k1.hostname= m1
a1.sinks.k1.port= 5555
a1.sinks.k2.type= avro
a1.sinks.k2.channel= c1
a1.sinks.k2.hostname= m2
a1.sinks.k2.port= 5555
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
b)在m1创建Load_balancing_Sink_Processors_avro配置文件
[Load_balancing_Sink_Processors_avro.conf]
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type= avro
a1.sources.r1.channels= c1
a1.sources.r1.bind= 0.0.0.0
a1.sources.r1.port= 5555
# Describe thesink
a1.sinks.k1.type= logger
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
c)将2个配置文件复制到m2上一份
d)打开4个窗口,在m1和m2上同时启动两个flume agent
>flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors_avro.conf-n a1 -Dflume.root.logger=INFO,console
>flume-ngagent -c . -f/home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors.conf -n a1-Dflume.root.logger=INFO,console
e)然后在m1或m2的任意一台机器上,测试产生log,一行一行输入,输入太快,容易落到一台机器上
> echo"idoall.org test1" | nc localhost 5140
>echo"idoall.org test2" | nc localhost 5140
>echo"idoall.org test3" | nc localhost 5140
>echo"idoall.org test4" | nc localhost 5140
f)在m1的sink窗口,可以看到以下信息:
14/08/10 15:35:29INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 32 idoall.org test2 }
14/08/1015:35:33 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid,Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 34 idoall.orgtest4 }
g)在m2的sink窗口,可以看到以下信息:
14/08/1015:35:27 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 31 idoall.org test1 }
14/08/1015:35:29 INFO sink.LoggerSink: Event: { headers:{Severity=0,flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 2074 65 73 74 33 idoall.org test3 }
说明轮询模式起到了作用。
案例12:Hbase sink
a)在测试之前,请先参考《ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1分布式环境部署》将hbase启动
b)然后将以下文件复制到flume中:
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/protobuf-java-2.5.0.jar/home/hadoop/flume-1.5.0-bin/lib
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-client-0.96.2-hadoop2.jar/home/hadoop/flume-1.5.0-bin/lib
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-common-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-protocol-0.96.2-hadoop2.jar/home/hadoop/flume-1.5.0-bin/lib
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-server-0.96.2-hadoop2.jar/home/hadoop/flume-1.5.0-bin/lib
cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-hadoop2-compat-0.96.2-hadoop2.jar/home/hadoop/flume-1.5.0-bin/lib
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-hadoop-compat-0.96.2-hadoop2.jar/home/hadoop/flume-1.5.0-bin/lib@@@
cp/home/hadoop/hbase-0.96.2-hadoop2/lib/htrace-core-2.04.jar/home/hadoop/flume-1.5.0-bin/lib
c)确保test_idoall_org表在hbase中已经存在
d)在m1创建hbase_simple配置文件
root@m1:/home/hadoop#vi /home/hadoop/flume-1.5.0-bin/conf/hbase_simple.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configurethe source
a1.sources.r1.type= syslogtcp
a1.sources.r1.port= 5140
a1.sources.r1.host= localhost
a1.sources.r1.channels= c1
# Describe thesink
a1.sinks.k1.type= logger
a1.sinks.k1.type= hbase
a1.sinks.k1.table= test_idoall_org
a1.sinks.k1.columnFamily= name
a1.sinks.k1.column= idoall
a1.sinks.k1.serializer= org.apache.flume.sink.hbase.RegexHbaseEventSerializer
a1.sinks.k1.channel= memoryChannel
# Use a channelwhich buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bind thesource and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
e)启动flume agent
/home/hadoop/flume-1.5.0-bin/bin/flume-ngagent -c . -f /home/hadoop/flume-1.5.0-bin/conf/hbase_simple.conf -n a1-Dflume.root.logger=INFO,console
f)测试产生syslog
root@m1:/home/hadoop#echo "hello idoall.org from flume" | nc localhost 5140
g)这时登录到hbase中,可以发现新数据已经插入
root@m1:/home/hadoop#/home/hadoop/hbase-0.96.2-hadoop2/bin/hbase shell
2014-08-1016:09:48,984 INFO [main] Configuration.deprecation:hadoop.native.lib is deprecated. Instead, use io.native.lib.available
HBase Shell;enter 'help<RETURN>' for list of supported commands.
Type"exit<RETURN>" to leave the HBase Shell
Version0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014
hbase(main):001:0>list
TABLE
SLF4J: Classpath contains multiple SLF4J bindings.
SLF4J: Foundbinding in[jar:file:/home/hadoop/hbase-0.96.2-hadoop2/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Foundbinding in[jar:file:/home/hadoop/hadoop-2.2.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Seehttp://www.slf4j.org/codes.html#multiple_bindings for an explanation.
hbase2hive_idoall
hive2hbase_idoall
test_idoall_org
3 row(s) in2.6880 seconds
=>["hbase2hive_idoall", "hive2hbase_idoall","test_idoall_org"]
hbase(main):002:0>scan "test_idoall_org"
ROW COLUMN+CELL
10086 column=name:idoall,timestamp=1406424831473, value=idoallvalue
1 row(s) in0.0550 seconds
hbase(main):003:0>scan "test_idoall_org"
ROW COLUMN+CELL
10086 column=name:idoall,timestamp=1406424831473, value=idoallvalue
1407658495588-XbQCOZrKK8-0 column=name:payload, timestamp=1407658498203,value=hello idoall.org from flume
2 row(s) in0.0200 seconds
hbase(main):004:0>quit
复制代码
经过这么多flume的例子测试,如果你全部做完后,会发现flume的功能真的很强大,可以进行各种搭配来完成你想要的工作,俗话说师傅领进门,修行在个人,如何能够结合你的产品业务,将flume更好的应用起来,快去动手实践吧。