Flume日志收集系統介紹


轉自:http://blog.csdn.net/a2011480169/article/details/51544664

在具體介紹本文內容之前,先給大家看一下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組件就可以獲取到該文件中的內容。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM