03 flume监听文件夹并将文件上传到HDFS

我们已经了解了使用flume监听端口以及文件内容,本节将展示使用flume去监听一个文件夹并且上传其中的文件到HDFS。

1 系统、软件以及前提约束

2 操作

  • 1 使用xshell登录到192.168.100.200
  • 2 修改/root/apache-flume-1.8.0-bin/conf/flume-conf.properties
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /root/upload
a1.sources.r1.basenameHeader = true
a1.sources.r1.basenameHeaderKey = fileName

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://192.168.100.200:9000/flume/
a1.sinks.k1.hdfs.filePrefix = %{fileName}

# 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
a1.channels.c1.type = file 
  • 3 拷贝jar包
cp /root/hadoop-2.5.2/share/hadoop/common/*.jar /root/apache-flume-1.8.0-bin/lib/
cp /root/hadoop-2.5.2/share/hadoop/common/lib/*.jar /root/apache-flume-1.8.0-bin/lib/
  • 4 启动
# 进入flume启动目录
cd /root/apache-flume-1.8.0-bin/bin
# 启动flume并申明a1服务
./flume-ng agent -c ../conf -f ../conf/flume-conf.properties -n a1 -Dflume.root.logger=INFO,console
  • 5 测试
# 创建对应的文件夹
mkdir -p /root/upload

不断在/root/upload中加入新的文件夹及其文件,查看HDFS存储。
以上就是通过flume监听文件夹变化并上传文件到HDFS。

  • 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、付费专栏及课程。

余额充值