Flume监听文件夹中的文件变化_并把文件下沉到hdfs

阅读全文

1、采集目录到HDFS

采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 
根据需求,首先定义以下3大要素 
采集源,即source——监控文件目录 : spooldir 
下沉目标,即sink——HDFS文件系统 : hdfs sink 
source和sink之间的传递通道——channel,可用file channel 也可以用内存channel

配置文件spooldir-hdfs.conf编写:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
##注意:不能往监控目中重复丢同名文件
## 通过spooldir来监控文件内容的变化
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /home/tuzq/software/flumedata
a1.sources.r1.fileHeader = true

# Describe the sink
## 表示下沉到hdfs,下面配置的类型不同,type下面的参数就不同
a1.sinks.k1.type = hdfs
#sinks.k1只能连接一个channel,source可以配置多个
a1.sinks.k1.channel = c1
#下面的配置告诉用hdfs去写文件的时候写到什么位置,下面的表示不是写死的,而是动态变化的。表示输出的目录名称是可变的
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
#表示文件的前缀
a1.sinks.k1.hdfs.filePrefix = events-
#表示到了需要触发的时间时,是否要更新文件夹,true:表示要更新
a1.sinks.k1.hdfs.round = true
##表示每隔1分钟改变一下文件夹
a1.sinks.k1.hdfs.roundValue = 1
##切换文件的时候单位是分钟
a1.sinks.k1.hdfs.roundUnit = minute
##表示只要过了3秒钟,就切换生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 3
##如果记录的文件大于20字节时切换一次
a1.sinks.k1.hdfs.rollSize = 20
##当写了5个事件时触发
a1.sinks.k1.hdfs.rollCount = 5
##收到了多少条消息往hdfs中追加内容
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
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

Channel参数解释: 
capacity:默认该通道中最大的可以存储的event数量 
trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量 
keep-alive:event添加到通道中或者移出的允许时间

执行命令


阅读全文

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过Flume的Spooling Directory Source来实现这个功能。Spooling Directory Source会监控一个指定的目录,当有新的文件被写入时,就会将文件内容作为事件传递给Flume的Channel。然后再通过Flume的Interceptor将文件的逗号修改为竖杠,并存储到HDFS。 具体的配置如下: 1. 在Flume的配置文件添加一个Spooling Directory Source: ``` # define the Spooling Directory Source agent.sources = spoolDir agent.sources.spoolDir.type = spooldir agent.sources.spoolDir.spoolDir = /path/to/spool/directory agent.sources.spoolDir.fileHeader = true agent.sources.spoolDir.fileSuffix = .COMPLETED ``` 2. 添加一个Interceptor来修改文件内容 ``` # define the Interceptor to modify the file content agent.sources.spoolDir.interceptors = interceptor1 agent.sources.spoolDir.interceptors.interceptor1.type = regex_replace agent.sources.spoolDir.interceptors.interceptor1.regex = , agent.sources.spoolDir.interceptors.interceptor1.replaceString = | ``` 3. 添加一个HDFS Sink ``` # define the HDFS Sink agent.sinks = hdfsSink agent.sinks.hdfsSink.type = hdfs agent.sinks.hdfsSink.hdfs.path = /path/to/hdfs/directory agent.sinks.hdfsSink.hdfs.fileType = DataStream agent.sinks.hdfsSink.hdfs.rollInterval = 0 ``` 最后将Spooling Directory Source和HDFS Sink连接起来: ``` # connect the Source and Sink agent.sources.spoolDir.channels = memoryChannel agent.sinks.hdfsSink.channel = memoryChannel ``` 这样配置后,Flume就会监听指定的目录,当有新文件写入时,将文件内容作为事件传递给内存Channel。Interceptor会修改文件内容,然后将修改后的事件存储到HDFS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值