1)簡介
Flume是一個分布式、可靠、和高可用的海量日志聚合的系統,支援在系統中定制各類資料發送方,用于收集資料;同時,Flume提供對資料進行簡單處理,并寫到各種資料接受方(可定制)的能力。
設計目标:
(1) 可靠性
當節點出現故障時,日志能夠被傳送到其他節點上而不會丢失。Flume提供了三種級别的可靠性保障,從強到弱依次分别為:end-to-end(收到資料agent首先将event寫到磁盤上,當資料傳送成功後,再删除;如果資料發送失敗,可以重新發送。),Store on failure(當資料接收方crash時,将資料寫到本地,待恢複後,繼續發送),Best effort(資料發送到接收方後,不會進行确認)。
(2) 可擴充性
Flume采用了三層架構,分别為agent,collector和storage,每一層均可以水準擴充。其中,所有agent和collector由master統一管理,這使得系統容易監控和維護,且master允許有多個(使用ZooKeeper進行管理和負載均衡),這就避免了單點故障問題。
(3) 可管理性
所有agent和colletor由master統一管理,這使得系統便于維護。多master情況,Flume利用ZooKeeper和gossip,保證動态配置資料的一緻性。使用者可以在master上檢視各個資料源或者資料流執行情況,且可以對各個資料源配置和動态加載。Flume提供了web 和shell script command兩種形式對資料流進行管理。
(4) 功能可擴充性
使用者可以根據需要添加自己的agent,collector或者storage。此外,Flume自帶了很多元件,包括各種agent(file, syslog等),collector和storage(File,HDFS,HBase等)。
2)配置
之前配置過Hadoop和hbase,是以需要先将hadoop和hbase啟動,才能将檔案寫入hdfs和hbase。hadoop-2.2.0和hbase-0.96.0的配置分别參考《Ubuntu和CentOS中分布式配置Hadoop-2.2.0》 http://www.linuxidc.com/Linux/2014-01/95799.htm 和《CentOS分布式環境安裝HBase-0.96.0》 http://www.linuxidc.com/Linux/2014-01/95801.htm 。
本次配置環境為兩台裝有centos 的測試叢集。主機名為master的機器負責收集日志,主機名為node的機器負責日志的寫入,本次配置的寫入方式有三種:寫入普通目錄,寫入hdfs。
首先下載下傳flume-ng的二進制壓縮檔案。位址:http://flume.apache.org/download.html。下載下傳好後,解壓檔案。首先編輯/etc/profile檔案,在其中添加如下幾行:
export FLUME_HOME=/home/aaron/apache-flume-1.4.0-bin
export FLUME_CONF_DIR=$FLUME_HOME/conf
export PATH=$PATH:$FLUME_HOME/bin
添加好之後記得運作$ souce /etc/profile指令使修改生效。
在master的flume檔案夾的conf目錄中,建立一個flume-master.conf檔案,内容如下:
agent.sources = seqGenSrc
agent.channels = memoryChannel
agent.sinks = remoteSink
# For each one of the sources, the type is defined
agent.sources.seqGenSrc.type = exec
agent.sources.seqGenSrc.command = tail -F /home/aaron/test
# The channel can be defined as follows.
agent.sources.seqGenSrc.channels = memoryChannel
# Each sink's type must be defined
agent.sinks.loggerSink.type = logger
#Specify the channel the sink should use
agent.sinks.loggerSink.channel = memoryChannel
# Each channel's type is defined.
agent.channels.memoryChannel.type = memory
# Other config values specific to each type of channel(sink or source)
# can be defined as well
# In this case, it specifies the capacity of the memory channel
agent.channels.memoryChannel.capacity = 100
agent.channels.memoryChannel.keep-alive = 100
agent.sinks.remoteSink.type = avro
agent.sinks.remoteSink.hostname = node
agent.sinks.remoteSink.port = 23004
agent.sinks.remoteSink.channel = memoryChannel
在node機器上也将/etc/profile檔案添加上面的配置。然後,在conf中建立一個flume-node.conf檔案,修改如下:
agent.sources = seqGenSrc1
#agent.sinks = fileSink
agent.sinks = <SPANstyle="FONT-FAMILY: Arial, Helvetica, sans-serif">fileSink</SPAN>
agent.sources.seqGenSrc1.type = avro
agent.sources.seqGenSrc1.bind = node
agent.sources.seqGenSrc1.port = 23004
agent.sources.seqGenSrc1.channels = memoryChannel
agent.sources.flieSink.type = avro
agent.sources.fileSink.channel = memoryChannel
agent.sources.fileSink.sink.directory = /home/aaron/
agent.sources.fileSink.serializer.appendNewline = true
在master上面運作指令:
$ bin/flume-ng agent --conf ./conf/ -f conf/flume-maste.conf -Dflume.root.logger=DEBUG,console -n agent
在node上運作指令:
$ bin/flume-ng agent --conf ./conf/ -f conf/flume-node.conf -Dflume.root.logger=DEBUG,console -n agent
啟動之後,就可以發現兩者之間可以互相通信,master上面的檔案就能發送到node上,修改master上的test檔案,在後面追加内容時,node也可以接收到。
如果想要将内容寫入hadoop,可以将node中的flume-node.conf檔案做如下修改:
agent.sinks = k2
agent.sinks.k2.type = hdfs
agent.sinks.k2.channel = memoryChannel
agent.sinks.k2.hdfs.path = hdfs://master:8089/hbase
agent.sinks.k2.hdfs.fileType = DataStream
agent.sinks.k2.hdfs.writeFormat = Text
其中,hdfs://master:8089/hbase為hadoop的hdfs檔案路徑。