Flume总结02_使用案例

本文详述 Flume 在不同场景下的应用案例,包括监控端口、读取本地文件到 HDFS、监听目录文件、单数据源多出口、多数据源汇总等,通过实例配置解析 Flume 的灵活运用。

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

目录

监控某个端口

实时读取本地文件到 HDFS

实时读取目录文件到 HDFS 案例

单数据源多出口案例(选择器)

单数据源多出口案例(Sink )

多数据源汇总案例


本文具体列举几个常见的flume使用案例,具体安装过程参见 Flume总结01_架构和基本概念 部分。

在安装完成后,具体配置过程如下

监控某个端口

1 在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf

# Name the components on this agent   a1:表示agent的名称
a1.sources = r1                       r1:表示a1的输入源
a1.sinks = k1                         k1:表示a1的输出目的地
a1.channels = c1                      c1:表示a1的缓冲区
# Describe/configure the source        
a1.sources.r1.type = netcat           表示a1的输入源类型为netcat端口类型
a1.sources.r1.bind = localhost        表示a1的监听的主机      
a1.sources.r1.port = 44444            表示a1的监听的端口号
# Describe the sink
a1.sinks.k1.type = logger             表示a1的输出目的地是控制台logger类型
# Use a channel which buffers events in memory
a1.channels.c1.type = memory          表示a1的channel类型是memory内存型
a1.channels.c1.capacity = 1000        表示a1的channel总容量1000个event
a1.channels.c1.transactionCapacity = 100        表示a1的channel传输时收集到了100条event以后再去提交事务
# Bind the source and sink to the channel
a1.sources.r1.channels = c1           表示将r1和c1连接起来
a1.sinks.k1.channel = c1              表示将k1和c1连接起来

2 启动任务

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

实时读取本地文件到 HDFS

1)拷贝依赖包

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

2)在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2

# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/flume/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-
#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 1000
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 600
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k2.hdfs.rollCount = 0
#最小冗余数
a2.sinks.k2.hdfs.minBlockReplicas = 1
# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

3.执行监控配置
    $ bin/flume-ng agent --conf conf/ --name  a2 --conf-file job/flume-file-hdfs.conf

实时读取目录文件到 HDFS 案例

1. 使用 Flume 监听整个目录的文件,如 /opt/module/flume/upload

2. 在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf

a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /opt/module/flume/upload
a3.sources.r3.fileSuffix = .COMPLETED
a3.sources.r3.fileHeader = true
#忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path  =
hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H
#上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
#多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue = 1
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 600
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0
#最小冗余数
a3.sinks.k3.hdfs.minBlockReplicas = 1
# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

注意: 在使用spooldir监控整个目录时
  1) 不要在监控目录中创建并持续修改文件
  2) 上传完成的文件会以.COMPLETED 结尾
  3) 被监控文件夹每 500 毫秒扫描一次文件变动

3. 启动监控文件夹命令
   $ bin/flume-ng agent --conf conf/ --name  a3 --conf-file job/flume-dir-hdfs.conf

单数据源多出口案例(选择器)

1.在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf,
配置 1 个接收日志文件的 source 和两个 channel、两个 sink,分别输送给 flume-flume-hdfs 和 flume-flume-dir。

# Name the components on this agent
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 将数据流复制给所有 channel
a1.sources.r1.selector.type = replicating
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/hive/logs/hive.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop102
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop102
a1.sinks.k2.port = 4142
# Describe the channel
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
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2

2.创建 flume-flume-hdfs.conf

配置上级 Flume 输出的 Source,输出是到 HDFS 的 Sink。

# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop102
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hadoop102:9000/flume2/%Y%m%d/%H
#上传文件的前缀
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照时间滚动文件夹
a2.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#设置每个文件的滚动大小大概是 128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k1.hdfs.rollCount = 0
#最小冗余数
a2.sinks.k1.hdfs.minBlockReplicas = 1
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1

3.创建 flume-flume-dir.conf
配置上级 Flume 输出的 Source,输出是到本地目录的 Sink。

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop102
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /opt/module/datas/flume3
# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2

4.执行配置文件
分别开启对应配置文件:flume-flume-dir,flume-flume-hdfs,flume-file-flume。
   $ bin/flume-ng agent --conf conf/ --name a3 --conf-file job//flume-flume-dir.conf
   $ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group1/flume-flume-hdfs.conf
   $ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group1/flume-file-flume.conf

单数据源多出口案例(Sink )

