hadoop 之flume 日誌收集系統,及與kafka相似與不同處

flume主要用於日誌採集,其中的agent裏面包含3個核心的組件:source(採集/輸入)—->channel(緩存/管道)—–>sink(輸出),類似生產者、倉庫、消費者的架構。 
source:source組件是專門用來收集數據的,可以處理各種類型、各種格式的日誌數據,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy、自定義。 
channel:source組件把數據收集來以後,臨時存放在channel中,即channel組件在agent中是專門用來存放臨時數據的——對採集到的數據進行簡單的緩存,可以存放在memory、jdbc、file等等。 
sink:sink組件是用於把數據發送到目的地的組件,目的地包括hdfs、logger、avro、thrift、ipc、file、null、hbase、solr、自定義。

Flume與Kafka對比可參看文章末尾部分

Flume

在具體介紹本文內容之前,先給大家看一下Hadoop業務的整體開發流程: 

è¿éåå¾çæè¿°

從Hadoop的業務開發流程圖中可以看出,在大數據的業務處理過程中,對於數據的採集是十分重要的一步,也是不可避免的一步,從而引出我們本文的主角—Flume。本文將圍繞Flume的架構、Flume的應用(日誌採集)進行詳細的介紹。 
(一)Flume架構介紹 
1、Flume的概念 
 è¿éåå¾çæè¿°
flume是分佈式的日誌收集系統,它將各個服務器中的數據收集起來並送到指定的地方去,比如說送到圖中的HDFS,簡單來說flume就是收集日誌的。 
2、Event的概念 
在這裏有必要先介紹一下flume中event的相關概念:flume的核心是把數據從數據源(source)收集過來,在將收集到的數據送到指定的目的地(sink)。爲了保證輸送的過程一定成功,在送到目的地(sink)之前,會先緩存數據(channel),待數據真正到達目的地(sink)後,flume在刪除自己緩存的數據。 
在整個數據的傳輸的過程中,流動的是event,即事務保證是在event級別進行的。那麼什麼是event呢?—–event將傳輸的數據進行封裝,是flume傳輸數據的基本單位,如果是文本文件,通常是一行記錄,event也是事務的基本單位。event從source,流向channel,再到sink,本身爲一個字節數組,並可攜帶headers(頭信息)信息。event代表着一個數據的最小完整單元,從外部數據源來,向外部的目的地去。 
爲了方便大家理解,給出一張event的數據流向圖: 
 è¿éåå¾çæè¿°
一個完整的event包括:event headers、event body、event信息(即文本文件中的單行記錄),如下所以: 

è¿éåå¾çæè¿°

其中event信息就是flume收集到的日記記錄。 
3、flume架構介紹 
flume之所以這麼神奇,是源於它自身的一個設計,這個設計就是agent,agent本身是一個java進程,運行在日誌收集節點—所謂日誌收集節點就是服務器節點。 
agent裏面包含3個核心的組件:source—->channel—–>sink,類似生產者、倉庫、消費者的架構。 
source:source組件是專門用來收集數據的,可以處理各種類型、各種格式的日誌數據,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy、自定義。 
channel:source組件把數據收集來以後,臨時存放在channel中,即channel組件在agent中是專門用來存放臨時數據的——對採集到的數據進行簡單的緩存,可以存放在memory、jdbc、file等等。 
sink:sink組件是用於把數據發送到目的地的組件,目的地包括hdfs、logger、avro、thrift、ipc、file、null、hbase、solr、自定義。 

