Flume簡單案例

1. 採集目錄到HDFS
採集需求:服務器的某特定目錄下,會不斷產生新的文件,每當有新文件出現,就需要把文件採集到HDFS中去
根據需求,首先定義以下3大要素
l 採集源,即source——監控文件目錄 : spooldir
l 下沉目標,即sink——HDFS文件系統 : hdfs sink
l source和sink之間的傳遞通道——channel,可用file channel 也可以用內存channel
配置文件編寫:

Name the components on this agent

a1.sources = r1
a1.sinks = k1
a1.channels = c1

Describe/configure the source

##注意:不能往監控目中重複丟同名文件
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /root/logs
a1.sources.r1.fileHeader = true

Describe the sink

a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = events-
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 = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件類型,默認是Sequencefile,可用DataStream,則爲普通文本
a1.sinks.k1.hdfs.fileType = DataStream

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
Channel參數解釋:
capacity:默認該通道中最大的可以存儲的event數量
trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數量
2. 採集文件到HDFS
採集需求:比如業務系統使用log4j生成的日誌,日誌內容不斷增加,需要把追加到日誌文件中的數據實時採集到hdfs
根據需求,首先定義以下3大要素
l 採集源,即source——監控文件內容更新 : exec ‘tail -F file’
l 下沉目標,即sink——HDFS文件系統 : hdfs sink
l Source和sink之間的傳遞通道——channel,可用file channel 也可以用 內存channel
配置文件編寫:

Name the components on this agent

a1.sources = r1
a1.sinks = k1
a1.channels = c1

Describe/configure the source

a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/test.log
a1.sources.r1.channels = c1

Describe the sink

a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = events-
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 = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件類型,默認是Sequencefile,可用DataStream,則爲普通文本
a1.sinks.k1.hdfs.fileType = DataStream

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

參數解析:
· rollInterval
默認值:30
hdfs sink間隔多長將臨時文件滾動成最終目標文件,單位:秒;
如果設置成0,則表示不根據時間來滾動文件;
注:滾動(roll)指的是,hdfs sink將臨時文件重命名成最終目標文件,並新打開一個臨時文件來寫入數據;
· rollSize
默認值:1024
當臨時文件達到該大小(單位:bytes)時,滾動成目標文件;
如果設置成0,則表示不根據臨時文件大小來滾動文件;
· rollCount
默認值:10
當events數據達到該數量時候,將臨時文件滾動成目標文件;
如果設置成0,則表示不根據events數據來滾動文件;
· round
默認值:false
是否啓用時間上的“捨棄”,這裏的“捨棄”,類似於“四捨五入”。
· roundValue
默認值:1
時間上進行“捨棄”的值;
· roundUnit
默認值:seconds
時間上進行“捨棄”的單位,包含:second,minute,hour
示例:
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
當時間爲2015-10-16 17:38:59時候,hdfs.path依然會被解析爲:
/flume/events/20151016/17:30/00
因爲設置的是捨棄10分鐘內的時間,因此,該目錄每10分鐘新生成一個。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章