flume的安装与使用

  1. flume的概念与原理

flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。支持在日志系统中定制各类数据发送方,用于收集数据;

同时,Flume提供对数据进行简单处理,并写到各种数据接受方(比如文本、HDFS、Hbase等)的能力 。

 

flume的数据流由事件(Event)贯穿始终。事件是Flume的基本数据单位,它携带日志数据(字节数组形式)并且携带有头信息,

这些Event由Agent外部的Source生成,当Source捕获事件后会进行特定的格式化,然后Source会把事件推入(单个或多个)Channel中。你可以把Channel看作是一个缓冲区,它将保存事件直到Sink处理完该事件。Sink负责持久化日志或者把事件推向另一个Source。

flume的核心概念

Client:Client生产数据,运行在一个独立的线程。

Event: 一个数据单元,消息头和消息体组成。(Events可以是日志记录、 avro 对象等。)

Flow: Event从源点到达目的点的迁移的抽象。

Agent: 一个独立的Flume进程,包含组件Source、 Channel、 Sink。(Agent使用JVM 运行Flume。每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks。)

Source: 数据收集组件。(source从Client收集数据,传递给Channel)

Channel: 中转Event的一个临时存储,保存由Source组件传递过来的Event。(Channel连接 sources 和 sinks ,这个有点像一个队列。)

Sink: 从Channel中读取并移除Event, 将Event传递到FlowPipeline中的下一个Agent(如果有的话)(Sink从Channel收集数据,运行在一个独立线程。)

Flume 运行的核心是 Agent。Flume以agent为最小的独立运行单位。一个agent就是一个JVM。它是一个完整的数据收集工具,含有三个核心组件,分别是source、 channel、 sink。通过这些组件, Event 可以从一个地方流向另一个地方。Source是数据的收集端,负责将数据捕获后进行特殊的格式化,将数据封装到事件(event) 里,然后将事件推入Channel中。

Flume提供了很多内置的Source, 支持 Avro, log4j, syslog 和 http post(body为json格式)。可以让应用程序同已有的Source直接打交道,

如AvroSource,SyslogTcpSource。 如果内置的Source无法满足需要, Flume还支持自定义Source。

Channel是连接Source和Sink的组件,大家可以将它看做一个数据的缓冲区(数据队列),它可以将事件暂存到内存中也可以持久化到本地磁盘上,

直到Sink处理完该事件。介绍两个较为常用的Channel, MemoryChannel和FileChannel。

Sink从Channel中取出事件,然后将数据发到别处,可以向文件系统、数据库、 hadoop存数据, 也可以是其他agent的Source。在日志数据较少时,可以将数据存储在文件系统中,并且设定一定的时间间隔保存数据。

当我们需要对数据进行过滤时,除了我们在Source、 Channel和Sink进行代码修改之外, Flume为我们提供了拦截器,拦截器也是chain形式的。

拦截器的位置在Source和Channel之间,当我们为Source指定拦截器后,我们在拦截器中会得到event,根据需求我们可以对event进行保留还是抛弃,抛弃的数据不会进入Channel中。

Flume数据流

1)Flume 的核心是把数据从数据源收集过来,再送到目的地。为了保证输送一定成功,在送到目的地之前,会先缓存数据,

       待数据真正到达目的地后,

    删除自己缓存的数据。

1、 Flume 传输的数据的基本单位是 Event,如果是文本文件,通常是一行记录,这也是事务的基本单位。 Event 从 Source,流向 Channel,再到 Sink,本身为一个 byte 数组,并可携带 headers 信息。 Event 代表着一个数据流的最小完整单元,从外部数据源来,向外部的目的地去。

2、flume安装:

tar -zxvf xxxx.gz

配置jdk

mv flume-env.sh.template flume-env.sh

 vi  flume-env.sh

配置jdk :export JAVA_HOME=/home/hadoop/cluster/jdk1.8.0_11

3、flume使用

案例1:Avro 可以发送一个给定的文件给Flume,Avro 源使用AVRO RPC机制

(a)创建agent配置文件

监测网络中某一地址端口,若有文件会检测到,将其放入channel中,然后sink处理显示在控制台上

vi /home/hadoop/cluster/flume-1.7.0/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 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

(b)启动服务 flume agent a1

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

(c)创建指定文件

echo "hello world" > /home/hadoop/log.log

(d)使用avro-client发送文件

bin/flume-ng avro-client -c . -H localhost -p 4141 -F /home/hadoop/log.log

(e)在m1的控制台,可以看到以下信息,注意最后一行:  

hello world

案例2:Exec 执行一个给定的命令获得输出的源,如果要使用tail命令,必选使得file足够大才能看到输出内容

1.创建agent配置文件

vi /home/hadoop/cluster/flume-1.7.0/conf/exec_tail.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 /home/hadoop/exec.log

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

(b)启动服务flume agent a1

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

(c)生成足够多的内容在文件里

shell脚本

while true

do

 date >>  /home/hadoop/exec.log  

sleep 2

done

(d)在m1的控制台,可以看到以下信息:

Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 20 74 65 73 74 exec tail test }

Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 20 74 65 73 74 exec tail test }

案例3:Spool 监测配置的目录下新增的文件,并将文件中的数据读取出来。

需要注意两点:

1) 拷贝到spool目录下的文件不可以再打开编辑。

2) spool目录下不可包含相应的子目录

(a)创建agent配置文件

vi  /home/hadoop/cluster/flume-1.7.0/conf/spool.conf

 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/hadoop/spool

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

(b)启动服务flume agent a1

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

(c)追加文件到/home/hadoop/spool目录

echo "spool test1" > /home/hadoop/spool/spool_text.log

(d)在m1的控制台,可以看到以下相关信息:

Event: { headers:{file=/home/hadoop/spool/spool_text.log} body: 73 70 6F 6F 6C 20 74 65 73 74 31        spool test1 }

案例4:Spool 监测配置的目录下新增的文件传到dfs服务器(hdfs服务器需要时开启的状态才能接收到文件),并将文件中的数据读取出来。

vi  /home/hadoop/cluster/flume-1.7.0/conf/spool.conf

agent1.sources = source1

agent1.sinks = sink1

agent1.channels = channel1

agent1.sources.source1.type = spooldir

agent1.sources.source1.spoolDir = /home/hadoop/spool

agent1.sources.source1.fileHeader = false

agent1.sinks.sink1.type = hdfs

agent1.sinks.sink1.hdfs.path =hdfs://haitong-1:9000/flume-collection/%y-%m-%d

agent1.sinks.sink1.hdfs.filePrefix = access_log

agent1.sinks.sink1.hdfs.fileType = DataStream

agent1.sinks.sink1.hdfs.writeFormat =Text

agent1.sinks.sink1.hdfs.rollSize = 102400

agent1.sinks.sink1.hdfs.rollCount = 0

agent1.sinks.sink1.hdfs.rollInterval = 60

agent1.sinks.sink1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in memory

agent1.channels.channel1.type = memory

agent1.channels.channel1.keep-alive = 120

agent1.channels.channel1.capacity = 500000

agent1.channels.channel1.transactionCapacity = 600

# Bind the source and sink to the channel

agent1.sources.source1.channels = channel1

agent1.sinks.sink1.channel = channel1

(b)启动服务bin/flume-ng agent --conf conf --conf-file conf/spool.conf --name agent1 -Dflume.root.logger=INFO,console

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rose and war

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值