大數據學習系列:Hadoop3.0苦命學習(六)

傳送門:
大數據學習系列:Hadoop3.0苦命學習(一)
大數據學習系列:Hadoop3.0苦命學習(二)
大數據學習系列:Hadoop3.0苦命學習(三)
大數據學習系列:Hadoop3.0苦命學習(四)
大數據學習系列:Hadoop3.0苦命學習(五)
大數據學習系列:Hadoop3.0苦命學習(六)
大數據學習系列:Hadoop3.0苦命學習(七)

本節主要學習Flume。

1 Flume 介紹

1.1 概述

  • Flume 是一個分佈式、可靠、和高可用的海量日誌採集、聚合和傳輸的系統。
  • Flume 可以採集文件,socket數據包、文件、文件夾、kafka等各種形式源數據,又可以將採集到的數據(下沉sink)輸出到HDFS、hbase、hive、kafka等衆多外部存儲系統中
  • 一般的採集需求,通過對 flume的簡單配置即可實現
  • Flume 針對特殊場景也具備良好的自定義擴展能力,因此,flume可以適用於大部分的日常數據採集場景

1.2 運行機制

  1. Flume分佈式系統中最核心的角色是agent,flume採集系統就是由一個個agent所連接起來形成
  2. 每一個agent相當於一個數據傳遞員,內部有三個組件:
    1. Source:採集組件,用於跟數據源對接,以獲取數據
    2. Sink:下沉組件,用於往下一級agent傳遞數據或者往最終存儲系統傳遞數據
    3. Channel:傳輸通道組件,用於從source將數據傳遞到sink
      在這裏插入圖片描述

1.3 Flume 結構圖

簡單結構
單個 Agent 採集數據
在這裏插入圖片描述
複雜結構
多級 Agent 之間串聯

在這裏插入圖片描述

2 Flume 實戰案例

案例:使用網絡telent命令向一臺機器發送一些網絡數據,然後通過flume採集網絡端口數據

在這裏插入圖片描述

2.1 Flume 的安裝部署

Step 1: 下載解壓修改配置文件

下載地址:官方下載地址

Flume的安裝非常簡單,只需要解壓即可,當然,前提是已有hadoop環境

上傳安裝包到數據源所在節點上

cd /export/software/
tar -zxvf apache-flume-1.8.0-bin.tar.gz -C ../services/
cd /export/services/apache-flume-1.8.0-bin/conf/
cp flume-env.sh.template flume-env.sh
vim flume-env.sh
export JAVA_HOME=/export/services/jdk1.8.0_251

在這裏插入圖片描述

Step 2 開發配置文件

根據數據採集的需求配置採集方案,描述在配置文件中(文件名可任意自定義)

配置我們的網絡收集的配置文件
在flume的conf目錄下新建一個配置文件(採集方案)

vim /export/services/apache-flume-1.8.0-bin/conf/netcat-logger.conf

# 定義這個agent中各組件的名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# 描述和配置source組件:r1
a1.sources.r1.type = netcat
a1.sources.r1.bind = 192.168.188.100
a1.sources.r1.port = 44444
# 描述和配置sink組件:k1
a1.sinks.k1.type = logger
# 描述和配置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

Step 3 啓動配置文件

指定採集方案配置文件,在相應的節點上啓動flume agent

先用一個最簡單的例子來測試一下程序環境是否正常
啓動agent去採集數據

bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console
  • -c conf 指定flume自身的配置文件所在目錄
  • -f conf/netcat-logger.con 指定我們所描述的採集方案
  • -n a1 指定我們這個agent的名字
    在這裏插入圖片描述
    可以看到監控的主機和端口。

Step 4 安裝 Telnet 準備測試

在node02機器上面安裝telnet客戶端,用於模擬數據的發送

yum -y install telnet
telnet node01 44444 # 使用telnet模擬數據發送

發送“消息”:
在這裏插入圖片描述
node01接收到“消息”:
在這裏插入圖片描述

2.2. 採集案例

2.2.1 採集目錄到 HDFS

需求
某服務器的某特定目錄下,會不斷產生新的文件,每當有新文件出現,就需要把文件採集到HDFS中去

