Flume 采集案例
1:采集目录到 HDFS
采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到 HDFS 中去。
采集源,即 source -- 监控文件目录 : spooldir
下沉目标,即 sink --HDFS文件系统 :HDFS sink
source 和 sink 之间的传递通道 -- channel , 可用 file channel 也可以用 内存 memory channel 。
配置文件编写:
#定义 source , sink ,channel 三大组件的名称
agent1.sources = source1
agent.sinks = sink1
agent.channels = channel1
#配置 source 组件
agent1.sources.source1.type = spooldir
agent1.sources.source1.spooldir = /root/date/
agent1.sources.source1.fileHeader = false
#配置拦截器
agent1.sources.source1.interceptors = i1
agent1.sources.source1.interceptors.i1.type = timestamp
#配置 sink 组件
agent1.sinks.sink1.type = hdfs
agent1.sinks.sink1.hdfs.path = /weblog/flume-collection/%y-%m-%d/%H-%M
agent1.sinks.sink1.hdfs.filePrefix = access_log
agent1.sinks.sink1.hdfs.maxOpenFiles = 5000
agent1.sinks.sink1.hdfs.batchSize = 100
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat = Text
#滚动生成的文件按大小生成
agent1.sinks.sink1.hdfs.rollSize = 102400
#滚动生成的文件按行数生成
agent1.sinks.sink1.hdfs.rollCount = 1000000
#滚动生成的文件按时间生成
agent1.sinks.sink1.hdfs.rollInterval = 60
#开启滚动生成目录
agent1.sinks.sink1.hdfs.round = true
#以 10 为一梯度滚动生成
agent1.sinks.sink1.hdfs.roundValue = 10
#单位为分钟
agent1.sinks.sink1.hdfs.roundUnit = minute
#配置 channel 组件
agent1.channels.channel1.type = memory
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600
agent1.channels.channel1.keep-alive = 120
#将 source channel sink 三个组件进行绑定
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1
flume 的 source 采用 spoodir 时!目录下面不允许存放同名的文件,否则报错!
capacity : 默认该通道最大的可以存储的 event 数量
transactionCapacity :每次最大可以从 source 中拿到或者送到 sink 中的 event 数量
keep-alive :event 添加到通道中或者移出的允许时间
其他组件: Interceptor (拦截器)
用于 source 的一组Interceptor,按照预设的顺序在必要的地方装饰和过滤 events。
内建的 Interceptor 允许增加 event 的 headers 比如:时间戳、主机名、静态标记等等
定制的 Interceptor 可以通过内省 event payload (读取原始日志),实现自己的业务逻辑(很强大)
2:采集文件到 HDFS
采集需求:比如业务系统使用 log4j 生成日志,日志内容不断的增加,需要把追加到日志文件中的数据实时采集到 hdfs
根据需求,首先定义一下 3 大要素:
采集源,即 source -- 监控文件内容更新: exec 'tail -F file'
下沉目标,即 sink -- HDFS文件系统: hdfs sink
Source 和 sink 之间的传递通道 -- channel ,可用 file channel 也可以使用 memory channel 。
配置文件编写:
#定义 source , sink ,channel 三大组件的名称
agent1.sources = source1
agent.sinks = sink1
agent.channels = channel1
#配置 source 组件
agent1.sources.source1.type = exec
agent1.sources.source1.spooldir = tail -F /home/hadoop/logs/access_log
#配置拦截器
agent1.sources.source1.interceptors = i1
agent1.sources.source1.interceptors.i1.type = host
agent1.sources.source1.interceptors.i1.hostHeader = hostname
#配置 sink 组件
agent1.sinks.sink1.type = hdfs
agent1.sinks.sink1.hdfs.path = hdfs://hadoop01:9000/file/%{hostname}/%y-%m-%d/%H-%M
agent1.sinks.sink1.hdfs.filePrefix = access_log
agent1.sinks.sink1.hdfs.batchSize = 100
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat = Text
#滚动生成的文件按大小生成
agent1.sinks.sink1.hdfs.rollSize = 102400
#滚动生成的文件按行数生成
agent1.sinks.sink1.hdfs.rollCount = 1000000
#滚动生成的文件按时间生成
agent1.sinks.sink1.hdfs.rollInterval = 60
#开启滚动生成目录
agent1.sinks.sink1.hdfs.round = true
#以 10 为一梯度滚动生成
agent1.sinks.sink1.hdfs.roundValue = 10
#单位为分钟
agent1.sinks.sink1.hdfs.roundUnit = minute
#配置 channel 组件
agent1.channels.channel1.type = memory
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600
agent1.channels.channel1.keep-alive = 120
#将 source channel sink 三个组件进行绑定
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1
3:多个 agent 串联
采集需求: 比如业务系统使用 log4j 生成的日志,日志内容不断增加,需求把追加到日志文件中的数据实时采集到hdfs ,使用agent 串联
根据需求,首先定义以下 3 大要素
第一台 flume agent
采集源,即 source -- 监控文件内容更新 : exec 'tail -F file'
下沉目标,即 sink -- 数据的发送者,实现序列化 : avro sink
Source 和 sink 之间的传递通道 -- channel ,可用 file channel 也可以使用 memory channel
第二台 flume agent
采集源,即 source -- 接受数据。并实现反序列化 : avro source
下沉目标,即 sink -- HDFS 文件系统 : hdfs sink
Source 和 sink 之间的传递通道 -- channel ,可用 file channel 也可以使用 memory channel
配置文件编写:
flume-agent1
#定义source sink channel 三大组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#配置 source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/test.log
#配置 sink
##sink端的avro是一个数据发送者
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop02
a1.sinks.k1.port = 41414
a1.sinks.k1.batch-size = 10
#配置 channel 使用内存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
flume-agent2
a1.sources = r1
a1.sinks =s1
a1.channels = c1
##source中的avro组件是一个接收者服务
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 41414
a1.sinks.s1.type=hdfs
a1.sinks.s1.hdfs.path=hdfs://hadoop01:9000/flumedata
a1.sinks.s1.hdfs.filePrefix = access_log
a1.sinks.s1.hdfs.batchSize= 100
a1.sinks.s1.hdfs.fileType = DataStream
a1.sinks.s1.hdfs.writeFormat =Text
a1.sinks.s1.hdfs.rollSize = 10240
a1.sinks.s1.hdfs.rollCount = 1000
a1.sinks.s1.hdfs.rollInterval = 10
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundValue = 10
a1.sinks.s1.hdfs.roundUnit = minute
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sinks.s1.channel = c1