大数据 - Flume的安装及使用(三)

本文详细介绍Flume作为日志采集系统的使用方法,包括配置、启动及应用场景,如监听端口、监控日志、多路复用等,适用于大数据处理初学者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

是什么

  1. Flume是一个分布式、高可靠、高可用的日志采集系统,可实现从不同来源的系统中将大容量的日志数据采集、汇总和搬移到一个集中式的数据存储中。

  2. Flume是流式大数据中的数据采集组件,可用于接收日志文件或报文数据。通过配置文件就可以定义各种数据路由方式,基本上不用编码。flume-ng agent命令是其中最主要的命令。

  3. Flume的Source、Channel和Sink都支持被配置为一个或多个,以实现数据分层或叠加。Source中的选择器(selector)可实现数据路由;Source中的拦截器(interceptor)可实现数据过滤。

怎么用

  1. 官网下载最新版本的flume,并解压到/home/hadoop/bigdata/flume目录下
  2. 添加FLUME的相关配置到路径
	export FLUME_HOME=/home/hadoop/bigdata/flume
	export FLUME_CONF_DIR=$FLUME_HOME/conf
	export PATH=$FLUME_HOME/bin:$PATH
  1. 在/home/hadoop/bigdata/flume/conf下,把flume-env.sh.template重命名为flume-env.sh,并修改文件的如下两处设定值:
export JAVA_HOME=/usr/local/java/jdk1.8.0_261
FLUME_CLASSPATH="/home/hadoop/bigdata/flume/lib/flume-ng-core-1.9.0.jar"

事实上,笔者只修改了FLUME_CLASSPATH,因为我的hadoop用户.bashrc里面已经有了JAVA_HOME的相关定义了。

  1. 在控制台输入“flume-ng version”,可看到flume的具体版本

  2. 模拟具体的应用场景

场景一:监听端口并打印收到的数据日志到控制台

#demo.conf: 单节点的Flume配置示例
# 定义Agent的名字是agent1,依次定义它的sources/sinks/channels
agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1

# 定义source1的信源类型为netcat,将服务绑定到本机的40404端口
agent1.sources.source1.type = netcat
agent1.sources.source1.bind = 0.0.0.0
agent1.sources.source1.port = 40404

# 定义sink1的类型是logger,即Flume的控制台日志
agent1.sinks.sink1.type = logger

# 定义channel1的类型为把Event放到内存
agent1.channels.channel1.type = memory

# 将Source和Sink绑定到channel1上
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1

运行命令:

flume-ng agent --conf-file=/home/hadoop/demo.conf --name agent1

在这里插入图片描述

场景二:监控某nginx日志,同步到另外一个文件目录

# 定义3种组件的名称
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# 配置数据源
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /data/logs/nginx/xxx.xxxx.com-access.log

# 设定Sink类型为文件类型
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /home/hadoop/flume-log/
a1.sinks.k1.sink.pathManager.extension = dat
a1.sinks.k1.sink.pathManager.prefix = 931
a1.sinks.k1.sink.rollInterval = 30

# 设定Channel类型为内存类型
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 把Source和Sink绑定到Channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

场景三:Sink的多路复用,Flume的Agent支持多个Sink,Sink支持同时把Channel中的数据写入文件和HDFS
在这里插入图片描述

# 定义3种组件的名称
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1

# 配置Event源
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /data/logs/nginx/xxx.xxxx.com-access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

# 定义文件类型的k1 Sink
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /home/hadoop/flume-log/
a1.sinks.k1.sink.pathManager.extension = dat
a1.sinks.k1.sink.pathManager.prefix = 932
a1.sinks.k1.sink.rollInterval = 0

# 定义HDFS类型的k2 Sink
a1.sinks.k2.type = hdfs
a1.sinks.k2.hdfs.path=hdfs://localhost/flume-log/%y-%m-%d
a1.sinks.k2.hdfs.userLocalTimeStamp = false
a1.sinks.k2.hdfs.rollSize = 10240000
a1.sinks.k2.hdfs.fileType = DataStream
a1.sinks.k2.hdfs.batchSize = 100

# 设定Channel类型为内存类型
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 把Source和Sink绑定到Channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1

场景四:Channel的多路复用,Flume中的Source、Sink和Channel都可以多路复用。下面是一个Channel多路复用的例子
在这里插入图片描述

# 定义3种组件的名称
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2

# 将Event源配置为HTTP报文类型的
a1.sources.r1.type = org.apache.flume.source.http.HTTPSource
a1.sources.r1.host = localhost
a1.sources.r1.port = 50000
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.channels = c1 c2

# 配置多路复用选择器(selector)
a1.sources.r1.selector.header = city
a1.sources.r1.selector.mapping.BJ = c1
a1.sources.r1.selector.mapping.SH = c2
a1.sources.r1.selector.default = c1

# 配置k1 Sink的类型为文件类型
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /home/hadoop/flume-log/
a1.sinks.k1.sink.pathManager.extension = dat
a1.sinks.k1.sink.pathManager.prefix = http
a1.sinks.k1.sink.rollInterval = 0
a1.sinks.k1.channel = c1

#配置k2 logger类型
a1.sinks.k2.type = logger
a1.sinks.k2.channel = c2

# 配置c1和c2的类型为内存类型
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