思路
根據需求,首先定義以下3大要素

  1. 數據源組件,即source ——監控文件目錄 : spooldir
    1. 監視一個目錄,只要目錄中出現新文件,就會採集文件中的內容
    2. 採集完成的文件,會被agent自動添加一個後綴:COMPLETED
    3. 所監視的目錄中不允許重複出現相同文件名的文件
  2. 下沉組件,即sink——HDFS文件系統 : hdfs sink
  3. 通道組件,即channel——可用file channel 也可以用內存channel

Step 1 Flume 配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
mkdir -p /export/services/dirfile
vim spooldir.conf

填寫下列配置

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
##注意:不能往監控目中重複丟同名文件
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /export/services/dirfile
a1.sources.r1.fileHeader = true
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = hdfs://node01:8020/spooldir/files/%y-%m-%d/%H%M/
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.rollInterval = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件類型,默認是Sequencefile,可用DataStream,則爲普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# 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

Channel參數解釋

capacity:默認該通道中最大的可以存儲的event數量
trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數量
keep-alive:event添加到通道中或者移出的允許時間

Step 2 啓動 Flume

bin/flume-ng agent -c ./conf -f ./conf/spooldir.conf -n a1 -Dflume.root.logger=INFO,console

Step 3 上傳文件到指定目錄

將不同的文件上傳到/export/servers/dirfile目錄里面去,注意文件不能重名

aaa.txt 文件內容

hello sky
hello hello
hello too

在這裏插入圖片描述
可以看到上圖產生了新的文件。
在這裏插入圖片描述
第一個文件內容如上。

2.2.2 採集文件到 HDFS

需求
比如業務系統使用log4j生成的日誌,日誌內容不斷增加,需要把追加到日誌文件中的數據實時採集到hdfs

分析
根據需求,首先定義以下3大要素

  • 採集源,即 source——監控文件內容更新 : exec ‘tail -F file’
  • 下沉目標,即 sink——HDFS文件系統 : hdfs sink
  • Source 和sink之間的傳遞通道——channel,可用file channel 也可以用 內存channel

Step 1 定義 Flume 配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
vim tail-file.conf

填寫下列配置:

agent1.sources = source1
agent1.sinks = sink1
agent1.channels = channel1
# Describe/configure tail -F source1
agent1.sources.source1.type = exec
agent1.sources.source1.command = tail -F /export/services/taillogs/access_log
agent1.sources.source1.channels = channel1
# Describe sink1
agent1.sinks.sink1.type = hdfs
#a1.sinks.k1.channel = c1
agent1.sinks.sink1.hdfs.path = hdfs://node01:8020/weblog/flume-collection/%y-%m-%d/%H%M/
agent1.sinks.sink1.hdfs.filePrefix = access_log
agent1.sinks.sink1.hdfs.maxOpenFiles = 5000
agent1.sinks.sink1.hdfs.batchSize= 100
agent1.sinks.sink1.hdfs.fileType = DataStream
agent1.sinks.sink1.hdfs.writeFormat =Text
agent1.sinks.sink1.hdfs.round = true
agent1.sinks.sink1.hdfs.roundValue = 10
agent1.sinks.sink1.hdfs.roundUnit = minute
agent1.sinks.sink1.hdfs.useLocalTimeStamp = true
# Use a channel which buffers events in memory
agent1.channels.channel1.type = memory
agent1.channels.channel1.keep-alive = 120
agent1.channels.channel1.capacity = 500000
agent1.channels.channel1.transactionCapacity = 600
# Bind the source and sink to the channel
agent1.sources.source1.channels = channel1
agent1.sinks.sink1.channel = channel1

Step 2 啓動 Flume

 cd /export/services/apache-flume-1.8.0-bin/
 bin/flume-ng agent -c conf -f conf/tail-file.conf -n agent1 -Dflume.root.logger=INFO,console

Step 3 開發 Shell 腳本定時追加文件內容

mkdir -p /export/services/shells/
cd /export/services/shells/
vim tail-file.sh

腳本如下:

#!/bin/bash
while true
do
 date >> /export/services/taillogs/access_log;
 sleep 0.5;
done

Step 4 啓動腳本

