Flume的安装与使用详解

Flume的简单介绍
Flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。
Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到的数据输出到HDFS、hbase、hive、kafka等众多外部存储系统中
Flume的运行机制
1、Flume分布式系统中最核心的角色是agent,flume采集系统就是由一个个agent所连接起来形成
2、每一个agent相当于一个数据传递员Source 到 Channel 到 Sink之间传递数据的形式是Event事件;Event事件是一个数据流单元。

内部有三个组件:
a)Source:采集源,用于跟数据源对接,以获取数据
b)Sink:下沉地,采集数据的传送目的,用于往下一级agent传递数据或者往最终存储系统传递数据
c)Channel:angent内部的数据传输通道,用于从source将数据传递到sink
单个agent采集数据
这里写图片描述
多个agent直接串联采集数据
这里写图片描述

Flume的安装与使用
安装Flume之前确保安装了hadoop,假设是安装了hadoop的前提下,介绍Flume的安装,其实解压下就行了。
我这里使用的是apache-flume-1.6.0-bin.tar.gz,上传到linux,解压到指定包即可,我这里解压到了apps这个包,里面放了我解压的hadoo、hive和zookeeper,安装就完了。
注:其实在conf目录里面需要配置JAVA_HOME,但是我没配置使用也没问题的。如果出现问题了再配置看。

Flume的使用(一)
这里打算做的是,接收网络传输的数据。也就是flume(安装在mini1)的作用是,然后在mini2这台机器上,发送数据,mini1上能采集到,可以下沉到hdfs(为了方便,这里暂时打印在控制台)
注:为了方便我这里就在mini1这条机器打开两个窗口来进行发送和采集数据了。
进入到flume的conf目录下,创建文件,进行配置

[root@mini1 ~]# cd apps/apache-flume-1.6.0-bin/conf/
[root@mini1 conf]# ll
总用量 28
-rw-r--r--. 1  501 games 1661 59 2015 flume-conf.properties.template
-rw-r--r--. 1  501 games 1110 59 2015 flume-env.ps1.template
-rw-r--r--. 1  501 games 1214 59 2015 flume-env.sh.template
-rw-r--r--. 1  501 games 3107 59 2015 log4j.properties
-rw-r--r--. 1 root root   487 1019 14:34 netcat-logger.conf
-rw-r--r--. 1 root root   507 1019 01:57 spool-logger.conf
-rw-r--r--. 1 root root  1271 1019 15:11 tail-hdfs.conf
[root@mini1 conf]# vi netcat-logger.conf 
# example.conf: A single-node Flume configuration

# Name the components on this agent
#给那三个组件取个名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
#类型, 从网络端口接收数据,本机mini1, type=spoolDir采集目录源,目录里有就采
a1.sources.r1.type = netcat
a1.sources.r1.bind = mini1
a1.sources.r1.port = 44444

# Describe the sink 日志下沉到log4j,打印在屏幕上
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
#下沉的时候是一批一批的, 下沉的时候是一个个event
Channel参数解释:
#capacity:默认该通道中最大的可以存储的event数量 1000条数据(1000个event,source拿到的数据是封装成event事件的)
#trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

配置好了就可以启动了

[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console
Warning: JAVA_HOME is not set!
...
2017-10-20 05:00:13,317 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:173)] Starting Sink k1
2017-10-20 05:00:13,318 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:184)] Starting Source r1
2017-10-20 05:00:13,320 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:150)] Source starting
2017-10-20 05:00:13,350 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:164)] Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/192.168.25.127:44444]

在mini1上重开一个窗口(或者其它机器),发送数据进行测试

[root@mini1 ~]# telnet mini1 44444
Trying 192.168.25.127...
Connected to mini1.
Escape character is '^]'.
jinbingmin
OK
haha
OK
oyasumi
OK

发送了三句话。
注:如果没有telnet命令,做法如下

rpm -qa telnet-server 查看有没有安装,没有输出的话,那么执行
yum install telnet-server  
rpm -qa telnet 来查看telnet-server 安装包有没有安装,如果没有输出,那么执行安装
yum install telnet
退出的话使用ctrl+],接着quit

再次查看服务端有没有采集到

2017-10-20 05:00:59,699 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 6A 69 6E 62 69 6E 67 6D 69 6E 0D                jinbingmin. }
2017-10-20 05:01:14,704 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 68 61 68 61 0D                                  haha. }
2017-10-20 05:01:19,421 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{} body: 6F 79 61 73 75 6D 69 0D      oyasumi. }

发现已经采集到了打印到了控制台

Flume的使用(二)
这次要做的是监听目录,往目录里面放文件看能否被监听到
步骤基本跟上面一样,注意是配置文件修改了(主要是改采集源配置),测试方法不一样。
conf下添加配置文件spooldir-hdfs.conf

[root@mini1 conf]# vi spool-logger.conf 
#Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
#监听目录,spoolDir指定目录, fileHeader要不要给文件加前缀名
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /root/flumespool
a1.sources.r1.fileHeader = true
# 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 = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动服务

[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent -c ./conf -f ./conf/spool-logger.conf -n a1 -Dflume.root.logger=INFO,console
2017-10-20 05:23:59,572 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:173)] Starting Sink k1
2017-10-20 05:23:59,572 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:184)] Starting Source r1
2017-10-20 05:23:59,573 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.SpoolDirectorySource.start(SpoolDirectorySource.java:78)] SpoolDirectorySource source starting with directory: /root/flumespool
2017-10-20 05:23:59,619 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.instrumentation.MonitoredCounterGroup.register(MonitoredCounterGroup.java:120)] Monitored counter group for type: SOURCE, name: r1: Successfully registered new MBean.
2017-10-20 05:23:59,619 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.instrumentation.MonitoredCounterGroup.start(MonitoredCounterGroup.java:96)] Component type: SOURCE, name: r1 started

测试,mini机器上,创建目录/root/flumepool。
一定文件到该目录下,再查看服务端

[root@mini1 ~]#mkdir flumepool
[root@mini1 ~]#vi a.txt 
1,a
2,b
3,c
4,d
7,y
8,u
[root@mini1 ~]#mv a.txt flumepool

服务端查看输出日志,文件中每行数据这里会输出一行

2017-10-20 05:24:53,616 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 31 2C 61                                        1,a }
2017-10-20 05:24:53,618 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 32 2C 62                                        2,b }
2017-10-20 05:24:53,619 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 33 2C 63                                        3,c }
2017-10-20 05:24:53,620 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 34 2C 64                                        4,d }
2017-10-20 05:24:53,624 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 37 2C 79                                        7,y }
2017-10-20 05:24:53,626 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:94)] Event: { headers:{file=/root/flumespool/a.txt} body: 38 2C 75                                        8,u }

查看文件名成 了a.txt.COMPLETED

[root@mini1 flumespool]# ll
总用量 8
-rw-r--r--. 1 root root 24 1020 05:30 a.txt.COMPLETED

Flume的使用(三)
采集数据到hdfs

这里要添加的配置文件里面的采集源和下沉地就都有变化了。

[root@mini1 conf]# vi tail-hdfs.conf 
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#exec 指的是命令
# Describe/configure the source
a1.sources.r1.type = exec
#F根据文件名追中, f根据文件的nodeid追中
a1.sources.r1.command = tail -F /root/log/test.log
a1.sources.r1.channels = c1
# Describe the sink
#下沉目标
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
#指定目录, flum帮做目的替换
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
#文件的命名, 前缀
a1.sinks.k1.hdfs.filePrefix = events-
#10 分钟就改目录
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
#文件滚动之前的等待时间
a1.sinks.k1.hdfs.rollInterval = 3
#文件滚动的大小限制(bytes)
a1.sinks.k1.hdfs.rollSize = 500
#写入多少个event数据后滚动文件(事件个数)
a1.sinks.k1.hdfs.rollCount = 20
#5个事件就往里面写入
a1.sinks.k1.hdfs.batchSize = 5
#用本地时间格式化目录
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#下沉后, 生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

现在就创建目录/root/log
往文件test.log里面不断写数据,去页面查看是不是输出到了hdfs。

[root@mini1 ~]# mkdir log
[root@mini1 ~]# while true
> do
> echo 1111111111111 >> /root/log/test.log
> sleep 0.5
> done

换个窗口
[root@mini1 ~]# cd log/
[root@mini1 log]# ll
总用量 4
-rw-r--r--. 1 root root 938 1019 15:13 test.log
[root@mini1 log]# tail -F test.log 
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
不断增加

启动服务

[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1;

去页面查看
这里写图片描述
创建的目录格式就是我们指定的,该目录下会不断创建文件(每三秒),文件名前缀也是我们指定的。另外每个10分钟会重新创建个文件夹比如1530。

Flume的使用(四)
多个agent连接。这里就用两个为例
两台机器上都要安装flume,由于mini1安装了,那么只需要在mini2上安装flume了。
就是两个agent,其中一个的下沉地下沉的数据就被另一个的采集源所采集,然后这里为了简便就打印在控制台。
mini1机器,进入flume的conf目录
编辑文件

[root@mini1 apache-flume-1.6.0-bin]# vi avro-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 = exec
a1.sources.r1.command = tail -F /root/log/test.log

# Describe the sink
#绑定的不是本机, 是另外一台机器的服务地址, sink端的avro是一个发送端
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = mini2
a1.sinks.k1.port = 4141
a1.sinks.k1.batch-size = 2

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

在mini2上,进入flume的conf目录下

[root@mini2 apache-flume-1.6.0-bin]## vi tail-avro.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
#source中的avro组件是接收者服务, 绑定本机
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 in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动mini2

[root@mini2 apache-flume-1.6.0-bin]#bin/flume-ng agent -c conf -f conf/avro-logger.conf -n al -Dflume.root.logger=INFO,console

mini1上发送数据

[root@mini1 ~]# cd log/
[root@mini1 log]# ll
总用量 4
-rw-r--r--. 1 root root 938 1019 15:13 test.log
[root@mini1 log]# tail -F test.log 
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111
1111111111111

启动mini1

[root@mini1 apache-flume-1.6.0-bin]bin/flume-ng agent -c conf -f conf/tail-avro.conf -n al 

接着能看到mini2上显示连接而且一直打印数据”11111111”

这些配置在官网是先的很清楚的,很多种采集源和下沉地这里就只列这几个。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值