4、flume的運行機制 
flume的核心就是一個agent,這個agent對外有兩個進行交互的地方,一個是接受數據的輸入——source,一個是數據的輸出sink,sink負責將數據發送到外部指定的目的地。source接收到數據之後,將數據發送給channel,chanel作爲一個數據緩衝區會臨時存放這些數據,隨後sink會將channel中的數據發送到指定的地方—-例如HDFS等,注意:只有在sink將channel中的數據成功發送出去之後,channel纔會將臨時數據進行刪除,這種機制保證了數據傳輸的可靠性與安全性。 
5、flume的廣義用法 
flume之所以這麼神奇—-其原因也在於flume可以支持多級flume的agent,即flume可以前後相繼,例如sink可以將數據寫到下一個agent的source中,這樣的話就可以連成串了,可以整體處理了。flume還支持扇入(fan-in)、扇出(fan-out)。所謂扇入就是source可以接受多個輸入,所謂扇出就是sink可以將數據輸出多個目的地destination中。 
 è¿éåå¾çæè¿°
(二)flume應用—日誌採集 
對於flume的原理其實很容易理解,我們更應該掌握flume的具體使用方法,flume提供了大量內置的Source、Channel和Sink類型。而且不同類型的Source、Channel和Sink可以自由組合—–組合方式基於用戶設置的配置文件,非常靈活。比如:Channel可以把事件暫存在內存裏,也可以持久化到本地硬盤上。Sink可以把日誌寫入HDFS, HBase,甚至是另外一個Source等等。下面我將用具體的案例詳述flume的具體用法。 
其實flume的用法很簡單—-書寫一個配置文件,在配置文件當中描述source、channel與sink的具體實現,而後運行一個agent實例,在運行agent實例的過程中會讀取配置文件的內容,這樣flume就會採集到數據。 
配置文件的編寫原則: 
1>從整體上描述代理agent中sources、sinks、channels所涉及到的組件

 

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



2>詳細描述agent中每一個source、sink與channel的具體實現:即在描述source的時候,需要 
指定source到底是什麼類型的,即這個source是接受文件的、還是接受http的、還是接受thrift 
的;對於sink也是同理,需要指定結果是輸出到HDFS中,還是Hbase中啊等等;對於channel 
需要指定是內存啊,還是數據庫啊,還是文件啊等等。   

# Describe/configure the source
    a1.sources.r1.type = netcat
    a1.sources.r1.bind = localhost
    a1.sources.r1.port = 44444

    # Describe the sink
    a1.sinks.k1.type = logger

    # Use a channel which buffers events in memory
    a1.channels.c1.type = memory
    a1.channels.c1.capacity = 1000
    a1.channels.c1.transactionCapacity = 100


3>通過channel將source與sink連接起來

   

 # Bind the source and sink to the channel
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1



啓動agent的shell操作:

   

flume-ng  agent -n a1  -c  ../conf   -f  ../conf/example.file  
    -Dflume.root.logger=DEBUG,console  



參數說明: -n 指定agent名稱(與配置文件中代理的名字相同) 
-c 指定flume中配置文件的目錄 
-f 指定配置文件 
-Dflume.root.logger=DEBUG,console 設置日誌等級

具體案例: 
案例1: NetCat Source:監聽一個指定的網絡端口,即只要應用程序向這個端口裏面寫數據,這個source組件就可以獲取到信息。 其中 Sink:logger Channel:memory 
flume官網中NetCat Source描述:

Property Name Default     Description
channels       –     
type           –     The component type name, needs to be netcat
bind           –  日誌需要發送到的主機名或者Ip地址,該主機運行着netcat類型的source在監聽          
port           –  日誌需要發送到的端口號,該端口號要有netcat類型的source在監聽

     

a) 編寫配置文件:

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

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = 192.168.80.80
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = logger

# 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


b) 啓動flume agent a1 服務端

flume-ng  agent -n a1  -c ../conf  -f ../conf/netcat.conf   -Dflume.root.logger=DEBUG,console



c) 使用telnet發送數據

telnet  192.168.80.80  44444  big data world!(windows中運行的)



d) 在控制檯上查看flume收集到的日誌數據: 

è¿éåå¾çæè¿°
案例2:NetCat Source:監聽一個指定的網絡端口,即只要應用程序向這個端口裏面寫數據,這個source組件就可以獲取到信息。 其中 Sink:hdfs Channel:file (相比於案例1的兩個變化) 
flume官網中HDFS Sink的描述: 

