1. 程式人生 > 其它 >第五章 Flume日誌採集系統

第五章 Flume日誌採集系統

主要內容:

  • 瞭解Flume
  • Flume安裝部署
  • HDFSSink

5.1 瞭解Flume

1)Flume概述

Flume是Cloudera提供的一個高可用,高可靠的,分散式的海量日誌採集、聚合和傳輸的軟體。

Flume的核心是把資料從資料來源(source)收集過來,再將收集到的資料送到指定的目的地(sink)。為了保證輸送的過程一定成功,在送到目的地(sink)之前,會先快取資料channel),待資料真正到達目的地(sink)後,Flume再刪除自己快取的資料。

Flume支援定製各類資料傳送方,用於收集各型別資料,同時,Flume支援定製各種資料接受方,用於最終儲存資料。一般的採集需求,通過對Flume的簡單配置即可實現。針對特殊場景也具備良好的自定義擴充套件能力。因此,Flume可以適用於大部分的日常資料採集場景。

當前Flume有兩個版本。Flume的初始發行版本統稱Flume OG(original generation),Flume1.X版本的統稱Flume NG(next generation)。版本改動的原因是隨著 Flume 功能的擴充套件,Flume OG 程式碼工程臃腫、核心元件設計不合理、核心配置不標準等缺點暴露出來,尤其是在 Flume OG 的最後一個發行版本 0.94中,日誌傳輸不穩定的現象尤為嚴重。

Flume NG經過核心元件、核心配置以及程式碼架構重構,與Flume OG有很大不同,使用時請注意區分。改動的另一個原因是將Flume納入Apache旗下,Cloudera Flume改名為Apache Flume。

2)Flume執行機制

Flume日誌採集傳輸系統中核心的角色是agent,agent本身是一個Java程序,一般執行在日誌收集節點,當然也可以執行在資料下沉的節點。 Source、Sink、Channel三個元件是Flume執行的核心。

agent元件介紹

每一個agent相當於一個數據傳遞員,內部三個元件的介紹:

Source:採集源,用於跟資料來源對接,以獲取資料;

Sink:下沉地,採集資料的傳送目的地,用於往下一級agent傳遞資料或者往最終儲存系統傳遞資料;

Channelagent內部的資料傳輸通道,用於從source將資料傳遞到sink;

event介紹

在整個資料的傳輸的過程中,流動的是event,它是Flume內部資料傳輸的最基本的單元

event將傳輸的資料進行封裝。如果是文字檔案,通常是一行記錄,event也是事務的基本單位。event從source,流向channel,再到sink,本身為一個位元組陣列,並可攜帶headers(頭資訊)資訊。event代表著一個數據的最小完整單元,從外部資料來源來,向外部的目的地去。

一個完整的event包括:event headers、event body、event資訊,其中event資訊就是Flume收集到的日誌記錄。

3)Flume採集系統結構圖

單個agent採集資料:

我們知道Flume一個很重要的功能就是應用於大資料環境中採集日誌資訊,那麼對於分散式系統上面所講的簡單結構滿足不了。所以有了複雜結構,比如在每一臺伺服器上都部署一個agent程序進行資料採集,再通過一個agent程序對所有采集到的資料進行彙總,並下沉到資料儲存系統。這樣使得Flume系統可以很方便地擴充套件。

多級agent之間串聯

4)官網介紹

http://flume.apache.org/

官網左邊的選單欄的documentation-> Flume User Guide,在頁面左邊選單的Configuration下面我們可以看到Flume Sources、Flume Sinks、Flume Channels。這裡所列舉的資料來源、下沉地和傳輸通道,基本上包含了大資料領域常用的軟體,能滿足大部分的需求(特殊情況下Flume也支援自定義)。

由此我們也可以知道,Flume使用的時候重點在於採集方案的配置。針對採集需求使用的是什麼樣的source、sink和channel,把服務配置好並啟動起來,就可以進行資料採集、資料的傳輸和資料的下沉。

5.2 Flume安裝和部署步驟介紹

5.2.1 Flume的安裝

1)上傳安裝包到資料來源所在節點上,實際上不是資料來源節點也是可以的,只要執行Flume的這臺機器與資料來源節點的這臺機器能夠通過某種協議進行通訊即可。

2)然後解壓tar -zxvf apache-flume-1.8.0-bin.tar.gz,並修改(mv)檔名為flume

3)然後進入flume的目錄,修改conf下的flume-env.sh(沒有的話複製(cp)flume-env.sh.template),在裡面配置JAVA_HOME為jdk的根目錄。

4)根據資料採集需求配置採集方案,描述在配置檔案中(檔名可任意自定義)

5)指定採集方案配置檔案,在相應的節點上啟動flume agent

