1. 程式人生 > >Flume的hdfsSink的roll引數不生效的原因(日誌上傳hdfs)

Flume的hdfsSink的roll引數不生效的原因(日誌上傳hdfs)

首先,本人菜雞一個,只是分享點東西出來,怕自己忘了,也給各位大佬填填坑噹噹墊背的!

事情是這樣的

測試:

通過exec作為一個源,將tail -f /opt/20171130.log這樣命令接收到的資料上傳到HDFS

先給個官網路徑:http://flume.apache.org/FlumeUserGuide.html

在官網這個頁面上,直接Ctrl+F,搜尋hdfs,就可以看到關於hdfs的sink有哪些引數可以配置,如下圖:

第一張圖,這個是配置生成的路徑的一些引數(大多數都是時間,比如生成的年月日時分秒之類的)


第二張圖,就是hdfs的sink可以配置的相關引數(其實也要注意下版本,看看有沒有新的一些可配置的引數)


這裡的引數其實後面都有說明,百度翻譯下應該是沒有問題

有幾個屬性稍微記錄下:

1hdfs.fileType:hdfs上的檔案型別,預設是二進位制,如果想要原封不動的保留你的字串型別的日誌,那麼使用DataStream,如果想要壓縮的話,使用CompressedStream,但是如果使用了這個屬性,記得要設定hdfs.codeC,指定一種壓縮格式gzip, bzip2, lzo, lzop, snappy

2、各種roll屬性:這些是用來這個flume儲存在hdfs上的檔案的大小的

首先說下,日誌上傳到hdfs,會先以tmp(臨時)檔案的形式儲存到hdfs上,等到達到某些閾值才會變成正式的檔案塊

hdfs.rollInterval,30

hdfs.rollSize,1024

hdfs.rollCount,10

以上這幾個值從三個維度來限定一個檔案塊的生成規則

首先是30s,

然後是1024byte(如果要1M一個檔案,那就是1024000),

最後是10條events

他們遵從(0 = never roll based on time interval),就是說如果你不想讓這個引數影響檔案的生成的話,就設定為0,否則就是預設值。

  其次其實如果你的副本數如果不為1的話,你會發現roll的配置不生效,是因為flume檢測到hdfs檔案塊的複製,然後強制終結了這次的tmp臨時檔案,生成新的,所以就會發現,roll的引數不生效

  解決方法:手動將hdfs.minBlockReplicas值設定為1,讓flume感應不到hdfs檔案塊的複製,備份數還是3,而且可以解決我們的問題!

如下給出一個案例:

####  defind  agent
Agent.sources=execSources
Agent.channels=fileChannel
Agent.sinks=hdfsSinks


####   defind  sources
Agent.sources.execSources.type=exec
Agent.sources.execSources.channels=fileChannel
Agent.sources.execSources.command=tail -f /opt/sink.log


####   defind  file channels
Agent.channels.fileChannel.type=file
Agent.channels.fileChannel.checkpointDir=/opt/cdh5/flume-1.5.0-cdh5.3.6/tmp/checkpointDir
Agent.channels.fileChannel.dataDirs=/opt/cdh5/flume-1.5.0-cdh5.3.6/tmp/dataDirs


####   defind hdfs sink
Agent.sinks.hdfsSinks.type=hdfs
Agent.sinks.hdfsSinks.channel=fileChannel
Agent.sinks.hdfsSinks.hdfs.path=hdfs://bigdata-03:8020/user/flume/efdlogs/%Y-%m/%d
Agent.sinks.hdfsSinks.hdfs.useLocalTimeStamp=true
Agent.sinks.hdfsSinks.hdfs.writeFormat=Text
Agent.sinks.hdfsSinks.hdfs.fileType=DataStream
Agent.sinks.hdfsSinks.hdfs.filePrefix=sink-log
Agent.sinks.hdfsSinks.hdfs.inUseSuffix=.tmp
Agent.sinks.hdfsSinks.hdfs.rollSize=1024000
Agent.sinks.hdfsSinks.hdfs.rollInterval=0
Agent.sinks.hdfsSinks.hdfs.rollCount=0



啟動flume的命令:bin/flume-ng agent --name Agent --conf ./conf/ --conf-file ./conf/exec-file-hdfs.conf.p -Dflume.root.logger=INFO,console

然後我是寫了個shell指令碼迴圈往sink.log裡面插入資料:

#!/bin/sh
i=0
while((i<10000))
do
echo "aalaansdlkanslabclkanslnclaksnuabcasjcbalsck" >> /opt/sink.log
i=$i+1
done

好了,結果大概是這樣,大家可以自己測試下這幾個引數的結合使用:

但是時間不太準。。。。。所以湊合著看看好了


在這裡也非常感謝,xuanxufeng這個博主所發的文章

(原文連結)http://www.aboutyun.com/thread-21365-1-1.html 

如果大家想研究為什麼設定成1就可以成功的話,可以看看這篇文章帶大家看的原始碼!!!

萬分感謝那些願意分享經驗的大神們,小弟學習了!