1. 程式人生 > 其它 >藍橋樓賽第24期-大資料-使用Flume採集資料 題解

藍橋樓賽第24期-大資料-使用Flume採集資料 題解

技術標籤:hadoopflume題解hadoopflumehdfs

挑戰介紹

在 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