Flume基础入门

本文详细介绍Flume的日志采集机制,包括netcat、exec、spool dir、Avro、Kafka、taildir等source类型,memory、file、Kafka等channel类型,以及HDFS、Avro等sink类型。深入探讨每种类型的特性和配置参数。

1.Flume简介

flume是一个日志采集工具,使用Event在中间传输,event可以携带headers,以byte数组的形式发送。为了保证一定传输成功,flume每个agent都有俩个事务,一个是source和channel之间的事务,一个是channel和sink之间的事务。
在数据传输的过程中,在到目的地之前会先缓存数据,直至成功以后才会删除数据

2.source类型

  • netcat source

工作机制

启动一个socket服务,监听一个端口;将端口上收到的数据,转成event写入channel

配置文件

a1.sources = s1
a1.sources.s1.type = netcat
a1.sources.s1.bind = 0.0.0.0
a1.sources.s1.port = 44444
a1.sources.s1.channels = c1
  • exec source

工作机制

启动一个用户所指定的linux shell命令;
采集一个用户指定的linux shell命令的输出,作为收集到的数据,转为event写入channel;

配置文件

a1.sources = s1
a1.sources.s1.channels = c1
a1.sources.s1.type = exec
a1.sources.s1.command = tail -F /root/weblog/access.log 
a1.sources.s1.batchSize = 100

a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 200
a1.channels.c1.transactionCapacity = 100

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1

缺点

当宕机以后,不会记录宕机前数据采集的偏移量,容易造成数据的丢失

  • spool dir source

工作机制

监视一个指定的文件夹,如果文件夹下有没采集过的新文件,则将这些新文件中的数据采集,并转成event写入channel;
注意:spooling目录中的文件必须是不可变的,而且是不能重名的!否则,source会loudly fail!

配置文件

a1.sources = s1
a1.sources.s1.channels = c1
a1.sources.s1.type = spooldir
a1.sources.s1.spoolDir = /root/weblog 
a1.sources.s1.batchSize = 200

a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 200
a1.channels.c1.transactionCapacity = 100

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1

spooling在宕机以后会记录宕机前数据采集的便宜量,所以是一个安全的采集source

  • avro source

工作机制

启动一个服务,监听一个端口,收集端口上收到的avro序列化数据流!
该source中拥有avro的反序列化器,能够将收到的二进制流进行正确反序列化,并装入一个event写入channel!

配置文件

a1.sources = r1
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 4141


a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 200
a1.channels.c1.transactionCapacity = 100

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1
  • kafka source

工作机制

Kafka source的工作机制:就是用kafka consumer连接kafka,读取数据,然后转换成event,写入channel

配置文件

a1.sources = s1
a1.sources.s1.type = org.apache.flume.source.kafka.KafkaSource
a1.sources.s1.channels = c1
a1.sources.s1.batchSize = 100
a1.sources.s1.batchDurationMillis = 2000
a1.sources.s1.kafka.bootstrap.servers = c701:9092,c702:9092,c703:9092
a1.sources.s1.kafka.topics = TAOGE
a1.sources.s1.kafka.consumer.group.id = g1

a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 200
a1.channels.c1.transactionCapacity = 100

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1
  • taildir source

工作机制

监视指定目录下的一批文件,只要某个文件中有新写入的行,则会被tail到
它会记录每一个文件所tail到的位置,记录到一个指定的positionfile保存目录中,格式为json(如果需要的时候,可以人为修改,就可以让source从任意指定的位置开始读取数据)
它对采集完成的文件,不会做任何修改(比如重命名,删除……)

配置文件

a1.sources = r1
a1.sources.r1.type = TAILDIR
a1.sources.r1.channels = c1
a1.sources.r1.positionFile = /root/flumedata/taildir_position.json
a1.sources.r1.filegroups = f1
a1.sources.r1.filegroups.f1 = /root/weblog/access.log
a1.sources.r1.fileHeader = true
a1.sources.ri.maxBatchCount = 1000

