需求
某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去
思路
根据需求,首先定义以下3大要素
1. 数据源组件,即source ——监控文件目录 : spooldir
1. 监视一个目录,只要目录中出现新文件,就会采集文件中的内容
2. 采集完成的文件,会被agent自动添加一个后缀:COMPLETED
3. 所监视的目录中不允许重复出现相同文件名的文件
2. 下沉组件,即sink——HDFS文件系统 : hdfs sink
3. 通道组件,即channel——可用file channel 也可以用内存channel
cd /usr/flume/conf
mkdir /usr/dirfile
vim spooldir.conf
#name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
##注意:不能往监控目中重复丢同名文件
a1.sources.r1.type = spooldir #conf
a1.sources.r1.spoolDir = /usr/dirfile #所监控的文件夹
a1.sources.r1.fileHeader = true
# Describe the sink flume目录数据采集
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 #hdfs文件滚动周期
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3 #每三秒产生新文件
a1.sinks.k1.hdfs.rollSize = 20 #临时文件大小达到20个字节
a1.sinks.k1.hdfs.rollCount = 5 #临时文件大小达到5个event时
a1.sinks.k1.hdfs.batchSize = 1 #channel中event转入transactionCapacity的数量
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
a1.channels.c1.capacity = 1000 #channels容量
a1.channels.c1.transactionCapacity = 100 #channels一次向sinks传递的文件数
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
Step 2: 启动 Flume
flume-ng agent -c conf -f conf/spooldir.conf -n a1 -Dflume.root.logger=INFO,console
flume-ng flume版本
-c conf 指定flume自身的配置文件所在目录
-f conf/spooldir.con 指定我们所描述的采集方案
-n a1 指定我们这个agent的名字
Step 3: 上传文件到指定目录
将不同的文件上传到下面目录里面去,注意文件不能重名,尽量纯英文
cd /usr/dirfile
来源:CSDN
作者:浮躁-lh
链接:https://blog.csdn.net/qq_42214376/article/details/104105305