# 創建文件夾
mkdir -p /export/services/taillogs/
# 啓動腳本
sh /export/services/shells/tail-file.sh

執行結果:
在這裏插入圖片描述
其中一個文件:
在這裏插入圖片描述

2.2.3 Agent 級聯

在這裏插入圖片描述
分析
第一個agent負責收集文件當中的數據,通過網絡發送到第二個agent當中去
第二個agent負責接收第一個agent發送的數據,並將數據保存到hdfs上面去

Step 1 Node02 安裝 Flume

將node01機器上面解壓後的flume文件夾拷貝到node02機器上面去

cd /export/services/
scp -r apache-flume-1.8.0-bin/ node02:$PWD

Step 2 Node02 配置 Flume

在node02機器配置我們的flume

cd /export/services/apache-flume-1.8.0-bin/conf/
vim tail-avro-avro-logger.conf

配置如下:

##################
# 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 /export/services/taillogs/access_log
a1.sources.r1.channels = c1
# Describe the sink
##sink端的avro是一個數據發送者
a1.sinks = k1
a1.sinks.k1.type = avro
a1.sinks.k1.channel = c1
a1.sinks.k1.hostname = 192.168.188.100
a1.sinks.k1.port = 4141
a1.sinks.k1.batch-size = 10
# 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

Step 3 開發腳本向文件中寫入數據

直接將node01下面的腳本拷貝到node02即可,node01機器上執行以下命令
cd /export/services/
scp -r shells/ node02:$PWD

Step 4 Node01 Flume 配置文件

在node01機器上開發flume的配置文件

cd /export/services/apache-flume-1.8.0-bin/
vim avro-hdfs.conf

配置如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
##source中的avro組件是一個接收者服務
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 192.168.188.100
a1.sources.r1.port = 4141
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://node01:8020/avro/%y-%m-%d/%H%M/
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.rollInterval = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件類型,默認是Sequencefile,可用DataStream,則爲普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# 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

Step 5 順序啓動

node01機器啓動flume進程

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -c conf -f conf/avro-hdfs.conf -n a1 -Dflume.root.logger=INFO,console

node02機器啓動flume進程

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -c conf -f conf/tail-avro-avro-logger.conf -n a1 -Dflume.root.logger=INFO,console

node02機器啓shell腳本生成文件

cd /export/services/shells/
sh tail-file.sh

執行結果:
在這裏插入圖片描述

3 高可用方案

在完成單點的Flume NG搭建後,下面我們搭建一個高可用的Flume NG集羣,架構圖如下所示:

3.1 角色分配

Flume的Agent和Collector分佈如下表所示:

名稱 HOST 角色
Agent1 node01 Web
Collector1 node02 AgentMstr1
Collector2 node03 AgentMstr2

圖中所示,Agent1數據分別流入到Collector1和Collector2,Flume NG本身提供了Failover機制,可以自動切換和恢復。在上圖中,有3個產生日誌服務器分佈在不同的機房,要把所有的日誌都收集到一個集羣中存儲。下 面我們開發配置Flume NG集羣

3.2 Node01 安裝和配置

將node01機器上面的flume安裝包以及文件生產的兩個目錄拷貝到node03機器上面去
node01機器執行以下命令

cd /export/services/
scp -r apache-flume-1.8.0-bin/ node03:$PWD
scp -r shells/ taillogs/ node03:$PWD

node01機器配置agent的配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
vim agent.conf

配置如下:

#agent1 name
agent1.channels = c1
agent1.sources = r1
agent1.sinks = k1 k2
#
##set gruop
agent1.sinkgroups = g1
#
agent1.sources.r1.channels = c1
agent1.sources.r1.type = exec
agent1.sources.r1.command = tail -F /export/services/taillogs/access_log
#
##set channel
agent1.channels.c1.type = memory
agent1.channels.c1.capacity = 1000
agent1.channels.c1.transactionCapacity = 100
#
## set sink1
agent1.sinks.k1.channel = c1
agent1.sinks.k1.type = avro
agent1.sinks.k1.hostname = node02
agent1.sinks.k1.port = 52020
#
## set sink2
agent1.sinks.k2.channel = c1
agent1.sinks.k2.type = avro
agent1.sinks.k2.hostname = node03
agent1.sinks.k2.port = 52020
#
##set sink group
agent1.sinkgroups.g1.sinks = k1 k2
#
##set failover
agent1.sinkgroups.g1.processor.type = failover
agent1.sinkgroups.g1.processor.priority.k1 = 10
agent1.sinkgroups.g1.processor.priority.k2 = 1
agent1.sinkgroups.g1.processor.maxpenalty = 10000