a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 200
a1.channels.c1.transactionCapacity = 100

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1

注意

taildir是一个可靠的source,采集数据的时候一般选择使用taildir

3.channel

  • memory channel

特性

事件被存储在实现配置好容量的内存(队列)中。
速度快,但可靠性较低,有可能会丢失数据

配置参数

a1.channels = c1
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 10000
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000
  • file channel

特性

event被缓存在本地磁盘文件中
可靠性高,不会丢失
但在极端情况下可能会重复数据

配置参数

a1.sources = r1
a1.sources.r1.type = TAILDIR
a1.sources.r1.channels = c1
a1.sources.r1.positionFile = /root/taildir_chkp/taildir_position.json
a1.sources.r1.filegroups = f1
a1.sources.r1.filegroups.f1 = /root/weblog/access.log
a1.sources.r1.fileHeader = true
a1.sources.ri.maxBatchCount = 1000


a1.channels = c1
a1.channels.c1.type = file
a1.channels.c1.capacity = 1000000
a1.channels.c1.transactionCapacity = 100
a1.channels.c1.checkpointDir = /root/flume_chkp
a1.channels.c1.dataDirs = /root/flume_data

a1.sinks = k1
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c1
  • kafka channel

特性

agent利用kafka作为channel数据缓存
kafka channel要跟 kafka source、 kafka sink区别开来
kafka channel在应用时,可以没有source | 或者可以没有sink

配置参数

a1.sources = r1
# 配置两个channel,为了便于观察
# c1 是kafkachannel ,c2是一个内存channel
a1.channels = c1 c2
a1.sinks = k1

a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/a.log
a1.sources.r1.channels = c1 c2

# kafka-channel具体配置,该channel没有sink
a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
a1.channels.c1.kafka.bootstrap.servers = h1:9092,h2:9092,h3:9092
a1.channels.c1.parseAsFlumeEvent = false

# 内存channel 配置,并对接一个logger sink来观察
a1.channels.c2.type = memory
a1.sinks.k1.type = logger
a1.sinks.k1.channel = c2

4.sink

  • hdfs sink

特性

数据被最终发往hdfs
可以生成text文件或 sequence 文件,而且支持压缩;
支持生成文件的周期性roll机制:基于文件size,或者时间间隔,或者event数量;
目标路径,可以使用动态通配符替换,比如用%D代表当前日期;

配置参数

## 定义
a1.sources = r1
a1.sinks = k1
a1.channels = c1

## source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/a.log
a1.sources.r1.channels = c1
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

## channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 100000000

## sink
a1.sinks.k1.channel = c1
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://h1:8020/doitedu/%Y-%m-%d/%H-%M
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute

a1.sinks.k1.hdfs.filePrefix = doit_
a1.sinks.k1.hdfs.fileSuffix = .log.gz

a1.sinks.k1.hdfs.rollInterval = 0
a1.sinks.k1.hdfs.rollSize = 102400
a1.sinks.k1.hdfs.rollCount = 0

a1.sinks.k1.hdfs.fileType = CompressedStream
a1.sinks.k1.hdfs.codeC = gzip
a1.sinks.k1.hdfs.writeFormat = Text

  • avro sink

特性

avro sink用来向avro source发送avro序列化数据,这样就可以实现agent之间的级联

配置参数

a1.sources = s1
a1.sources.s1.type = exec
a1.sources.s1.command = tail -F /root/weblog/access.log
a1.sources.s1.channels = c1

a1.channels = c1
a1.channels.c1.type = memory

a1.sinks = k1
a1.sinks.k1.type = avro
a1.sinks.k1.channel = c1
a1.sinks.k1.hostname = c701
a1.sinks.k1.port = 4545

5.总结

flume是一个开箱即用的工具,使用的时候配置好参数即可,可以在任何服务器安装flume。

