Hadoop入門進階課程12--Flume介紹、安裝與應(yīng)用案例
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,博主為石山園,博客地址為 http://www.rzrgm.cn/shishanyuan 。該系列課程是應(yīng)邀實驗樓整理編寫的,這里需要贊一下實驗樓提供了學習的新方式,可以邊看博客邊上機實驗,課程地址為 https://www.shiyanlou.com/courses/237
【注】該系列所使用到安裝包、測試數(shù)據(jù)和代碼均可在百度網(wǎng)盤下載,具體地址為 http://pan.baidu.com/s/10PnDs,下載該PDF文件
1、搭建環(huán)境
部署節(jié)點操作系統(tǒng)為CentOS,防火墻和SElinux禁用,創(chuàng)建了一個shiyanlou用戶并在系統(tǒng)根目錄下創(chuàng)建/app目錄,用于存放Hadoop等組件運行包。因為該目錄用于安裝hadoop等組件程序,用戶對shiyanlou必須賦予rwx權(quán)限(一般做法是root用戶在根目錄下創(chuàng)建/app目錄,并修改該目錄擁有者為shiyanlou(chown –R shiyanlou:shiyanlou /app)。
Hadoop搭建環(huán)境:
l 虛擬機操作系統(tǒng): CentOS6.6 64位,單核,1G內(nèi)存
l JDK:1.7.0_55 64位
l Hadoop:1.1.2
2、Flume介紹
Flume是Cloudera提供的日志收集系統(tǒng),Flume支持在日志系統(tǒng)中定制各類數(shù)據(jù)發(fā)送方,用于收集數(shù)據(jù);同時,Flume提供對數(shù)據(jù)進行簡單處理,并寫到各種數(shù)據(jù)接受方(可定制)的能力。 Flume是一個分布式、可靠和高可用的海量日志采集、聚合和傳輸?shù)南到y(tǒng)。
Flume具有Reliability、Scalability、Manageability和Extensibility特點:
1.Reliability:Flume提供3中數(shù)據(jù)可靠性選項,包括End-to-end、Store on failure和Best effort。其中End-to-end使用了磁盤日志和接受端Ack的方式,保證Flume接受到的數(shù)據(jù)會最終到達目的。Store on failure在目的不可用的時候,數(shù)據(jù)會保持在本地硬盤。和End-to-end不同的是,如果是進程出現(xiàn)問題,Store on failure可能會丟失部分數(shù)據(jù)。Best effort不做任何QoS保證。
2.Scalability:Flume的3大組件:collector、master和storage tier都是可伸縮的。需要注意的是,Flume中對事件的處理不需要帶狀態(tài),它的Scalability可以很容易實現(xiàn)。
3.Manageability:Flume利用ZooKeeper和gossip,保證配置數(shù)據(jù)的一致性、高可用。同時,多Master,保證Master可以管理大量的節(jié)點。
4.Extensibility:基于Java,用戶可以為Flume添加各種新的功能,如通過繼承Source,用戶可以實現(xiàn)自己的數(shù)據(jù)接入方式,實現(xiàn)Sink的子類,用戶可以將數(shù)據(jù)寫往特定目標,同時,通過SinkDecorator,用戶可以對數(shù)據(jù)進行一定的預處理。
2.1 Flume架構(gòu)
上圖的Flume的架構(gòu)中最重要的抽象是data flow(數(shù)據(jù)流),data flow描述了數(shù)據(jù)從產(chǎn)生,傳輸、處理并最終寫入目標的一條路徑(在上圖中,實線描述了data flow)。 Agent用于采集數(shù)據(jù),agent是flume中產(chǎn)生數(shù)據(jù)流的地方,同時,agent會將產(chǎn)生的數(shù)據(jù)流傳輸?shù)?span lang="EN-US">collector。對應(yīng)的,collector用于對數(shù)據(jù)進行聚合,往往會產(chǎn)生一個更大的流。
Flume提供了從console(控制臺)、RPC(Thrift-RPC)、text(文件)、tail(UNIX tail)、syslog(syslog日志系統(tǒng),支持TCP和UDP等2種模式),exec(命令執(zhí)行)等數(shù)據(jù)源上收集數(shù)據(jù)的能力。同時,Flume的數(shù)據(jù)接受方,可以是console(控制臺)、text(文件)、dfs(HDFS文件)、RPC(Thrift-RPC)和syslogTCP(TCP syslog日志系統(tǒng))等。
其中,收集數(shù)據(jù)有2種主要工作模式,如下:
1. Push Sources:外部系統(tǒng)會主動地將數(shù)據(jù)推送到Flume中,如RPC、syslog。
2. Polling Sources:Flume到外部系統(tǒng)中獲取數(shù)據(jù),一般使用輪詢的方式,如text和exec。
注意,在Flume中,agent和collector對應(yīng),而source和sink對應(yīng)。Source和sink強調(diào)發(fā)送、接受方的特性(如數(shù)據(jù)格式、編碼等),而agent和collector關(guān)注功能。
2.2 Flume管理方式
Flume Master用于管理數(shù)據(jù)流的配置,如下圖。
為了保證可擴展性,Flume采用了多Master的方式。為了保證配置數(shù)據(jù)的一致性,Flume引入了ZooKeeper,用于保存配置數(shù)據(jù),ZooKeeper本身可保證配置數(shù)據(jù)的一致性和高可用,另外,在配置數(shù)據(jù)發(fā)生變化時,ZooKeeper可以通知Flume Master節(jié)點。
Flume Master間使用gossip協(xié)議同步數(shù)據(jù)。
3、安裝部署Flume
3.1 Flume部署過程
3.1.1 下載Flume
可以到apache基金flume官網(wǎng)http://flume.apache.org/download.html,選擇鏡像下載地址http://mirrors.hust.edu.cn/apache/flume/下載一個穩(wěn)定版本,如下圖所示下載flume-1.5.2-bin.tar.gz:
也可以在/home/shiyanlou/install-pack目錄中找到該安裝包,解壓該安裝包并把該安裝包復制到/app目錄中
cd /home/shiyanlou/install-pack
tar -xzf flume-1.5.2-bin.tar.gz
mv apache-flume-1.5.2-bin /app/flume-1.5.2
3.1.2 設(shè)置/etc/profile參數(shù)
編輯/etc/profile文件,聲明flume的home路徑和在path加入bin的路徑:
export FLUME_HOME=/app/flume-1.5.2
export FLUME_CONF_DIR=$FLUME_HOME/conf
export PATH=$PATH:$FLUME_HOME/bin
編譯配置文件/etc/profile,并確認生效
source /etc/profile
echo $PATH
3.1.3 設(shè)置flume-env.sh配置文件
在$FLUME_HOME/conf 下復制改名flume-env.sh.template為flume-env.sh,修改conf/flume-env.sh配置文件
cd /app/flume-1.5.2/conf
cp flume-env.sh.template flume-env.sh
sudo vi flume-env.sh
修改配置文件內(nèi)容 :
JAVA_HOME=/app/lib/jdk1.7.0_55
JAVA_OPTS="-Xms100m -Xmx200m -Dcom.sun.management.jmxremote"
3.2 部署驗證
3.2.1 驗證安裝
1. 修改flume-conf配置文件
在$FLUME_HOME/conf目錄下修改flume-conf.properties.template文件,復制并改名為flume-conf,
cd /app/flume-1.5.2/conf
cp flume-conf.properties.template flume-conf.properties
sudo vi flume-conf.properties
修改flume-conf配置文件內(nèi)容
# The configuration file needs to define the sources, the channels and the sinks.
# Sources, channels and sinks are defined per agent, in this case called 'a1'
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# For each one of the sources, the type is defined
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
#The channel can be defined as follows.
a1.sources.r1.channels = c1
# Each sink's type must be defined
a1.sinks.k1.type = logger
#Specify the channel the sink should use
a1.sinks.k1.channel = c1
# Each channel's type is defined.
a1.channels.c1.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
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
2. 在flume的安裝目錄/flume-1.5.2下運行
cd /app/flume-1.5.2
./bin/flume-ng agent --conf ./conf/ --conf-file ./conf/flume-conf.properties --name a1 -Dflume.root.logger=INFO,console
3. 再打開一個終端,輸入如下命令:
telnet localhost 44444
hello world
注:在CentOS6.5運行telnet提示"command not found",使用sudo yum install telnet進行安裝
4. 在原來的終端上查看,可以收到來自于telnet發(fā)出的消息
3.2.2 測試收集日志到HDFS
1. 在$FLUME_HOME/conf目錄下修改flume-conf.properties.template文件,復制并改名為flume-conf2.properties
cd /app/flume-1.5.2/conf
cp flume-conf.properties.template flume-conf2.properties
sudo vi flume-conf2.properties
a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.type = exec
a1.sources.r1.channels = c1
a1.sources.r1.command = tail -F /app/hadoop-1.1.2/logs/hadoop-shiyanlou-namenode-b393a04554e1.log
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = hdfs://hadoop:9000/class12/out_flume
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollSize = 4000000
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.batchSize = 10
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
2. 在flume的安裝目錄/flume-1.5.2下運行
cd /app/flume-1.5.2
./bin/flume-ng agent --conf ./conf/ --conf-file ./conf/flume-conf2.properties --name a1 -Dflume.root.logger=INFO,console
3. 不斷收集hadoop-hadoop-namenode-hadoop1.log的數(shù)據(jù)寫入HDFS中
4. 查看hdfs中/class12/out_flume中的文件
hadoop fs -ls /class12/out_flume
hadoop fs -cat /class12/out_flume/events-.1433921305493
















浙公網(wǎng)安備 33010602011771號