Flume的基本使用介紹

一、Flume概述

Flume是一個分佈式的、高可靠的、可用的一個服務,用於收集、聚合、移動大量數據。它有簡單、靈活的結構基於數據流,具有健壯性和容錯性,它能夠使用簡單的、可擴展的數據模型用於在線實時分析應用。結構圖如下:



webserver(源端)  ===>  flume   ===> hdfs(目的地),一個Flume對應一個agent,agent裏包含Source、channel、sink,所以只需要配置agent,就可以完成各項配置,也增強了Flume的管理性。

設計目標爲:可靠性、擴展性、管理性。

二、Flume的架構和核心組件

2.1 三個核心組件

1.source(輸入)

source相當於數據的輸入,source的種類很多中,像Avro source、exec source、JMS source等,你也可以自定義source,這根據你不同的需要去官網查看對應的配置。

2.channel(收集)

主要用於數據的收集,相當於一個緩存池一樣,可以指定大小,先把數據積攢起來,再一起處理。種類有memory channel、File channel、 kafka channel、 file channel等,也可以自定義。

3.sink(輸出)

主要用於將收集好的數據對目的地進行輸出,主要有HDFS sink,HIVE sink,Logger sink,Avro sink, Hbase sink等,也可以自定義sink。

2.2 Flume的設置方式

Flume可以進行串聯和並聯,很高的擴展性,下面來介紹幾種Flume的設置方式。

1.串聯:


2.整合使用(Consolidation),多個agent合併輸出到某個agent:


3.多路複用流(multiplexing the flow),一個agent多個sink輸出:


三、Flume安裝與部署

3.1 Flume安裝前置條件

1. Java內存環境1.7 及 以上

2.要有足夠內存

3. 足夠的磁盤空間

4. 有目錄的權限

3.2 安裝JDK

下載,解壓到~/app,再將java配置系統環境變量中: ~/.bash_profile,設置如下:
export JAVA_HOME=/home/hadoop/app/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
配置好後,source下讓其配置生效,最後檢測: java  -version。

3.3 安裝FLUME

下載,解壓到~/app,再將java配置系統環境變量中: ~/.bash_profile
export FLUME_HOME=/home/hadoop/app/apache-flume-1.6.0-cdh5.7.0-bin
export PATH=$FLUME_HOME/bin:$PATH
配置好後,source下讓其配置生效,其中主要的是flume-env.sh的配置:

export JAVA_HOME=/home/hadoop/app/jdk1.8.0_144
最後檢測: flume-ng version。

3.4 使用

使用Flume的關鍵就是寫配置文件
A) 配置Source
B) 配置Channel
C) 配置Sink
D) 把以上三個組件串起來

接下來我就用三個Demo來進行展示。

四、Flume實戰Demo

4-1 從指定網絡端口採集數據輸出到控制檯

在Flume的conf目錄下生成一個配置文件,內容如下

a1: agent名稱 
r1: source的名稱
k1: sink的名稱
c1: channel的名稱

# 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 = hadoop000
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

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
 採用到source的netcat類型,意思就是監聽hadoop000 的44444端口,把文本的每行內容轉成一個event,就相當於一個日誌輸出單元。channel試着爲memory,採用內存進行緩存,sink的type設置爲logger,意思是sink的形式爲日誌形式,並且是Info的等級。後面兩句配置語句,就是將source、channel、sink配置起來,注意的是一個source可以設置多個channel,而一個channel只能對應一個sink,和然後啓動agent,命令如下:

flume-ng agent \
--name a1  \
--conf $FLUME_HOME/conf  \
--conf-file $FLUME_HOME/conf/example.conf \
-Dflume.root.logger=INFO,console
使用telnet進行測試: telnet hadoop000 44444。

測試結果如下:


其中要注意的是:
Event: { headers:{} body: 68 65 6C 6C 6F 0D hello. }
Event是FLume數據傳輸的基本單元
Event =  可選的header + byte array

4.2 監控一個文件實時採集新增的數據輸出到控制檯

Agent選型:exec source + memory channel + logger sink,選擇exec source就是運行unix指定的命令行來處理不斷的數據,以此來生產數據。本demo就是實時監控data.log日誌內的內容。

配置文件如下:

# 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 /home/hadoop/data/data.log
a1.sources.r1.shell = /bin/sh -c

# Describe the sink
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
a1.channels.c1.type = memory

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
此處用到的source command也就是執行對data.log進行數據的讀取命令,啓動agent命令如下:

flume-ng agent \
--name a1  \
--conf $FLUME_HOME/conf  \
--conf-file $FLUME_HOME/conf/exec-memory-logger.conf \
-Dflume.root.logger=INFO,console

4.3 將A服務器上的日誌實時採集到B服務器上

技術選型:
exec source + memory channel + avro sink
avro source + memory channel + logger sink

流程圖:


注意跨節點的數據一般都採用到的是avro 類型。

配置文件exec-memory-avro.conf:

exec-memory-avro.sources = exec-source
exec-memory-avro.sinks = avro-sink
exec-memory-avro.channels = memory-channel

exec-memory-avro.sources.exec-source.type = exec
exec-memory-avro.sources.exec-source.command = tail -F /home/hadoop/data/data.log
exec-memory-avro.sources.exec-source.shell = /bin/sh -c

exec-memory-avro.sinks.avro-sink.type = avro
exec-memory-avro.sinks.avro-sink.hostname = hadoop000
exec-memory-avro.sinks.avro-sink.port = 44444

exec-memory-avro.channels.memory-channel.type = memory

exec-memory-avro.sources.exec-source.channels = memory-channel
exec-memory-avro.sinks.avro-sink.channel = memory-channel
配置文件avro-memory-logger.conf:

avro-memory-logger.sources = avro-source
avro-memory-logger.sinks = logger-sink
avro-memory-logger.channels = memory-channel

avro-memory-logger.sources.avro-source.type = avro
avro-memory-logger.sources.avro-source.bind = hadoop000
avro-memory-logger.sources.avro-source.port = 44444

avro-memory-logger.sinks.logger-sink.type = logger

avro-memory-logger.channels.memory-channel.type = memory

avro-memory-logger.sources.avro-source.channels = memory-channel
avro-memory-logger.sinks.logger-sink.channel = memory-channel

先啓動avro-memory-logger,命令如下:

flume-ng agent \
--name avro-memory-logger  \
--conf $FLUME_HOME/conf  \
--conf-file $FLUME_HOME/conf/avro-memory-logger.conf \
-Dflume.root.logger=INFO,console
再啓動exec-memory-avro,命令如下:

flume-ng agent \
--name exec-memory-avro  \
--conf $FLUME_HOME/conf  \
--conf-file $FLUME_HOME/conf/exec-memory-avro.conf \
-Dflume.root.logger=INFO,console
效果基本就是在data.log進行輸入,然後會在avro-memory-logger這個配置文件的agent裏進行控制檯的輸出。







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章