### Apache Flume 入门教程 #### 什么是 Apache Flume? Apache Flume 是一个分布式、可靠且高可用的日志收集系统,主要用于从多个数据源高效地采集日志并将其传输到集中存储系统中。它支持多种数据源输入方式以及灵活的目标输出配置[^1]。 #### Flume 的核心概念 Flume 的架构基于事件驱动模型,其主要组成部分包括以下几个方面: - **Source**:负责接收外部系统的数据流,并将这些数据转换成 Flume 中的标准 Event 形式。 - **Channel**:作为临时缓冲区,用于暂存来自 Source 的 Events,直到它们被传递给 Sink。 - **Sink**:负责将 Channel 中的 Events 转发至目标存储位置(如 HDFS 或 Kafka)。 此外,Flume 还可以扩展其他功能模块,例如拦截器(Interceptors)和选择器(Selectors),以便实现更加复杂的数据处理需求[^2]。 #### 安装与基本配置 以下是安装 Apache Flume 并完成基础配置的具体方法: 1. 下载官方发布的二进制版本压缩包 `apache-flume-1.9.0-bin.tar.gz`,并将该文件上传至 Linux 系统中的任意目录下。 2. 使用命令解压此压缩包到指定路径 `/opt/module/` 下: ```bash tar -zxvf apache-flume-1.9.0-bin.tar.gz -C /opt/module/ ``` 3. 对解压后的文件夹进行重命名操作以简化后续调用过程: ```bash mv apache-flume-1.9.0-bin flume ``` 4. 修改默认模板脚本名称为实际使用的环境变量设置文件名: ```bash cd /opt/module/flume/conf && mv flume-env.sh.template flume-env.sh ``` 5. 编辑上述生成的新文件,在其中定义好 Java 开发工具链所在的位置参数值: ```bash export JAVA_HOME=/usr/local/java/jdk1.8.0_151 ``` #### 启动简单监控端口示例 为了验证当前部署是否成功运行起来,可以通过创建一个小例子来观察效果——即监听某个特定 TCP/IP 协议上的流量情况。 编辑一个新的配置文件命名为 test.conf ,内容如下所示: ```properties agent.sources = netcat-source agent.sinks = logger-sink agent.channels = memory-channel # Describe/configure the source agent.sources.netcat-source.type = netcat agent.sources.netcat-source.bind = localhost agent.sources.netcat-source.port = 44444 # Describe the sink agent.sinks.logger-sink.type = logger # Use a channel which buffers events in memory agent.channels.memory-channel.type = memory agent.channels.memory-channel.capacity = 1000 agent.channels.memory-channel.transactionCapacity = 100 # Bind the source and sink to the channel agent.sources.netcat-source.channels = memory-channel agent.sinks.logger-sink.channel = memory-channel ``` 保存之后执行启动指令即可开始工作模式: ```bash ./bin/flume-ng agent --conf conf/ --name agent --conf-file ./test.conf -Dflume.root.logger=INFO,console ``` 此时如果向本地机器开放的服务地址发送消息,则应该能够在终端窗口看到打印出来的相应记录信息[^3]。 #### 结合 Maven 构建项目实例 当需要通过编程手段接入 Flume 功能时,通常会借助于流行的构建管理框架之一 —— Maven 来组织整个工程结构及其依赖关系。下面给出一段典型的 POM.XML 片段描述如何引入必要的库资源: ```xml <dependency> <groupId>org.apache.flume</groupId> <artifactId>flume-ng-core</artifactId> <version>1.9.0</version> </dependency> <!-- 如果需要用到 Avro 组件 --> <dependency> <groupId>org.apache.avro</groupId> <artifactId>avro</artifactId> <version>1.11.0</version> </dependency> ``` 另外还有一种常见场景就是把 Flume 和 Log4J 日志框架结合起来使用,这样能够方便开发者捕获应用程序内部产生的各种调试提示或者错误警告等内容,并进一步转发出去做统一分析处理[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值