Flume-ng 例子

Flume是一个分布式、可靠、高可用的海量日志聚合系统,支持在系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据的简单处理,并写到各种数据接收方。

其中相关组件如下:

Agent:使用JVM 运行Flume。每台机器运行一个agent,但是可以在一个agent 中包含多个 sources 和 sinks。
Source:从Client 端收集数据,传递给 Channel。
Sink:从Channel 收集数据,运行在一个独立线程。
Channel:连接 sources 和 sinks ,这个有点像一个队列。
Events:可以是日志记录、 avro 对象等。

Flume以agent为最小的独立运行单位。一个agent就是一个JVM。单agent由Source、Sink和Channel三大组件构成。

事件是Flume的基本数据单位,它携带日志数据(字节数组形式)并且携带有头信息。


本文用到的 apache-flume-1.5.0-bin.tar.gz


关于Flume 的安装可以参考:http://blog.csdn.net/u012689336/article/details/52687956


Flume Source:完成对日志数据的收集,分成transtion 和event 打入到channel之中。

Flume提供了各种 source 的实现,包括 Avro Source、Exce Source、Spooling Directory Source、NetCat Source、Syslog Source、Syslog TCP Source、Syslog UDP Source、HTTP Source、HDFS Source,etc。

1、Avro Source

支持Avro协议(实际上是Avro RPC),内置支持

在conf 目录下面

[sparkadmin@hadoop4 conf]$ touch avro_case.conf 

[sparkadmin@hadoop4 conf]$ vim avro_case.conf

#agent
a1.sources= r1
a1.sinks= k1
a1.channels= c1


#source
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = hadoop4
a1.sources.r1.port = 55555


#sink
a1.sinks.k1.type= logger
a1.sinks.k1.channel= c1


#channel
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100


启动:控制台输出

