0628 Flume教程

Flume

1. Flume解决的问题

 

2. Flume概念

FlumeCloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。

3. Flume的工作机制

 

4. Flume组成架构

4.1. Agent

Agent是一个JVM进程,它以事件的形式将数据从源头送至目的,是Flume数据传输的基本单元。

Agent主要有3个部分组成,SourceChannelSink

4.2. Source

Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avrothriftexecjmsspooling directorynetcatsequence generatorsysloghttplegacy

4.3. Channel

Channel是位于SourceSink之间的缓冲区。因此,Channel允许SourceSink运作在不同的速率上。Channel是线程安全的,可以同时处理几个Source的写入操作和几个Sink的读取操作。

Flume自带两种ChannelMemory ChannelFile Channel

Memory Channel是内存中的队列。Memory Channel在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么Memory Channel就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。

File Channel将所有事件写到磁盘。因此在程序关闭或机器宕机的情况下不会丢失数据。

4.4. Sink

Sink不断地轮询Channel中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent

Sink是完全事务性的。在从Channel批量删除数据之前,每个SinkChannel启动一个事务。批量事件一旦成功写出到存储系统或下一个Flume AgentSink就利用Channel提交事务。事务一旦被提交,该Channel从自己的内部缓冲区删除事件。

Sink组件目的地包括hdfsloggeravrothriftipcfilenullHBasesolr、自定义。

4.5. Event

传输单元,Flume数据传输的基本单元,以事件的形式将数据从源头送至目的地。

5. Flume的安装部署

5.1. Flume下载地址

1Flume官网地址

http://flume.apache.org/

2)文档查看地址

http://flume.apache.org/FlumeUserGuide.html

3)下载地址

http://archive.apache.org/dist/flume/

5.2. 安装部署

1)将apache-flume-1.7.0-bin.tar.gz上传到linux/opt/software目录下

2)解压apache-flume-1.7.0-bin.tar.gz/opt/module/目录下

[hadoop01@hadoop01 software]$ tar -zxf apache-flume-1.7.0-bin.tar.gz -C /opt/module/

3)修改apache-flume-1.7.0-bin的名称为flume

[hadoop01 @hadoop01 module]$ mv apache-flume-1.7.0-bin flume

4) 将flume/conf下的flume-env.sh.template文件修改为flume-env.sh,并配置flume-env.sh文件

[hadoop01@ hadoop01 conf]$ mv flume-env.sh.template flume-env.sh

[hadoop01@ hadoop01 conf]$ vi flume-env.sh

export JAVA_HOME=/opt/module/jdk1.8.0_144

6. Flume实践案例

6.1. 采集目录中的新文件到HDFS

采集需求:使用flume监听整个目录的文件

6.1.1. Flume要想将数据输出到HDFS,必须持有Hadoop相关jar

将commons-configuration-1.6.jarhadoop-auth-2.7.2.jarhadoop-common-2.7.2.jarhadoop-hdfs-2.7.2.jarcommons-io-2.4.jarhtrace-core-3.1.0-incubating.jar拷贝到/opt/module/flume/lib文件夹下。

尖叫提示:标红的jar1.99版本flume必须引用的jar。其他版本可以不引用。

6.1.2. 创建flume-file-hdfs.conf文件

创建文件

[hadoop02@hadoop02 flume]$ touch dir-hdfs.conf

#定义三大组件的名称

ag1.sources = source1

ag1.sinks = sink1

ag1.channels = channel1

 

# 配置source组件

ag1.sources.source1.type = spooldir

ag1.sources.source1.spoolDir = /root/log/

ag1.sources.source1.fileSuffix=.FINISHED

#ag1.sources.source1.deserializer.maxLineLength=5120

 

# 配置sink组件

ag1.sinks.sink1.type = hdfs

ag1.sinks.sink1.hdfs.path =hdfs://hadoop01:9000/access_log/%y-%m-%d/%H-%M

ag1.sinks.sink1.hdfs.filePrefix = app_log