1.在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf,
配置 1 个接收日志文件的 source 和 1 个 channel、两个 sink,分别输送给flume-flume-console1 和 flume-flume-console2

# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinkgroups = g1
a1.sinks = k1 k2
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
a1.sinkgroups.g1.processor.selector = round_robin
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop102
a1.sinks.k1.port = 4141
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop102
a1.sinks.k2.port = 4142
# Describe the channel
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.sinkgroups.g1.sinks = k1 k2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1

2.创建 flume-flume-console1.conf
配置上级 Flume 输出的 Source,输出是到本地控制台。

# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop102
a2.sources.r1.port = 4141
# Describe the sink
a2.sinks.k1.type = logger
# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1

3.创建 flume-flume-console2.conf
配置上级 Flume 输出的 Source,输出是到本地控制台。

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop102
a3.sources.r1.port = 4142
# Describe the sink
a3.sinks.k1.type = logger
# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2

4.执行配置文件
分别开启对应配置文件:flume-flume-console2,flume-flume-console1,flume-netcat-flume。

$ bin/flume-ng agent --conf conf/ --name a3  --conf-file  job/flume-flume-console2.conf -Dflume.root.logger=INFO,console
$ bin/flume-ng agent --conf conf/ --name a2  --conf-file  job/flume-flume-console1.conf -Dflume.root.logger=INFO,console
$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-flume.conf

多数据源汇总案例

1.在 flume 目录下创建 job 文件夹,在 job 文件夹下创建 Flume Agent 配置文件 flume-telnet-logger.conf,
配置 Source 用于监控 hive.log 文件,配置 Sink 输出数据到下一级 Flume。

# 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 /opt/module/group.log
a1.sources.r1.shell = /bin/bash -c
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop104
a1.sinks.k1.port = 4141
# Describe the channel
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

2.创建 flume2-netcat-flume.conf
配置 Source 监控端口 44444 数据流,配置 Sink 数据到下一级 Flume:

# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1
# Describe/configure the source
a2.sources.r1.type = netcat
a2.sources.r1.bind = hadoop102
a2.sources.r1.port = 44444
# Describe the sink
a2.sinks.k1.type = avro
a2.sinks.k1.hostname = hadoop104
a2.sinks.k1.port = 4141
# Use a channel which buffers events in memory
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1

3.创建 flume3-flume-logger.conf
配置 source 用于接收 flume1 与 flume2 发送过来的数据流,最终合并后 sink 到控制台。

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c1
# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop104
a3.sources.r1.port = 4141
# Describe the sink
# Describe the sink
a3.sinks.k1.type = logger
# Describe the channel
a3.channels.c1.type = memory
a3.channels.c1.capacity = 1000
a3.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a3.sources.r1.channels = c1
a3.sinks.k1.channel = c1

4.执行配置文件
分 别 开 启 对 应 配 置 文 件 : flume3-flume-logger.conf , flume2-netcat-flume.conf ,flume1-logger-flume.conf。