è¿éåå¾çæè¿°

a) 編寫配置文件:

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

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = 192.168.80.80
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop80:9000/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1


b) 啓動flume agent a1 服務端

 

flume-ng  agent -n a1  -c ../conf  -f ../conf/netcat.conf   -Dflume.root.logger=DEBUG,console


c) 使用telnet發送數據

telnet  192.168.80.80  44444  big data world!(windows中運行的)



d) 在HDFS中查看flume收集到的日誌數據: 
 è¿éåå¾çæè¿°
案例3:Spooling Directory Source:監聽一個指定的目錄,即只要應用程序向這個指定的目錄中添加新的文件,source組件就可以獲取到該信息,並解析該文件的內容,然後寫入到channle。寫入完成後,標記該文件已完成或者刪除該文件。其中 Sink:

logger Channel:memory 
flume官網中Spooling Directory Source描述:

Property Name       Default      Description
channels              –  
type                  –          The component type name, needs to be spooldir.
spoolDir              –          Spooling Directory Source監聽的目錄
fileSuffix         .COMPLETED    文件內容寫入到channel之後,標記該文件
deletePolicy       never         文件內容寫入到channel之後的刪除策略: never or immediate
fileHeader         false         Whether to add a header storing the absolute path filename.
ignorePattern      ^$           Regular expression specifying which files to ignore (skip)
interceptors          –          指定傳輸中event的head(頭信息),常用timestamp


Spooling Directory Source的兩個注意事項:

①If a file is written to after being placed into the spooling directory, Flume will print an error to its log file and stop processing.
即:拷貝到spool目錄下的文件不可以再打開編輯
②If a file name is reused at a later time, Flume will print an error to its log file and stop processing.
即:不能將具有相同文件名字的文件拷貝到這個目錄下


a) 編寫配置文件:

# 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 = /usr/local/datainput
a1.sources.r1.fileHeader = true
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

# Describe the sink
a1.sinks.k1.type = logger

# 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


b) 啓動flume agent a1 服務端

 

flume-ng  agent -n a1  -c ../conf  -f ../conf/spool.conf   -Dflume.root.logger=DEBUG,console


c) 使用cp命令向Spooling Directory 中發送數據

 

cp datafile  /usr/local/datainput   (注:datafile中的內容爲:big data world!)


d) 在控制檯上查看flume收集到的日誌數據: 

è¿éåå¾çæè¿°

從控制檯顯示的結果可以看出event的頭信息中包含了時間戳信息。 
同時我們查看一下Spooling Directory中的datafile信息—-文件內容寫入到channel之後,該文件被標記了:

 

[root@hadoop80 datainput]# ls
datafile.COMPLETED


案例4:Spooling Directory Source:監聽一個指定的目錄,即只要應用程序向這個指定的目錄中添加新的文件,source組件就可以獲取到該信息,並解析該文件的內容,然後寫入到channle。寫入完成後,標記該文件已完成或者刪除該文件。 其中 Sink:hdfs Channel:file (相比於案例3的兩個變化)

a) 編寫配置文件:

# 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 = /usr/local/datainput
a1.sources.r1.fileHeader = true
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

# Describe the sink
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop80:9000/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1


b) 啓動flume agent a1 服務端

flume-ng  agent -n a1  -c ../conf  -f ../conf/spool.conf   -Dflume.root.logger=DEBUG,console



c) 使用cp命令向Spooling Directory 中發送數據

 

cp datafile  /usr/local/datainput   (注:datafile中的內容爲:big data world!)



d) 在控制檯上可以參看sink的運行進度日誌: 

è¿éåå¾çæè¿°

d) 在HDFS中查看flume收集到的日誌數據: 

 è¿éåå¾çæè¿°

è¿éåå¾çæè¿°
從案例1與案例2、案例3與案例4的對比中我們可以發現:flume的配置文件在編寫的過程中是非常靈活的。

