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

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

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添加到通道中或者移出的允许时间

执行命令

[root@hadoop1 apache-flume-1.6.0-bin]#cd /home/tuzq/software/apache-flume-1.6.0-bin
[root@hadoop1 apache-flume-1.6.0-bin]#bin/flume-ng agent -c ./conf -f ./agentconf/spool-logger.conf -n a1 -Dflume.root.logger=INFO,console;

接着往/home/tuzq/software/flumedata文件夹中扔文件

[root@hadoop1 flumedata]# pwd
/home/tuzq/software/flumedata
[root@hadoop1 flumedata]# echo 111111111 >> 1.txt
[root@hadoop1 flumedata]# ls
1.txt.COMPLETED  test.log.COMPLETED
[root@hadoop1 flumedata]# echo 22222222 >> 2.txt
[root@hadoop1 flumedata]# echo 33333333 >> 3.txt
[root@hadoop1 flumedata]# echo 44444444 >> 4.txt
[root@hadoop1 flumedata]# ls
1.txt.COMPLETED  2.txt.COMPLETED  3.txt.COMPLETED  4.txt.COMPLETED  test.log.COMPLETED
[root@hadoop1 flumedata]#

扔了之后,现象是 
1、/home/tuzq/software/flumedata文件文件夹下的文件倍加了一个一个后缀.COMPLETED, 
2、在flume的监控位置,出现类似下图一样的文件: 
3、到hdfs上查看文件:

[root@hadoop1 flumedata]# hdfs dfs -ls /
Found 5 items
drwxr-xr-x   - root supergroup          0 2017-06-13 12:01 /40000
drwxr-xr-x   - root supergroup          0 2017-06-13 23:43 /flume
-rw-r--r--   3 root supergroup       3719 2017-06-10 12:11 /kms.sh
drwxrwxrwx   - root supergroup          0 2017-06-10 22:06 /tmp
drwxr-xr-x   - root supergroup          0 2017-06-10 22:27 /user
[root@hadoop1 flumedata]# hdfs dfs -ls /flume
Found 2 items
drwxr-xr-x   - root supergroup          0 2017-06-13 23:43 /flume/events
drwxr-xr-x   - root supergroup          0 2017-06-13 22:01 /flume/tailout
[root@hadoop1 flumedata]# hdfs dfs -ls /flume/events
Found 1 items
drwxr-xr-x   - root supergroup          0 2017-06-13 23:47 /flume/events/17-06-13
[root@hadoop1 flumedata]# hdfs dfs -ls /flume/events/17-06-13
Found 3 items
drwxr-xr-x   - root supergroup          0 2017-06-13 23:43 /flume/events/17-06-13/2343
drwxr-xr-x   - root supergroup          0 2017-06-13 23:46 /flume/events/17-06-13/2346
drwxr-xr-x   - root supergroup          0 2017-06-13 23:47 /flume/events/17-06-13/2347
[root@hadoop1 flumedata]#

综上所述:说明通过flume已经把新增的文件下沉到了hdfs中。

原文链接

如果Flume监听文件完成之后,HDFS未接收到数据,可能有以下几个原因: 1. **配置文件不正确**[^1]: 检查`flume-file-hdfs.conf`中的source(源)、sink(目的地)设置是否正确,比如Source应配置为读取指定文件,Sink应配置为将数据发送至HDFS。 ```yaml .sources = source1 sinks = sink1 channels = channel1 sources.source1.type = file sources.source1.file.filename = <your_file_path> sources.source1.file.channels = channel1 sinks.sink1.type = hdfs sinks.sink1.hdfs.path = <hdfs_directory> sinks.sink1.hdfs.rollCount = 1 ``` 2. **权限问题**: 确认Flume进程是否有足够的权限访问源文件和写入HDFS。可能是文件权限不够,或者是HDFS客户端配置错误。 3. **数据流异常**[^2]: 如果`flume-hdfs.conf`配置的是追加模式(`-Dflume.root.logger=INFO,console`),可能是因为程序运行时没有开启agent,或者agent在写入数据之前就已经关闭了。 4. **网络问题**: 检查网络连接是否稳定,以及Hadoop集群是否正常运行。如果Flume Agent与HDFS服务器之间的通信有问题,数据可能会丢失或无法到达。 5. **Agent启动方式**: 确认Flume Agent是否按照正确的命令行参数启动,如 `-conf-file job/flume-file-hdfs.conf` 和 `-name a1` 是否匹配配置文件名称。 要解决这个问题,你可以按照以下步骤排查: 1. 查看Flume日志以寻找错误信息。 2. 确认配置文件中的文件路径和HDFS路径是否正确。 3. 检查网络状态和代理之间的通信是否畅通。 4. 启动Agent时确保使用的配置文件与实际配置一致。 5. 如果是在追加模式下,确认数据已经写入到本地文件Flume Agent已启动正在监视该文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值