一、安裝
1、必備條件
因為要把日志上傳到hdfs上,所以需要以下hadoop依賴包:
- commons-configuration-1.6
- hadoop-auth-2.5.0-cdh5.3.6
- hadoop-common-2.5.0-cdh5.3.6
- hadoop-hdfs-2.5.0-cdh5.3.6
2、安裝使用
# 講壓縮包解壓安裝到cdh目錄下,進入flume目錄進行測試:
bin/flume-ng version
# 查看flume都有哪些命令
bin/flume-ne
3、說明
本文中的代碼相應(yīng)位置使用的值如下,請根據(jù)個人情況自行修改:
Linux用戶:hadoop
解壓目錄:/home/hadoop/cdh
主機名:master
hdfs:hdfs://master:9000
hadoop目錄:
flume目錄:
hive目錄:
二、案例
打開 http://flume.apache.org ,左側(cè) Documentation > Flume User Guide
1、日志來源于端口的情況下conf的配置
參考官方文檔 Set up > A simple example
conf目錄下新建flume.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source 注意更換localhost為使用Telnet發(fā)送日志的主機名
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
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
配置好之后啟動flume
bin/flume-ng agent --conf conf --conf-file conf/flume.conf --name a1 -Dflume.root.logger=INFO,console
重新啟動一個控制臺,telnet連接之后發(fā)送任意字符,即可在啟動flume的控制臺看到發(fā)送的內(nèi)容
telnet localhost 44444
hello
如果沒有安裝Telnet,去下載以下三個文件:
- telnet-0.17-48.el6.x86_64
- telnet-server-0.17-48.el6.x86_64
- xinetd-2.3.14-40.el6.x86_64
# telnet 安裝
rpm -i *.rpm
# 重新啟動xinetd守護進程
service xinetd restart
2、日志來源于某個目錄
參考官方文檔 Configuration > Flume Source > Exec Source
conf目錄下新建一個exec.conf
## define agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1
## define sources
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/hadoop/cdh/test/text.log
a1.sources.r1.shell = /bin/bash -c
## define channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
## define sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://master:9000/user/hadoop/flume/hive-logs
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.batchSize = 10
## bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
配置好之后啟動flume
bin/flume-ng agent \
-c conf \
-n a1 \
-f conf/exec.conf \
-Dflume.root.logger=DEBUG,console
```
如果啟動失敗,那就是沒有把hadoop依賴放入lib目錄
> Failed to start agent because dependencies were not found in classpath.
通過向test.log里面寫入內(nèi)容來模擬日志生成過程,flume就會把text.log里面的內(nèi)容傳到HDFS上
```
# 新建input文件隨便寫入內(nèi)容,再新建text.log文件 通過以下命令模擬日志生成
cat input >> text.log
```
## 三、架構(gòu)
##### Flume架構(gòu)設(shè)計要點
Flume的架構(gòu)主要有一下幾個核心概念:
- Event:一個數(shù)據(jù)單元,帶有一個可選的消息頭
- Flow:Event從源點到達目的點的遷移的抽象
- Client:操作位于源點處的Event,將其發(fā)送到Flume Agent
- Agent:一個獨立的Flume進程,包含組件Source、Channel、Sink
- Source:用來消費傳遞到該組件的Event
- Channel:中轉(zhuǎn)Event的一個臨時存儲,保存有Source組件傳遞過來的Event
- Sink:從Channel中讀取并移除Event,將Event傳遞到Flow Pipeline中的下一個Agent(如果有的話)
Flume NG架構(gòu),如圖所示:
Flume之所以這么神奇,是源于它自身的一個設(shè)計,這個設(shè)計就是agent,agent本身是一個Java進程,運行在日志收集節(jié)點—所謂日志收集節(jié)點就是服務(wù)器節(jié)點。
agent里面包含3個核心的組件:source—->channel—–>sink,類似生產(chǎn)者、倉庫、消費者的架構(gòu)。
source:source組件是專門用來收集數(shù)據(jù)的,可以處理各種類型、各種格式的日志數(shù)據(jù),包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy、自定義。
channel:source組件把數(shù)據(jù)收集來以后,臨時存放在channel中,即channel組件在agent中是專門用來存放臨時數(shù)據(jù)的——對采集到的數(shù)據(jù)進行簡單的緩存,可以存放在memory、jdbc、file等等。 sink:
sink組件是用于把數(shù)據(jù)發(fā)送到目的地的組件,目的地包括hdfs、logger、avro、thrift、ipc、file、null、Hbase、solr、自定義。
## 四、理解
>
Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregating and moving large amounts of log data from many different sources to a centralized data store.
Flume NG是一個分布式、可靠、可用的系統(tǒng),它能夠?qū)⒉煌瑪?shù)據(jù)源的海量日志數(shù)據(jù)進行高效收集、聚合、移動,最后存儲到一個中心化數(shù)據(jù)存儲系統(tǒng)中。
## 五、應(yīng)用場景
多個Agent順序連接
可以將多個Agent順序連接起來,將最初的數(shù)據(jù)源經(jīng)過收集,存儲到最終的存儲系統(tǒng)中。這是最簡單的情況,一般情況下,應(yīng)該控制這種順序連接的Agent的數(shù)量,因為數(shù)據(jù)流經(jīng)的路徑變長了,如果不考慮failover的話,出現(xiàn)故障將影響整個Flow上的Agent收集服務(wù)。
多個Agent的數(shù)據(jù)匯聚到同一個Agent
這種情況應(yīng)用的場景比較多,比如要收集Web網(wǎng)站的用戶行為日志,Web網(wǎng)站為了可用性使用的負載均衡的集群模式,每個節(jié)點都產(chǎn)生用戶行為日志,可以為每個節(jié)點都配置一個Agent來單獨收集日志數(shù)據(jù),然后多個Agent將數(shù)據(jù)最終匯聚到一個用來存儲數(shù)據(jù)存儲系統(tǒng),如HDFS上。
多路(Multiplexing)Agent
這種模式,有兩種方式,一種是用來復(fù)制(Replication),另一種是用來分流(Multiplexing)。Replication方式,可以將最前端的數(shù)據(jù)源復(fù)制多份,分別傳遞到多個channel中,每個channel接收到的數(shù)據(jù)都是相同的,
實現(xiàn)load balance功能
Load balancing Sink Processor能夠?qū)崿F(xiàn)load balance功能,上圖Agent1是一個路由節(jié)點,負責(zé)將Channel暫存的Event均衡到對應(yīng)的多個Sink組件上,而每個Sink組件分別連接到一個獨立的Agent上
實現(xiàn)failover能
Failover Sink Processor能夠?qū)崿F(xiàn)failover功能,具體流程類似load balance,但是內(nèi)部處理機制與load balance完全不同:Failover Sink Processor維護一個優(yōu)先級Sink組件列表,只要有一個Sink組件可用,Event就被傳遞到下一個組件。如果一個Sink能夠成功處理Event,則會加入到一個Pool中,否則會被移出Pool并計算失敗次數(shù),設(shè)置一個懲罰因子,
## 參考文章
[Flume架構(gòu)以及應(yīng)用介紹](http://blog.csdn.net/a2011480169/article/details/51544664)
[Flume(NG)架構(gòu)設(shè)計要點及配置實踐](http://shiyanjun.cn/archives/915.html)
## Flume好文
[基于Flume的美團日志收集系統(tǒng)(一)架構(gòu)和設(shè)計](http://www.aboutyun.com/thread-8317-1-1.html)