挑战介绍
在 Hadoop 处理数据之前,首先需要采集数据并且上传到集群中。本次挑战需要你使用 Flume 上传数据,来监测指定目录中文件的变化,一旦该目录有新文件时,就会把该文件自动地采集到 HDFS 上的指定目录里。
知识点
- Flume 配置文件编写
挑战准备
首先需要下载挑战使用的原始数据 a.txt
到环境的 /home/shiyanlou
目录中。
cd ~
wget https://labfile.oss.aliyuncs.com/courses/1379/a.txt
然后在 /home/shiyanlou
目录下新建 data
目录。
mkdir data
接下来在终端输入 start-all.sh
启动 Hadoop:
# 注意首次启动需要输入 yes
start-all.sh
启动完成后,输入 jps
查看集群是否成功启动,确保存在如下的进程:
最后在 /home/shiyanlou
目录下创建文件 spool.conf
。
cd ~
touch spool.conf
挑战目标
请根据要求,参考下面的 Flume 配置文件模板,补全其中 <code1>
、<code2>
和 <code3>
三处的配置,然后写入 sqool.conf
。
- Flume 模板文件:
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = <code1>
a1.sources.r1.spoolDir = <code2>
a1.sources.r1.fileHeader = true
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = <code3>
a1.sinks.k1.hdfs.filePrefix = log-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3
#设置每多少个个字节上传一次
a1.sinks.k1.hdfs.rollSize = 0
#设置每多少条数据上传一次
a1.sinks.k1.hdfs.rollCount = 3000
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
挑战要求
依照下面的要求编写 spool.conf
:
- 配置文件名称需为
spool.conf
且必须保存在/home/shiyanlou
目录下。 - 配置文件需要检测
/home/shiyanlou/data
目录中文件的变化,采集的数据需要保存在 HDFS 上的/flume
目录中。 - 挑战过程中务必保证 Hadoop 已经正确启动。
挑战验证
编写完成后使用如下命令启动 Flume:
flume-ng agent -n a1 -f /home/shiyanlou/spool.conf -Dflume.root.logger=INFO,console
启动 Flume 后新打开一个终端,将 a.txt
拷贝到 /home/shiyanlou/data
目录中。
cd ~
cp a.txt data
执行完成后可使用 hadoop fs -ls /flume
查看 HDFS 上是否已经有采集的数据文件。
成功采集到文件如下图所示:
来源:蓝桥(实验楼)
链接:https://www.lanqiao.cn/problems/86/learning/?is_contest=true
题解
简单的签到题,配置下路径即可。
sqool.conf
:
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /home/shiyanlou/data
a1.sources.r1.fileHeader = true
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = hdfs://127.0.0.1:9000/flume
a1.sinks.k1.hdfs.filePrefix = log-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3
#设置每多少个个字节上传一次
a1.sinks.k1.hdfs.rollSize = 0
#设置每多少条数据上传一次
a1.sinks.k1.hdfs.rollCount = 3000
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1