案例5:Exec Source:監聽一個指定的命令,獲取一條命令的結果作爲它的數據源 
常用的是tail -F file指令,即只要應用程序向日志(文件)裏面寫數據,source組件就可以獲取到日誌(文件)中最新的內容 。 其中 Sink:hdfs Channel:file 
這個案列爲了方便顯示Exec Source的運行效果,結合Hive中的external table進行來說明。

a) 編寫配置文件:

# 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 /usr/local/log.file

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop80:9000/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1


b)在hive中建立外部表—–hdfs://hadoop80:9000/dataoutput的目錄,方便查看日誌捕獲內容

 

hive> create external table t1(infor  string)
    > row format delimited
    > fields terminated by '\t'
    > location '/dataoutput/';
OK
Time taken: 0.284 seconds


c) 啓動flume agent a1 服務端

flume-ng  agent -n a1  -c ../conf  -f ../conf/exec.conf   -Dflume.root.logger=DEBUG,console



d) 使用echo命令向/usr/local/datainput 中發送數據

 

echo  big data > log.file



d) 在HDFS和Hive分別中查看flume收集到的日誌數據: 

è¿éåå¾çæè¿°

hive> select * from t1;
OK
big data
Time taken: 0.086 seconds


e)使用echo命令向/usr/local/datainput 中在追加一條數據

echo big data world! >> log.file



d) 在HDFS和Hive再次分別中查看flume收集到的日誌數據: 

è¿éåå¾çæè¿°

è¿éåå¾çæè¿°

hive> select * from t1;
OK
big data
big data world!
Time taken: 0.511 seconds


總結Exec source:Exec source和Spooling Directory Source是兩種常用的日誌採集的方式,其中Exec source可以實現對日誌的實時採集,Spooling Directory Source在對日誌的實時採集上稍有欠缺,儘管Exec source可以實現對日誌的實時採集,但是當Flume不運行或者指令執行出錯時,Exec source將無法收集到日誌數據,日誌會出現丟失,從而無法保證收集日誌的完整性。

案例6:Avro Source:監聽一個指定的Avro 端口,通過Avro 端口可以獲取到Avro client發送過來的文件 。即只要應用程序通過Avro 端口發送文件,source組件就可以獲取到該文件中的內容。 其中 Sink:hdfs Channel:file 
(注:Avro和Thrift都是一些序列化的網絡端口–通過這些網絡端口可以接受或者發送信息,Avro可以發送一個給定的文件給Flume,Avro 源使用AVRO RPC機制) 
Avro Source運行原理如下圖: 
 è¿éåå¾çæè¿°
flume官網中Avro Source的描述:

Property     Name   Default Description
channels      –  
type          –     The component type name, needs to be avro
bind          –     日誌需要發送到的主機名或者ip,該主機運行着ARVO類型的source
port          –     日誌需要發送到的端口號,該端口要有ARVO類型的source在監聽


1)編寫配置文件

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

# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.bind = 192.168.80.80
a1.sources.r1.port = 4141

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop80:9000/dataoutput
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize = 0
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.filePrefix = %Y-%m-%d-%H-%M-%S
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# Use a channel which buffers events in file
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /usr/flume/checkpoint
a1.channels.c1.dataDirs = /usr/flume/data

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1


b) 啓動flume agent a1 服務端

flume-ng  agent -n a1  -c ../conf  -f ../conf/avro.conf   -Dflume.root.logger=DEBUG,console



c)使用avro-client發送文件

flume-ng avro-client -c  ../conf  -H 192.168.80.80  -p 4141 -F /usr/local/log.file



注:log.file文件中的內容爲:

[root@hadoop80 local]# more log.file
big data
big data world!


d) 在HDFS中查看flume收集到的日誌數據: 

è¿éåå¾çæè¿°

è¿éåå¾çæè¿°

è¿éåå¾çæè¿°
通過上面的幾個案例,我們可以發現:flume配置文件的書寫是相當靈活的—-不同類型的Source、Channel和Sink可以自由組合!