$ bin/flume-ng agent --conf conf/ --name a3  --conf-file  job/group3/flume3-flume-logger.conf -Dflume.root.logger=INFO,console
$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group3/flume2-netcat-flume.conf
$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group3/flume1-logger-flume.conf
### Apache Flume 数据采集案例与示例 Apache Flume 是一个分布式、可靠且高可用的系统,用于高效地收集、聚合和移动大量日志数据。以下是一些常见Flume 数据采集案例及配置教程。 #### 1. Flume 单机部署案例 Flume 可以在单机模式下运行,适用于小型项目或测试环境。通过简单的配置文件,可以实现从源端(source)到目标端(sink)的数据传输。 - **配置文件示例**: ```properties # 定义组件 agent.sources = r1 agent.sinks = k1 agent.channels = c1 # 配置 source agent.sources.r1.type = netcat agent.sources.r1.bind = localhost agent.sources.r1.port = 44444 # 配置 channel agent.channels.c1.type = memory agent.channels.c1.capacity = 1000 agent.channels.c1.transactionCapacity = 100 # 配置 sink agent.sinks.k1.type = logger # 绑定 source 和 sink 到 channel agent.sources.r1.channels = c1 agent.sinks.k1.channel = c1 ``` 启动命令: ```bash flume-ng agent --name agent --conf-file flume-conf.properties -Dflume.root.logger=INFO,console ``` 上述配置中,`netcat` 类型的 source 监听指定端口上的数据输入,并将其通过内存 channel 传递给 logger 类型的 sink[^1]。 --- #### 2. Flume 集群部署案例 Flume 支持集群部署,适用于大规模数据采集场景。以下是一个负载均衡和故障转移的案例。 - **配置文件示例**: ##### flume1.conf(主节点) ```properties # 定义组件 a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # 配置 source a1.sources.r1.type = netcat a1.sources.r1.bind = localhost a1.sources.r1.port = 44444 # 配置 channels 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 # 配置 sinks a1.sinks.k1.type = avro a1.sinks.k1.hostname = worker215 a1.sinks.k1.port = 4141 a1.sinks.k2.type = avro a1.sinks.k2.hostname = worker216 a1.sinks.k2.port = 4142 # 绑定 source 和 sinks 到 channels a1.sources.r1.channels = c1 c2 a1.sinks.k1.channel = c1 a1.sinks.k2.channel = c2 ``` ##### flume2.conf(子节点1) ```properties # 定义组件 a2.sources = r1 a2.sinks = k1 a2.channels = c1 # 配置 source a2.sources.r1.type = avro a2.sources.r1.bind = worker215 a2.sources.r1.port = 4141 # 配置 channel a2.channels.c1.type = memory a2.channels.c1.capacity = 1000 a2.channels.c1.transactionCapacity = 100 # 配置 sink a2.sinks.k1.type = logger # 绑定 source 和 sink 到 channel a2.sources.r1.channels = c1 a2.sinks.k1.channel = c1 ``` ##### flume3.conf(子节点2) ```properties # 定义组件 a3.sources = r1 a3.sinks = k1 a3.channels = c2 # 配置 source a3.sources.r1.type = avro a3.sources.r1.bind = worker216 a3.sources.r1.port = 4142 # 配置 channel a3.channels.c2.type = memory a3.channels.c2.capacity = 1000 a3.channels.c2.transactionCapacity = 100 # 配置 sink a3.sinks.k1.type = logger # 绑定 source 和 sink 到 channel a3.sources.r1.channels = c2 a3.sinks.k1.channel = c2 ``` 启动顺序: 1. 启动子节点 `flume2` 和 `flume3`。 2. 启动主节点 `flume1`。 通过这种方式,Flume 实现了负载均衡和故障转移功能[^2]。 --- #### 3. Flume 与 Python 集成示例 Flume 提供了 Avro 源类型,支持通过 Python 客户端向 Flume 发送数据。 - **Python 示例代码**: ```python from avro.ipc import AvroRemoteException from avro.datafile import DataFileReader, DataFileWriter from avro.io import DatumReader, DatumWriter import avro.protocol import avro.schema import sys import json # 定义协议 proto = avro.protocol.parse(json.dumps({ "namespace": "example.proto", "protocol": "FlumeProtocol", "types": [], "messages": { "append": { "request": [{"name": "body", "type": "bytes"}], "response": "null" } } })) # 创建客户端 client = proto.client('http://localhost:4141') # 发送数据 data = b"Hello Flume!" try: client.request('append', {'body': data}) except AvroRemoteException as e: print(f"Error sending data: {e}") ``` 上述代码展示了如何使用 Python 客户端向 Flume 的 Avro source 发送数据。 --- #### 4. Flume 与 Java 集成示例 Flume 提供了 Java API,允许开发者直接通过编程方式与 Flume 进行交互。 - **Java 示例代码**: ```java import org.apache.flume.Event; import org.apache.flume.EventDeliveryException; import org.apache.flume.api.RpcClient; import org.apache.flume.api.RpcClientFactory; import org.apache.flume.event.SimpleEvent; public class FlumeJavaClient { public static void main(String[] args) { // 创建 RpcClient RpcClient client = RpcClientFactory.getDefaultInstance("localhost", 4141); try { // 创建 Event Event event = new SimpleEvent(); event.setBody("Hello Flume!".getBytes()); // 发送 Event client.append(event); System.out.println("Data sent successfully!"); } catch (EventDeliveryException e) { System.err.println("Failed to send data: " + e.getMessage()); } finally { // 关闭客户端 client.close(); } } } ``` 上述代码展示了如何使用 Java API 向 Flume 的 Avro source 发送数据。 --- #### 5. 总结 以上案例涵盖了 Flume 在单机和集群环境下的数据采集配置,以及与 Python 和 Java 的集成示例。通过这些示例,用户可以灵活调整 Flume 的数据流处理方式,满足不同的业务需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值