1. 程式人生 > 實用技巧 >Flume案例:目錄檔案到HDFS

Flume案例:目錄檔案到HDFS

實時讀取目錄檔案到HDFS案例

1案例需求:使用flume監聽整個目錄的檔案

2需求分析:

建立一個檔案

[jason@hadoop102 job]$ vim flume-dir-hdfs.conf

新增如下內容

a3.sources = r3       #定義source

a3.sinks = k3         #定義sink

a3.channels = c3      #定義channel

 

# Describe/configure the source

a3.sources.r3.type = spooldir   #定義source型別:目錄

a3.sources.r3.spoolDir 
= /opt/module/flume/upload #定義監控目錄 a3.sources.r3.fileSuffix = .COMPLETED #定義檔案上傳成功,字尾 a3.sources.r3.fileHeader = true #是否有檔案頭 #忽略所有以.tmp結尾的檔案,不上傳 a3.sources.r3.ignorePattern = ([^ ]*\.tmp) # Describe the sink a3.sinks.k3.type = hdfs #sink型別hdfs a3.sinks.k3.hdfs.path
= hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H #上傳目標hdfs路徑 #上傳檔案的字首 a3.sinks.k3.hdfs.filePrefix = upload- #是否按照時間滾動資料夾 a3.sinks.k3.hdfs.round = true #多少時間單位建立一個新的資料夾 a3.sinks.k3.hdfs.roundValue = 1 #重新定義時間單位 a3.sinks.k3.hdfs.roundUnit = hour #是否使用本地時間戳 a3.sinks.k3.hdfs.useLocalTimeStamp = true #積攢多少個Event才flush到HDFS一次 a3.sinks.k3.hdfs.batchSize
= 100 #設定檔案型別,可支援壓縮 a3.sinks.k3.hdfs.fileType = DataStream #多久生成一個新的檔案(0表示不按照時間來分割檔案),單位:秒 a3.sinks.k3.hdfs.rollInterval = 600 #設定每個檔案的滾動大小大概是128M a3.sinks.k3.hdfs.rollSize = 134217700 #多少event生產新檔案,0表示:檔案的滾動與Event數量無關 a3.sinks.k3.hdfs.rollCount = 0 #多少個副本 a3.sinks.k3.hdfs.minBlockReplicas = 1 # Use a channel which buffers events in memory a3.channels.c3.type = memory a3.channels.c3.capacity = 1000 a3.channels.c3.transactionCapacity = 100 # Bind the source and sink to the channel a3.sources.r3.channels = c3 a3.sinks.k3.channel = c3

3)啟動監控資料夾命令

[jason@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf

說明:在使用Spooling Directory Source

  ① 不要在監控目錄中建立並持續修改檔案,在另一篇部落格中有處理方案(shell重新命名檔案)

  ②上傳完成的檔案會以.COMPLETED結尾

  ③被監控資料夾每600毫秒掃描一次檔案變動

4)upload資料夾中新增檔案

在/opt/module/flume目錄下建立upload檔案

[jason@hadoop102 flume]$ mkdir upload

向upload資料夾中新增檔案

[jason@hadoop102 upload]$ touch jason.txt

[jason@hadoop102 upload]$ touch jason.tmp

[jason@hadoop102 upload]$ touch jason.log

5)檢視HDFS上的資料

6)等待1s,再次查詢upload資料夾

[jason@hadoop102 upload]$ ll

總用量 0

-rw-rw-r--. 1 jason jason 0 5月  20 22:31 jason.log.COMPLETED

-rw-rw-r--. 1 jason jason 0 5月  20 22:31 jason.tmp

-rw-rw-r--. 1 jason jason 0 5月  20 22:31 jason.txt.COMPLETED