最後對上面用的幾個flume source進行適當總結: 
① NetCat Source:監聽一個指定的網絡端口,即只要應用程序向這個端口裏面寫數據,這個source組件 
就可以獲取到信息。 
②Spooling Directory Source:監聽一個指定的目錄,即只要應用程序向這個指定的目錄中添加新的文 
件,source組件就可以獲取到該信息,並解析該文件的內容,然後寫入到channle。寫入完成後,標記 
該文件已完成或者刪除該文件。 
③Exec Source:監聽一個指定的命令,獲取一條命令的結果作爲它的數據源 
常用的是tail -F file指令,即只要應用程序向日志(文件)裏面寫數據,source組件就可以獲取到日誌(文件)中最新的內容 。 
④Avro Source:監聽一個指定的Avro 端口,通過Avro 端口可以獲取到Avro client發送過來的文件 。即只要應用程序通過Avro 端口發送文件,source組件就可以獲取到該文件中的內容。

Flume與Kafka對比

kafka和flume都是日誌系統,kafka是分佈式消息中間件,自帶存儲,提供push和pull存取數據功能。flume分爲agent(數據採集器),collector(數據簡單處理和寫入),storage(存儲器)三部分,每一部分都是可以定製的。比如agent採用RPC(Thrift-RPC)、text(文件)等,storage指定用hdfs做。
kafka做日誌緩存應該是更爲合適的,但是 flume的數據採集部分做的很好,可以定製很多數據源,減少開發量。所以比較流行flume+kafka模式,如果爲了利用flume寫hdfs的能力,也可以採用kafka+flume的方式。


採集層 主要可以使用Flume, Kafka兩種技術。

Flume:Flume 是管道流方式,提供了很多的默認實現,讓用戶通過參數部署,及擴展API.

Kafka:Kafka是一個可持久化的分佈式的消息隊列。

Kafka 是一個非常通用的系統。你可以有許多生產者和很多的消費者共享多個主題Topics。相比之下,Flume是一個專用工具被設計爲旨在往HDFS,HBase發送數據。它對HDFS有特殊的優化,並且集成了Hadoop的安全特性。所以,Cloudera 建議如果數據被多個系統消費的話,使用kafka;如果數據被設計給Hadoop使用,使用Flume。

 

正如你們所知Flume內置很多的source和sink組件。然而,Kafka明顯有一個更小的生產消費者生態系統,並且Kafka的社區支持不好。希望將來這種情況會得到改善,但是目前:使用Kafka意味着你準備好了編寫你自己的生產者和消費者代碼。如果已經存在的Flume Sources和Sinks滿足你的需求,並且你更喜歡不需要任何開發的系統,請使用Flume。

 

Flume可以使用攔截器實時處理數據。這些對數據屏蔽或者過量是很有用的。Kafka需要外部的流處理系統才能做到。

 

Kafka和Flume都是可靠的系統,通過適當的配置能保證零數據丟失。然而,Flume不支持副本事件。於是,如果Flume代理的一個節點崩潰了,即使使用了可靠的文件管道方式,你也將丟失這些事件直到你恢復這些磁盤。如果你需要一個高可靠行的管道,那麼使用Kafka是個更好的選擇。

 

Flume和Kafka可以很好地結合起來使用。如果你的設計需要從Kafka到Hadoop的流數據,使用Flume代理並配置Kafka的Source讀取數據也是可行的:你沒有必要實現自己的消費者。你可以直接利用Flume與HDFS及HBase的結合的所有好處。你可以使用Cloudera Manager對消費者的監控,並且你甚至可以添加攔截器進行一些流處理。

Flume和Kafka可以結合起來使用。通常會使用Flume + Kafka的方式。其實如果爲了利用Flume已有的寫HDFS功能,也可以使用Kafka + Flume的方式。
 

原文:https://blog.csdn.net/a2011480169/article/details/51544664 

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