先用一個最簡單的例子來測試一下程式環境是否正常。重點是體會一下Flume三個元件之間該怎麼配置。

5.2.2 需求

往一個網路埠上傳送資料,Flume監聽該埠,將接收到的資料收集起來,並下沉到終端上以日誌的形式打印出來。

5.2.3 配置採集方案

在flume的conf目錄下新建一個檔案:vi netcat-logger.conf (命名規則建議source-sink.conf)

配置檔案netcat-logger.conf參考內容:

#從網路埠接受資料,下沉到logger

#採集配置檔案,netcat-logger.conf

#定義這個agent中各元件的名字

a1.sources=r1
a1.sinks=k1
a1.channels=c1

#描述和配置source元件:r1

a1.sources.r1.type=netcat
a1.sources.r1.bind=localhost
a1.sources.r1.port=44444

#描述和配置sink元件:k1

a1.sinks.k1.type=logger

#描述和配置channel元件,此處使用是記憶體快取的方式

a1.channels.c1.type=memory
# The maximum number of events stored in the channel
a1.channels.c1.capacity=1000
# The maximum number of events the channel will take from a source or give to a sink per transaction
a1.channels.c1.transactionCapacity=100

#描述和配置source channel sink之間的連線關係

a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1

5.2.4 啟動agent採集資料

命令格式:

$ bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template

示例:

bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console

命令解釋:

-c (或--conf) : flume自身所需的conf檔案路徑

-f (--conf-file) : 自定義的flume配置檔案,也就是採集方案檔案

-n (--name): 自定義的flume配置檔案中agent的name

5.2.5 測試

先要往agent採集監聽的埠上傳送資料,讓agent有資料可採。

隨便在一個能跟agent節點聯網的機器上,也可以在本機:

telnet agent-hostname port (如:telnet localhost 44444)

注意1:如果telnet還沒有安裝,則需要執行yum install -y telnet進行安裝

telnet安裝過程記錄:

執行之前:

  • 設定裡面網路連線更改為vmnet8

1、檢測telnet-server的rpm包是否安裝

# rpm -qa telnet-server

若無輸出內容,則表示沒有安裝。如:

2、若未安裝,則安裝telnet-server,否則忽略此步驟

#yum install telnet-server

3、檢測telnet的rpm包是否安裝

# rpm -qa telnet
telnet-0.17-47.el6_3.1.x86_64

4、若未安裝,則安裝telnet,否則忽略此步驟

# yum install telnet

5、啟動telnet服務

# systemctl start telnet.socket

6、設定開機啟動

#chkconfig telnet on

注意2:如果telnet 127.0.0.1 44444能成功,而telnet localhost 44444不能成功,說明hosts檔案內容需要補充。參考樣式如下:

#vi /etc/hosts

5.3HDFSSink

This sink writes events into the Hadoop Distributed File System (HDFS). It currently supports creating text and sequence files. It supports compression in both file types. The files can be rolled (close current file and create a new one) periodically based on the elapsed time or size of data or number of events. It also buckets/partitions data by attributes like timestamp or machine where the event originated. The HDFS directory path may contain formatting escape sequences that will replaced by the HDFS sink to generate a directory/file name to store the events. Using this sink requires hadoop to be installed so that Flume can use the Hadoop jars to communicate with the HDFS cluster. Note that a version of Hadoop that supports the sync() call is required.

Example for agent named a1:

a1.channels = c1
a1.sinks = k1
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute

hdfs中建立資料夾用於採集日誌的存放:

hdfs檢視檔案目錄:

#hdfs dfs -lsr /

建立資料夾:

hdfs dfs -mkdir /user/flume

建立配置檔案netcat-hdfs.conf,參考內容如下:

#從網路埠接受資料,下沉到hdfs

#採集配置檔案,netcat-hdfs.conf

#定義這個agent中各元件的名字

a1.sources=r1
a1.sinks=k1
a1.channels=c1
#描述和配置source元件:r1
a1.sources.r1.type=netcat
a1.sources.r1.bind=localhost
a1.sources.r1.port=44444

#描述和配置sink元件:k1

a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /user/flume/events/%y-%m-%d/%H%M/%S
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = second
a1.sinks.k1.hdfs.useLocalTimeStamp = true

#描述和配置channel元件,此處使用是記憶體快取的方式

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100

#描述和配置source channel sink之間的連線關係

a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1

flume資料夾下啟動:

bin/flume-ng agent --conf conf --conf-file conf/netcat-hdfs.conf --name a1

另開一個視窗:

#telnet localhost 44444

隨便輸入一些資訊

檢視對應hdfs檔案資訊:

#hdfs dfs -lsr /user/flume