Flume(03)——Flume采集案例

1 采集目录到HDFS

需求分析

采集需求:某服务器的某特定目录/export/servers/dirfile下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去。
需求分析:
通过flume采集数据,最重要的就是配置三大组件。
这里可以通过source来监控文件目录。
通过channel,来将source采集到的内容发送到sink
通过sink,将文件上传到HDFS文件系统。
在这里插入图片描述
数据源组件source ——监控文件目录 的type为: spooldir
spooldir具有以下一些特性:
1、监视一个目录,只要目录中出现新文件,就会采集文件中的内容
2、采集完成的文件,会被agent自动添加一个后缀:COMPLETED
3、所监视的目录中不允许重复出现相同文件名的文件

开发配置文件

flume的开发,大多数时候是flume配置文件的开发。
cd /export/servers/apache-flume-1.6.0-cdh5.14.0-bin/conf
mkdir -p /export/servers/dirfile
vim spooldir.conf

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
##定义type为spooldir,必配项。不能往监控目录中重复丢同名文件
a1.sources.r1.type = spooldir
## spoolDir是要监控的目录,这个选项为必配项。
a1.sources.r1.spoolDir = /export/servers/dirfile
a1.sources.r1.fileHeader = true
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = hdfs://node01:8020/spooldir/files/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
#capacity:默认该通道中最大的可以存储的event数量
a1.channels.c1.capacity = 1000
#trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动flume

cd /export/servers/apache-flume-1.6.0-cdh5.14.0-bin

bin/flume-ng agent -c ./conf -f ./conf/spooldir.conf -n a1 -Dflume.root.logger=INFO,console
测试:上传文件到指定目录

将不同的文件上传到目录/export/servers/dirfile里面去,注意文件不能重名。会发现文件汇被自动收集。并且存储到hdfs://node01:8020/spooldir/files/%y-%m-%d/%H%M/中。

2 采集文件到HDFS

需求分析:

采集需求:某业务系统使用log4j生成日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs
根据需求,首先定义以下3大要素
采集源,即source——监控文件内容更新 : exec ‘tail -F file’
下沉目标,即sink——HDFS文件系统 : hdfs sink
Source和sink之间的传递通道——channel,可用file channel 也可以用 内存channel
在这里插入图片描述

开发配置文件

cd /export/servers/apache-flume-1.6.0-cdh5.14.0-bin/conf
vim tail-file.conf

配置文件内容:

a1.sources = source1
a1.sinks = sink1
a1.channels = channel1

# Describe/configure tail -F source1
a1.sources.source1.type = exec
a1.sources.source1.command = tail -F /export/servers/taillogs/access_log
a1.sources.source1.channels = channel1

#configure host for source
#a1.sources.source1.interceptors = i1
#a1.sources.source1.interceptors.i1.type = host
#a1.sources.source1.interceptors.i1.hostHeader = hostname

# Describe sink1
a1.sinks.sink1.type = hdfs
#a1.sinks.k1.channel = c1
a1.sinks.sink1.hdfs.path = hdfs://node01:8020/weblog/flume-collection/%y-%m-%d/%H-%M
a1.sinks.sink1.hdfs.filePrefix = access_log
a1.sinks.sink1.hdfs.maxOpenFiles = 5000
a1.sinks.sink1.hdfs.batchSize= 100
a1.sinks.sink1.hdfs.fileType = DataStream
a1.sinks.sink1.hdfs.writeFormat =Text
a1.sinks.sink1.hdfs.rollSize = 102400
a1.sinks.sink1.hdfs.rollCount = 1000000
a1.sinks.sink1.hdfs.rollInterval = 60
a1.sinks.sink1.hdfs.round = true
a1.sinks.sink1.hdfs.roundValue = 10
a1.sinks.sink1.hdfs.roundUnit = minute
a1.sinks.sink1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in memory
a1.channels.channel1.type = memory
a1.channels.channel1.keep-alive = 120
a1.channels.channel1.capacity = 500000
a1.channels.channel1.transactionCapacity = 600

# Bind the source and sink to the channel
a1.sources.source1.channels = channel1
a1.sinks.sink1.channel = channel1

(agent的名字也是可以随便起的,不一定非要叫a1)

启动flume

cd /export/servers/apache-flume-1.6.0-cdh5.14.0-bin

bin/flume-ng agent -c conf -f conf/tail-file.conf -n a1  -Dflume.root.logger=INFO,console
增加文件测试程序
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值