[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/avro_case.conf -n a1 -Dflume.root.logger=INFO,console


然后在另一个机器hadoop3 上面运行发送文件

[sparkadmin@hadoop3 flume]$ bin/flume-ng avro-client -c conf -H hadoop4 -p 55555 -F ./README


发送事件成功后,文件名不会被修改。

然后到 hadoop4 端控制台上查看接收的数据。将显示出 hadoop3 上面文件里面的内容。


2、Exec Source

基于Unix 的command 在标准输出上生产数据

Exec Source 以运行Linux 命令的方式,持续的输出最新的数据,如tail -F 文件名,在这种方式下,文件名必须是指定的。

Exec Source 虽然支持实时数据,但是在flume 不运行和脚本错误时,会丢数据,也不支持断点续传功能。因为没有记录上次文件读到的位置,从而没办法知道,下次再读时,从什么地方开始读。

[sparkadmin@hadoop4 conf]$ vim exec_case.conf

#agent
a1.sources= r1
a1.sinks= k1
a1.channels= c1


#source                       
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /cloud/test/tmp.log
a1.sources.r1.channels = c1


#sink
a1.sinks.k1.type= logger
a1.sinks.k1.channel= c1


#channel
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100


这里用tail –F 命令一直都在日志的尾部。

[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/exec_case.conf -n a1 -Dflume.root.logger=INFO,console


控制台上面将显示出tmp.log 里面的内容

echo "looklook" >>  tmp.log ,会发现终端立马会输出数据,即在文件末尾添加内容都将输出出来。



3、Spool Source

监测配置的目录下新增的文件,并将文件中的数据读取出来。其中,Spool Source有2个注意地方,第一个是拷贝到spool 目录下的文件不可以再打开编辑,第二个是spool 目录下不可包含相应的子目录。这个主要用途作为对日志的准实时监控。

Flume 在传完文件之后,将会修改文件的后缀,变为.COMPLETED

[sparkadmin@hadoop4 conf]$ vim spool_case.conf

#agent                             
a1.sources = r1
a1.sinks = k1
a1.channels = c1


#source                        
a1.sources.r1.type =spooldir
a1.sources.r1.spoolDir =/cloud/test
a1.sources.r1.fileHeader= true
a1.sources.r1.channels =c1


#sink              
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1


#channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100


[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/spool_case.conf -n a1 -Dflume.root.logger=INFO,console


控制台将每个文件里面的数据都显示出来了。

我们查看监控日志目录/cloud/test

[sparkadmin@hadoop4 test]$ ll
-rw-rw-r-- 1 sparkadmin sparkadmin  47 Oct 12 13:46 tmp.log.COMPLETED



4、NetCat Source

监控某个端口,将流经端口的每一个文本行数据作为Event输入

[sparkadmin@hadoop4 conf]$ vim netcat_case.conf

#agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1


#source
a1.sources.r1.type = netcat
a1.sources.r1.bind = hadoop4
a1.sources.r1.port = 44212
a1.sources.r1.channels = c1


#sink
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1


#channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100


[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/netcat_case.conf --n a1 -Dflume.root.logger=INFO,console


利用telnet来直接访问且发送数据

[sparkadmin@hadoop3 ~]$ telnet hadoop4 44212
Trying 172.28.18.237...
Connected to hadoop4.
Escape character is '^]'.
hello world
OK


在hadoop4 的控制台上就会输出 hello world。



5、Syslog Sources

读取syslog 数据,产生Event,支持UDP和TCP两种协议

这个Source分成三类 Syslog TCPSource、Multiport Syslog TCPSource(多端口)与 Syslog UDPSource。

其中 TCP Source 为每一个用回车(\ n)来分隔的字符串创建一个新的事件。而UDP Source将整个消息作为一个单一的事件。


5.1、Syslog TCPSource

这个只能监听一个端口

[sparkadmin@hadoop4 conf]$ vim syslog_case.conf

#agent                             
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#source                        
a1.sources.r1.type =syslogtcp
a1.sources.r1.port =50000
a1.sources.r1.host =hadoop4
a1.sources.r1.channels =c1

#sink              
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1 

#channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100


这里我们设置的侦听端口为hadoop4 50000

[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/syslog_case.conf --n a1 -Dflume.root.logger=INFO,console


启动成功后,打开另一个终端输入,往侦听端口送数据
[sparkadmin@hadoop4 ~]$ echo "hello world" | nc hadoop4 50000

在hadoop3 上面也可以运行这个,照样显示出数据。

[sparkadmin@hadoop3 ~]$ echo "hello world" | nc hadoop4 50000

将会在 hadoop4 控制台上显示出 hello world。


5.2、Multiport Syslog TCP Source

这是一个更新,更快,支持多端口版本的SyslogTCP Source。他不仅仅监控一个端口,还可以监控多个端口。

[sparkadmin@hadoop4 conf]$ vim multiport_syslog_case.conf

#agent                            
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#source                       
a1.sources.r1.type = multiport_syslogtcp
a1.sources.r1.ports = 50000 60000
a1.sources.r1.host = hadoop4
a1.sources.r1.channels = c1

#sink             
a1.sinks.k1.type= logger
a1.sinks.k1.channel = c1 

#channel
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100


这里我们监听 hadoop4 的2个端口50000与60000

[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/multiport_syslog_case.conf --n a1 -Dflume.root.logger=INFO,console


启动成功后,打开另一个终端输入,往监听端口送数据

[sparkadmin@hadoop4 ~]$ echo "hello world" | nc hadoop4 50000
[sparkadmin@hadoop4 ~]$ echo "hello world" | nc hadoop4 60000


在 hadoop4 的控制台上都会收到来自50000 和 60000 端口的数据。


5.3、Syslog UDP Source

其实就是与TCP不同的协议而已,只是配置相同而已。

[sparkadmin@hadoop4 conf]$ vim udp_syslog_case.conf

#agent                            
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#source                       
a1.sources.r1.type = syslogudp
a1.sources.r1.port = 50000
a1.sources.r1.host = hadoop4
a1.sources.r1.channels = c1

#sink             
a1.sinks.k1.type= logger
a1.sinks.k1.channel = c1 

#channel
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100


[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/udp_syslog_case.conf --n a1 -Dflume.root.logger=INFO,console


启动成功后,打开另一个终端输入,往侦听端口送数据
[sparkadmin@hadoop4 ~]$ echo "hello world" | nc -u hadoop4 50000

在 hadoop4 的控制台上都会收到来自50000端口的数据。



6、HTTP Source

HTTP Source是HTTP POST和GET来发送事件数据的,支持JSON、BLOB表示形式

[sparkadmin@hadoop4 conf]$ vim http_case.conf

#agent                            
a1.sources= r1
a1.sinks= k1
a1.channels= c1

#source                       
a1.sources.r1.type= http
a1.sources.r1.port= 50000
a1.sources.r1.channels= c1

#sink             
a1.sinks.k1.type= logger
a1.sinks.k1.channel = c1 

#channel
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100


[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/http_case.conf -n a1 -Dflume.root.logger=INFO,console


启动成功后
#我们用生成JSON 格式的POSTrequest发数据
[sparkadmin@hadoop4 ~]$ curl -X POST -d '[{"headers" :{"flume" : "flume is good","sources" : "source of type"},"body" : "hello flume"}]' hadoop4:50000

#在启动的终端查看console输出

2016-10-12 15:35:43,133 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{flume=flume is good, sources=source of type} body: 68 65 6C 6C 6F 20 66 6C 75 6D 65                hello flume }


7、Sequence Generator Source

不断生成从0开始的数字,主要用于测试

[sparkadmin@hadoop4 conf]$ vim sequence_case.conf

a1.sources=s1
a1.sinks=k1
a1.channels=c1
   
a1.sources.s1.channels=c1
a1.sinks.k1.channel=c1
   
a1.sources.s1.type=seq
   
a1.sinks.k1.type=logger
   
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100


启动:

[sparkadmin@hadoop4 flume]$ bin/flume-ng agent -c conf -f conf/aa.conf -n a1 -Dflume.root.logger=INFO,console


控制台上面将显示

2016-10-12 15:58:01,607 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 35 32 33                                        0 }
2016-10-12 15:58:01,608 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 35 32 34                                        1 }


8、JMS Source

官网JMSsource 测试的是 ActiveMQ

从JMS系统(消息、主题)中读取数据,ActiveMQ已经测试过


9、Thrift Source

支持Thrift协议,内置支持



你可能感兴趣的:(Flume)