ag1.sinks.sink1.hdfs.fileSuffix = .log

ag1.sinks.sink1.hdfs.batchSize= 100

ag1.sinks.sink1.hdfs.fileType = DataStream

ag1.sinks.sink1.hdfs.writeFormat =Text

 

## roll:滚动切换:控制写文件的切换规则

## 按文件体积(字节)来切

ag1.sinks.sink1.hdfs.rollSize = 512000

## event条数切     

ag1.sinks.sink1.hdfs.rollCount = 1000000  

## 按时间间隔切换文件

ag1.sinks.sink1.hdfs.rollInterval = 60    

 

## 控制生成目录的规则

ag1.sinks.sink1.hdfs.round = true

ag1.sinks.sink1.hdfs.roundValue = 10

ag1.sinks.sink1.hdfs.roundUnit = minute

 

ag1.sinks.sink1.hdfs.useLocalTimeStamp = true

 

# channel组件配置

ag1.channels.channel1.type = memory

## event条数

ag1.channels.channel1.capacity = 500000   

##flume事务控制所需要的缓存容量600event

ag1.channels.channel1.transactionCapacity = 600

  

# 绑定sourcechannelsink之间的连接

ag1.sources.source1.channels = channel1

ag1.sinks.sink1.channel = channel1

 

6.1.3. 启动Flume采集命令(测试阶段下使用)

日志打印到控制台启动命令:

[root@hadoop01 flume]# bin/flume-ng agent -c conf -f dir-hdfs.conf -n ag1 -Dflume.root.logger=INFO,console

重要说明: 在使用Spooling Directory Source

1) 不要在监控目录中创建并持续修改文件

2) 采集完成的文件会以.FINISHED结尾

3) 被监控文件夹每600毫秒扫描一次文件变动

6.1.4. 启动Flume采集命令(生产环境下使用)

在生产环境中,启动flume,应该把flume启动在后台

[root@hadoop01 flume]# nohup bin/flume-ng agent -c conf -f dir-hdfs.conf -n ag1 1>/dev/null 2>&1 &

 

6.2. 采集文件新增内容到HDFS

采集需求:比如业务系统使用log4j生成日志,日志内容不断添加,需要把追加的日志实时采集到hdfs存储.

6.2.1. 需求分析

根据需求首先定义以下3大要素:

采集源: 即是source--监控文件内容的更新: exec 'tail -F file'

下沉目标: 即是sink --HDFS文件系统 : hdfs sink

sourcesink的传递通道-- channel, 可用file channel 也可以用内存channel.

 

6.2.2. 实现

#定义三大组件的名称

ag1.sources = source1

ag1.sinks = sink1

ag1.channels = channel1

 

# 配置source组件

ag1.sources.source1.type = exec

ag1.sources.source1.command = tail -F /root/log/access.log

 

# 配置sink组件

ag1.sinks.sink1.type = hdfs

ag1.sinks.sink1.hdfs.path =hdfs://hadoop01:9000/access_log/%y-%m-%d

ag1.sinks.sink1.hdfs.filePrefix = app_log

ag1.sinks.sink1.hdfs.fileSuffix = .log

ag1.sinks.sink1.hdfs.batchSize= 100

ag1.sinks.sink1.hdfs.fileType = DataStream

ag1.sinks.sink1.hdfs.writeFormat =Text

 

## roll:滚动切换:控制写文件的切换规则

## 按文件体积(字节)来切   

ag1.sinks.sink1.hdfs.rollSize = 512000

## event条数切

ag1.sinks.sink1.hdfs.rollCount = 1000000

## 按时间间隔切换文件

ag1.sinks.sink1.hdfs.rollInterval = 60

 

## 控制生成目录的规则

ag1.sinks.sink1.hdfs.round = true

ag1.sinks.sink1.hdfs.roundValue = 10

ag1.sinks.sink1.hdfs.roundUnit = minute

ag1.sinks.sink1.hdfs.useLocalTimeStamp = true

 