运行命令:

flume-ng agent --conf /home/hadoop/bigdata/flume/conf/ --f /home/hadoop/934.conf --name a1 -Dflume.root.logger=INFO,console

模拟请求:

curl -X POST -d '[{"headers" :{"city" : "BJ"},"body" :"test from Beijing"}]' http://49.235.66.205:50000
curl -X POST -d '[{"headers" :{"city" : "SH"},"body" :"test from Beijing"}]' http://49.235.66.205:50000

场景五:合并两个网站的日志。Flume Agent也支持多个Source。合并网站日志的数据流图如下图所示,这里分别把slave01和slave02上的Apache日志Event以Avro方式发送到master Agent上,再落地到文件中
在这里插入图片描述
slave01:

# Name the components on this agent
a1.sources = r1
a1.sinks = avrosink
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = sudo tail -F /var/log/apache2/access.log

# Describe the avro sink
a1.sinks.avrosink.type = avro
a1.sinks.avrosink.hostname = master
a1.sinks.avrosink.port = 41414

# 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.avrosink.channel = c1

slave02:

# Name the components on this agent
a1.sources = r1
a1.sinks = avrosink
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = sudo tail -F /var/log/apache2/access.log

a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = regex_filter
a1.sources.r1.interceptors.i1.regex = .html
a1.sources.r1.interceptors.i1.excludeEvents = false

# Describe the avro sink
a1.sinks.avrosink.type = avro
a1.sinks.avrosink.hostname = master
a1.sinks.avrosink.port = 41414

# 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.avrosink.channel = c1

master

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.bind = master
a1.sources.r1.port = 41414
# Describe the sink
a1.sinks.k1.type =file_roll
a1.sinks.k1.sink.directory=/home/hadoop/flume-log/
a1.sinks.k1.sink.pathManager.extension=dat
a1.sinks.k1.sink.pathManager.prefix=avro
a1.sinks.k1.sink.rollInterval=3000
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.channels.c1.keep-alive = 60

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

### 关于大数据平台中 Flume安装配置与运维 Flume 是一种分布式、可靠且高可用的日志收集系统,广泛应用于大数据平台中的日志数据采集和传输场景。以下是关于 Flume大数据平台中的安装、配置以及运维的一些关键点: #### 1. **Flume 的基本概念** Flume 主要由个核心组件组成:Source(源)、Channel(通道)和 Sink(目标)。 - Source 负责接收事件并将其传递到 Channel 中。 - Channel 是一个临时缓冲区,用于存储来自 Source 的事件直到它们被传送到 Sink- Sink 将事件写入外部存储或发送到下一个 Flume Agent[^3]。 #### 2. **Flume安装过程** 在 Linux 环境下安装 Flume 可以通过以下方式完成: - 下载 Apache Flume 的官方版本包,并解压至指定目录。 - 设置环境变量 `FLUME_HOME` 和 `PATH` 来简化命令调用。 - 使用如下命令验证 Flume 是否成功安装: ```bash [root@master conf]# flume-ng version ``` 如果显示 Flume 版本号,则说明安装成功[^3]。 #### 3. **Flume 的配置方法** Flume 的配置主要依赖于其配置文件(通常是 `.conf` 文件),该文件定义了 Source、Channel 和 Sink 的具体实现及其参数设置。例如,下面是一个简单的 Flume 配置示例: ```properties agent.sources = netcat-source agent.sinks = logger-sink agent.channels = memory-channel agent.sources.netcat-source.type = netcat agent.sources.netcat-source.bind = localhost agent.sources.netcat-source.port = 44444 agent.sinks.logger-sink.type = logger agent.channels.memory-channel.type = memory agent.channels.memory-channel.capacity = 1000 agent.channels.memory-channel.transactionCapacity = 100 agent.sources.netcat-source.channels = memory-channel agent.sinks.logger-sink.channel = memory-channel ``` 此配置表示 Flume 接收网络输入并通过内存通道将数据记录到控制台日志中[^3]。 #### 4. **Flume 的运维实践** 为了确保 Flume 在生产环境中稳定运行,可以采取以下措施: - **性能优化**:调整 Channel 的容量 (`capacity`) 和事务大小 (`transactionCapacity`) 参数来适应不同的流量负载[^3]。 - **监控机制**:利用 Cloudera Manager 或 Ambari 等工具对 Flume 实时监控,查看吞吐量、延迟等指标[^4]。 - **错误恢复策略**:启用 Flume 的内置重试机制,在发生短暂连接失败时自动尝试重新发送数据[^3]。 #### 5. **常见问题排查** 当遇到 Flume 故障时,可以通过检查日志文件定位问题原因。一些典型的错误可能包括但不限于: - 数据丢失:可能是由于 Channel 缓存不足或者 Sink 写入速度过慢引起。 - 性能瓶颈:通常表现为 CPU 占用率过高或磁盘 I/O 延迟增加。 --- ### 示例代码 启动 Flume Agent 的命令如下所示: ```bash flume-ng agent --name agent --conf $FLUME_CONF_DIR --conf-file example.conf -Dflume.root.logger=INFO,console ``` 其中 `$FLUME_CONF_DIR` 表示 Flume 配置文件所在的路径,而 `example.conf` 则是具体的配置文件名称[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值