我们已经了解了使用flume监听端口以及文件内容,本节将展示使用flume去监听一个文件夹并且上传其中的文件到HDFS。
1 系统、软件以及前提约束
- CentOS 7 64 工作站 作者的机子ip是192.168.100.200,请读者根据自己实际情况设置
- 已完成flume安装并监听端口
https://www.jianshu.com/p/3e4f7db8080f - 已完成HDFS安装并启动
https://www.jianshu.com/p/b7ae3b51e559 - xshell客户端
- 为去除权限对操作的影响,所有操作都以root进行
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。