3.3 Node02 與 Node03 配置 FlumeCollection

node02機器修改配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
vim collector.conf

配置如下:

#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#
## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = node02
a1.sources.r1.port = 52020
a1.sources.r1.channels = c1
#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path= hdfs://node01:8020/flume/failover/
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=TEXT
a1.sinks.k1.hdfs.rollInterval=10
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#

node03機器修改配置文件

cd /export/servers/apache-flume-1.8.0-bin/conf
vim collector.conf

配置如下:

#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#
## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = node03
a1.sources.r1.port = 52020
a1.sources.r1.channels = c1
#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path= hdfs://node01:8020/flume/failover/
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=TEXT
a1.sinks.k1.hdfs.rollInterval=10
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d
a1.sinks.k1.hdfs.useLocalTimeStamp = true

3.4 順序啓動

node03機器上面啓動flume

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n a1 -c conf -f conf/collector.conf -Dflume.root.logger=INFO,console

node02機器上面啓動flume

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n a1 -c conf -f conf/collector.conf -Dflume.root.logger=INFO,console

node01機器上面啓動flume

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n agent1 -c conf -f conf/agent.conf  -Dflume.root.logger=INFO,console

node01機器啓動文件產生腳本

cd /export/services/shells/
sh tail-file.sh

測試結果是:
我們在Agent1節點變更文件數據,由於我們配置Collector1的權重比Collector2大,所以 Collector1優先採集並上傳到存儲系統。然後我們kill掉Collector1,此時有Collector2負責日誌的採集上傳工作,之後,我 們手動恢復Collector1節點的Flume服務,再次在Agent1變更文件數據,發現Collector1恢復優先級別的採集工作。

4 Flume 的負載均衡

負載均衡是用於解決一臺機器(一個進程)無法解決所有請求而產生的一種算法。Load balancing Sink Processor 能夠實現 load balance 功能,如下圖Agent1 是一個路由節點,負責將 Channel 暫存的 Event 均衡到對應的多個 Sink組件上,而每個 Sink 組件分別連接到一個獨立的 Agent 上,示例配置,如下所示:
在這裏插入圖片描述
在此處我們通過三臺機器來進行模擬 flume的負載均衡

三臺機器規劃如下:

  • node01:採集數據,發送到node02和node03機器上去
  • node02:接收node01的部分數據
  • node03:接收node01的部分數據

第一步 開發node01服務器的flume配置

node01服務器配置:

cd /export/services/apache-flume-1.8.0-bin/conf/
vim load_banlancer_client.conf

配置如下:

# agent name
a1.channels = c1
a1.sources = r1
a1.sinks = k1 k2

# set gruop
a1.sinkgroups = g1

# set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /export/services/taillogs/access_log

# set sink1
a1.sinks.k1.channel = c1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node02
a1.sinks.k1.port = 52020

# set sink2
a1.sinks.k2.channel = c1
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = node03
a1.sinks.k2.port = 52020

# set sink group
a1.sinkgroups.g1.sinks = k1 k2

# set failover
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
a1.sinkgroups.g1.processor.selector = round_robin
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000

第二步 開發node02服務器的flume配置

cd /export/services/apache-flume-1.8.0-bin/conf/
vim load_banlancer_server.conf

配置如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = node02
a1.sources.r1.port = 52020

# 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

第三步 開發 node03服務器flume配置

node03服務器配置

cd /export/services/apache-flume-1.8.0-bin/conf/
vim load_banlancer_server.conf

配置如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = node03
a1.sources.r1.port = 52020

# 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服務

啓動node03的flume服務

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n a1 -c conf -f conf/load_banlancer_server.conf -Dflume.root.logger=INFO,console

