Flume單數據源多出口案例(選擇器)

寫在前面尖叫提示哈:本人也是大數據的初學者,寫博客的目的也就是儘快的掌握大數據技術,文章正難免出現不足,如有錯誤大家多多留言指出哈.....感激不盡。

此案例是一個數據源,但是有兩個或者兩個以上的channel和Sink的案例,爲簡單起見,現在有兩個輸出源,一個是Hdfs文件系統,另一個是本地文件系統,多個輸出依次擴展即可,先貼一張圖說明問題:圖片是從官網扒下來的,,,hahaha

需求分析:使用Flume-1監控文件變動,Flume-1將變動內容傳遞給Flume-2,Flume-2負責存儲到HDFS。同時Flume-1將變動內容傳遞給Flume-3,Flume-3負責輸出到Local FileSystem。

 

由於輸入源在接收到數據以後不是立刻分發給channel的,中間還會經過xhannel選擇器進行選擇或者過濾,所以此案例source收到數據以後會先把數據發送到channel選擇器,然後選擇器負責選擇分發給哪一個channel緩存數據。此案例中一共有3個agent,其中還有一個輸入source,兩個channel和兩個sink,稍後會在配置文件中進行配置。

第一步:在flume目錄下面創建job文件夾,然後在文件夾裏面創建第一個配置文件flume-file-flume.conf,用於配置source,並且添加如下配置內容:

#在這裏有一個輸入源r1,兩個緩存通道channel,c1,c2,兩個輸出源,k1,k2
# Name the components on this agent
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 注意:這裏將數據複製給所有channel
a1.sources.r1.selector.type = replicating

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/hive/logs/hive.log//數據的來源,hive輸出的日誌
a1.sources.r1.shell = /bin/bash -c

# Describe the sink
# sink端的avro是一個數據發送者
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop101
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop101
a1.sinks.k2.port = 4142

# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
//注意這裏有兩個channel,稍後會看到如何連接
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
/*注:Avro是由Hadoop創始人Doug Cutting創建的一種語言無關的數據序列化和RPC框架。
注:RPC(Remote Procedure Call)—遠程過程調用,它是一種通過網絡從遠程計算機程序上請求服務,而不需要了解底層網絡技術的協議。*/

第二步:創建flume-flume-hdfs.conf,此配置文件配置輸出到hdfs文件系統上

# Name the components on this agent
a2.sources = r1
a2.sinks = k1
a2.channels = c1

# Describe/configure the source
# source端的avro是一個數據接收服務
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop101
a2.sources.r1.port = 4141

# Describe the sink
a2.sinks.k1.type = hdfs
#在這裏可以指定輸出到hdfs文件系統上的路徑
a2.sinks.k1.hdfs.path = hdfs://hadoop101:9000/rui/user/flumeFile/%Y%m%d/%H
#上傳文件的前綴
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照時間滾動文件夾
a2.sinks.k1.hdfs.round = true
#多少時間單位創建一個新的文件夾
a2.sinks.k1.hdfs.roundValue = 1
#重新定義時間單位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地時間戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a2.sinks.k1.hdfs.batchSize = 100
#設置文件類型,可支持壓縮
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#設置每個文件的滾動大小大概是128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a2.sinks.k1.hdfs.rollCount = 0

# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100
#此處請注意是如何連接的
# Bind the source and sink to the channel
a2.sources.r1.channels = c1
a2.sinks.k1.channel = c1

 第三步:創建輸出到本地文件系統的配置文件flume-flume-dir.conf,輸入以下配置信息

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2

# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop101
a3.sources.r1.port = 4142

# Describe the sink
a3.sinks.k1.type = file_roll
#在這裏可以配置輸出到本地文件的目錄,
#提示:輸出的本地目錄必須是已經存在的目錄,如果該目錄不存在,並不會創建新的目錄。
a3.sinks.k1.sink.directory = /opt/module/data/flume3

# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2

第四步:好了,現在我們一次啓動接收端的進程,然後在啓動輸入端的監聽進程

//啓動輸出到本地文件系統的進程
bin/flume-ng agent -c conf/ -n a3 -f /opt/module/flume-1.7.0/jobs/flume-f
//啓動輸出到hdfs文件系統的進程
 bin/flume-ng agent -c conf/ -n a2 -f /opt/module/flume-1.7.0/jobs/flume-f
//啓動監控hive日誌的進程
bin/flume-ng agent -c conf/ -n a1 -f /opt/module/flume-1.7.0/jobs/flume-file-flume.conf

好了,最後我們在啓動Hive就可歐克了!最後查看一下本地文件系統或者HDFs文件系統是不是發現有很多日誌文件呢?如果只是僅僅啓動hive的話可能只有一處有文件,反正我只是在本地看到了日誌哈。

 

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