# channel组件配置

ag1.channels.channel1.type = memory

## event条数

ag1.channels.channel1.capacity = 500000

##flume事务控制所需要的缓存容量600event

ag1.channels.channel1.transactionCapacity = 600

 

# 绑定sourcechannelsink之间的连接

ag1.sources.source1.channels = channel1

ag1.sinks.sink1.channel = channel1

6.2.3. 写入数据测试

 

6.3. 多级agent串联

采集需求:flume-1监控hadoop01日志文件access.logflume-3监控hadoop03日志文件access.logflume-1flume-3将数据发送给flume-3flume3将最终数据写入到HDFS

 

6.3.1. 配置hadoop02监听服务端

1) 配置文件avro-hdfs.conf

[root@hadoop02 flume]# vim avro-hdfs.conf

#定义三大组件的名称

ag1.sources = source1

ag1.sinks = sink1

ag1.channels = channel1

 

# 配置source组件,sourceavro组件是一个接收者的服务

ag1.sources.source1.type = avro

ag1.sources.source1.bind = hadoop02

ag1.sources.source1.port = 4141

 

# 配置sink组件

ag1.sinks.sink1.type = hdfs

ag1.sinks.sink1.hdfs.path =hdfs://hadoop01:9000/flume/taildata/%y-%m-%d/

ag1.sinks.sink1.hdfs.filePrefix = tail-

ag1.sinks.sink1.hdfs.round = true

ag1.sinks.sink1.hdfs.roundValue = 24

ag1.sinks.sink1.hdfs.roundUnit = hour

ag1.sinks.sink1.hdfs.rollInterval = 0

ag1.sinks.sink1.hdfs.rollSize = 0

ag1.sinks.sink1.hdfs.rollCount = 50

ag1.sinks.sink1.hdfs.batchSize = 10

ag1.sinks.sink1.hdfs.useLocalTimeStamp = true

#生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本

ag1.sinks.sink1.hdfs.fileType = DataStream

 

# channel组件配置

ag1.channels.channel1.type = memory

## event条数

ag1.channels.channel1.capacity = 1000

##flume事务控制所需要的缓存容量100event

ag1.channels.channel1.transactionCapacity = 100

 

# 绑定sourcechannelsink之间的连接

ag1.sources.source1.channels = channel1

ag1.sinks.sink1.channel = channel1

 

2) 启动

[root@hadoop02 flume]# bin/flume-ng agent -c conf -f avro-hdfs.conf -n ag1 -Dflume.root.logger=INFO,console  

3) 查看是否启动监听

[root@hadoop02 ~]# netstat -nltp

4) 查看flume进程详情

[root@hadoop02 ~]# jps -m

6.3.2. 在其它节点配置监听客户端

[root@hadoop01 flume]# vim tail-avro.conf

#定义三大组件的名称

ag1.sources = source1

ag1.sinks = sink1

ag1.channels = channel1

 

# 配置source组件

ag1.sources.source1.type = exec

ag1.sources.source1.command = tail -F /root/log/access.log

 

# 配置sink组件

ag1.sinks.sink1.type = avro

ag1.sinks.sink1.hostname = hadoop02

ag1.sinks.sink1.port = 4141

ag1.sinks.sink1.batch-size = 2

 

# channel组件配置

ag1.channels.channel1.type = memory

## event条数

ag1.channels.channel1.capacity = 1000

##flume事务控制所需要的缓存容量100event

ag1.channels.channel1.transactionCapacity = 100

 

# 绑定sourcechannelsink之间的连接

ag1.sources.source1.channels = channel1

ag1.sinks.sink1.channel = channel1

 

启动进行测试

[root@hadoop01 flume]#  bin/flume-ng agent -c conf -f tail-avro.conf -n ag1 -Dflume.root.logger=INFO,console

发送数据测试

[root@hadoop01 flume]# while true; do echo `date` >> access.log; sleep 0.1; done

 

posted @   linzm14  阅读(244)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示