啓動node02的flume服務

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n a1 -c conf -f conf/load_banlancer_server.conf -Dflume.root.logger=INFO,console

啓動node01的flume服務

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -n a1 -c conf -f conf/load_banlancer_client.conf -Dflume.root.logger=INFO,console

第五步 node01服務器運行腳本產生數據

cd /export/services/shells
sh tail-file.sh

結果:輪詢消費數據,達到負載均衡效果。

5 Flume 案例

5.1 案例場景

A、B兩臺日誌服務機器實時生產日誌主要類型爲access.log、nginx.log、web.log

現在要求:
把A、B 機器中的access.log、nginx.log、web.log 採集彙總到C機器上然後統一收集到hdfs中。

但是在hdfs中要求的目錄爲:
/source/logs/access/20180101/**
/source/logs/nginx/20180101/**
/source/logs/web/20180101/**

5.2 場景分析

在這裏插入圖片描述

5.3 數據流程處理分析

在這裏插入圖片描述

5.4 實現

服務器A對應的IP爲 192.168.174.100
服務器B對應的IP爲 192.168.174.110
服務器C對應的IP爲 192.168.174.120

採集端配置文件開發
node03與node02服務器開發flume的配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
vim exec_source_avro_sink.conf

配置如下:

# Name the components on this agent
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F  /export/services/taillogs/access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
## static攔截器的功能就是往採集到的數據的header中插入自己定## 義的key-value對
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access

a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /export/services/taillogs/nginx.log
a1.sources.r2.interceptors = i2
a1.sources.r2.interceptors.i2.type = static
a1.sources.r2.interceptors.i2.key = type
a1.sources.r2.interceptors.i2.value = nginx

a1.sources.r3.type = exec
a1.sources.r3.command = tail -F/export/services/taillogs/web.log
a1.sources.r3.interceptors = i3
a1.sources.r3.interceptors.i3.type = static
a1.sources.r3.interceptors.i3.key = type
a1.sources.r3.interceptors.i3.value = web

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node01
a1.sinks.k1.port = 41414

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1

服務端配置文件開發
在node01上面開發flume配置文件

cd /export/services/apache-flume-1.8.0-bin/conf/
vim avro_source_hdfs_sink.conf

配置如下:

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

# 定義source
a1.sources.r1.type = avro
a1.sources.r1.bind = 192.168.188.100
a1.sources.r1.port =41414

# 添加時間攔截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder

# 定義channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000

# 定義sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path=hdfs://192.168.188.100:8020/source/logs/%{type}/%Y%m%d
a1.sinks.k1.hdfs.filePrefix =events
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text

# 時間類型
a1.sinks.k1.hdfs.useLocalTimeStamp = true

# 生成的文件不按條數生成
a1.sinks.k1.hdfs.rollCount = 0

# 生成的文件按時間生成
a1.sinks.k1.hdfs.rollInterval = 30

# 生成的文件按大小生成
a1.sinks.k1.hdfs.rollSize = 10485760

# 批量寫入hdfs的個數
a1.sinks.k1.hdfs.batchSize = 10000

# flume操作hdfs的線程數(包括新建,寫入等)
a1.sinks.k1.hdfs.threadsPoolSize=10

# 操作hdfs超時時間
a1.sinks.k1.hdfs.callTimeout=30000

# 組裝source、channel、sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

採集端文件 生成腳本
在node03與node02上面開發shell腳本,模擬數據生成

cd /export/services/shells
vim server.sh
# !/bin/bash
while true
do
date >> /export/services/taillogs/access.log;
date >> /export/services/taillogs/web.log;
date >> /export/services/taillogs/nginx.log;
sleep 0.5;
done

順序啓動服務
node01啓動flume實現數據收集

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -c conf -f conf/avro_source_hdfs_sink.conf -name a1 -Dflume.root.logger=INFO,console

node03與node02啓動flume實現數據監控

cd /export/services/apache-flume-1.8.0-bin/
bin/flume-ng agent -c conf -f conf/exec_source_avro_sink.conf -name a1 -Dflume.root.logger=INFO,console

node03 與node02啓動生成文件腳本

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