Flume簡單案例的實現(完整)

案例一 :採集目錄到HDFS

採集需求:服務器的某特定目錄下,會不斷產生新的文件,每當有新文件出現,就需要把文件採集到HDFS中去

根據需求,首先定義以下3大要素

  • 採集源,即source——監控文件目錄 : spooldir
  • 下沉目標,即sink——HDFS文件系統 : hdfs sink
  • source和sink之間的傳遞通道——channel,可用file channel 也可以用內存channel

1. flume配置文件開發

mkdir -p /root/logs
cd /export/servers/flume1.8.0/conf
vim spooldir.conf

添加:

# 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數量

其中: a1.sources.r1.spoolDir 爲監控的目錄!!!

2.啓動flume:

bin/flume-ng agent -c ./conf -f ./conf/spooldir.conf -n a1 -Dflume.root.logger=INFO,console

3.上傳文件到指定目錄

將不同的文件上傳到下面目錄裏面去,注意文件不能重名
重名後,程序會中斷!!!!

cd /root/logs

在logs文件夾中添加文件:

touch a.txt

可以看到監控 控制檯打印數據:

2020-04-06 21:25:14,997 (pool-5-thread-1) [INFO - org.apache.flume.client.avro.ReliableSpoolingFileEventReader.readEvents(ReliableSpoolingFileEventReader.java:324)] Last read took us just up to a file boundary. Rolling to the next file, if there is one.
2020-04-06 21:25:14,998 (pool-5-thread-1) [INFO - org.apache.flume.client.avro.ReliableSpoolingFileEventReader.rollCurrentFile(ReliableSpoolingFileEventReader.java:433)] Preparing to move file /root/logs/a.txt to /root/logs/a.txt.COMPLETED
2020-04-06 21:25:17,127 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.HDFSDataStream.configure(HDFSDataStream.java:57)] Serializer = TEXT, UseRawLocalFileSystem = false
2020-04-06 21:25:17,338 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.BucketWriter.open(BucketWriter.java:251)] Creating /flume/events/20-04-06/2120//events-.1586179517128.tmp
2020-04-06 21:25:21,201 (hdfs-k1-roll-timer-0) [INFO - org.apache.flume.sink.hdfs.BucketWriter.close(BucketWriter.java:393)] Closing /flume/events/20-04-06/2120//events-.1586179517128.tmp

效果:
在這裏插入圖片描述

案例二 : 採集文件到HDFS

監控文件內容的變化!!

採集需求:比如業務系統使用log4j生成的日誌,日誌內容不斷增加,需要把追加到日誌文件中的數據實時採集到hdfs

根據需求,首先定義以下3大要素

  • 採集源,即source——監控文件內容更新 : exec ‘tail -F file’
  • 下沉目標,即sink——HDFS文件系統 : hdfs sink
  • Source和sink之間的傳遞通道——channel,可用file channel 也可以用 內存channel

1.編寫配置文件:

# 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分鐘新生成一個。

模擬,本地文件,動態寫入:
將時間動態寫入test.log的文件:

while true; do date >> /root/logs/test.log;done

或者:

#!/bin/bash
while true
do
 date >> /export/servers/taillogs/access_log;
  sleep 0.5;
done

[root@node01 logs]# while true; do date >> /root/logs/test.log;done

查看是否動態寫入:

[root@node01 ~]# tail -f /root/logs/test.log

啓動flume:

bin/flume-ng agent -c conf -f conf/tail-file.conf -n a1  -Dflume.root.logger=INFO,console

注意: 這裏的配置文件名爲: tail-file.conf

效果:

在這裏插入圖片描述
在這裏插入圖片描述

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