[大數據基礎]Flume學習

Flume定義

Flume是Cloudera提供的一個高可用的,高可靠的,分佈式的海量日誌採集、聚合和傳輸的系統。Flume基於流式架構,靈活簡單。
在這裏插入圖片描述

Flume框架

在這裏插入圖片描述

在這裏插入圖片描述

批處理

下面我們來詳細介紹一下Flume架構中的組件。

  1. Agent
    Agent是一個JVM進程,它以事件的形式將數據從源頭送至目的,是Flume數據傳輸的基本單元。
    Agent主要有3個部分組成,Source、Channel、Sink。
  2. Source
    Source是負責接收數據到Flume Agent的組件。Source組件可以處理各種類型、各種格式的日誌數據,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。
  3. Channel
    Channel是位於Source和Sink之間的緩衝區。因此,Channel允許Source和Sink運作在不同的速率上。Channel是線程安全的,可以同時處理幾個Source的寫入操作和幾個Sink的讀取操作。
    Flume自帶兩種Channel:Memory Channel和File Channel。
    Memory Channel是內存中的隊列。Memory Channel在不需要關心數據丟失的情景下適用。如果需要關心數據丟失,那麼Memory Channel就不應該使用,因爲程序死亡、機器宕機或者重啓都會導致數據丟失。
    File Channel將所有事件寫到磁盤。因此在程序關閉或機器宕機的情況下不會丟失數據。
  4. Sink
    Sink不斷地輪詢Channel中的事件且批量地移除它們,並將這些事件批量寫入到存儲或索引系統、或者被髮送到另一個Flume Agent。
    Sink是完全事務性的。在從Channel批量刪除數據之前,每個Sink用Channel啓動一個事務。批量事件一旦成功寫出到存儲系統或下一個Flume Agent,Sink就利用Channel提交事務。事務一旦被提交,該Channel從自己的內部緩衝區刪除事件。
    Sink組件目的地包括hdfs、logger、avro、thrift、ipc、file、null、HBase、solr、自定義。
  5. Event
    傳輸單元,Flume數據傳輸的基本單元,以事件的形式將數據從源頭送至目的地。

Flume拓撲序列

Flume的拓撲結構如圖所示:
在這裏插入圖片描述
圖 Flume Agent連接

在這裏插入圖片描述
圖 單source,多channel、sink

在這裏插入圖片描述
圖 Flume負載均衡

在這裏插入圖片描述
圖 Flume Agent聚合

Flume Agent內部原理

在這裏插入圖片描述

Flume安裝

1) Flume官網地址
http://flume.apache.org/
2)文檔查看地址
http://flume.apache.org/FlumeUserGuide.html
3)下載地址
http://archive.apache.org/dist/flume/
2.2 安裝部署
1)將apache-flume-1.7.0-bin.tar.gz上傳到linux的/opt/software目錄下
2)解壓apache-flume-1.7.0-bin.tar.gz到/opt/module/目錄下

$ tar -zxf apache-flume-1.7.0-bin.tar.gz -C /opt/module/

3)修改apache-flume-1.7.0-bin的名稱爲flume

$ mv apache-flume-1.7.0-bin flume

4)將flume/conf下的flume-env.sh.template文件修改爲flume-env.sh,並配置flume-env.sh文件

$ mv flume-env.sh.template flume-env.sh
$ vi flume-env.sh
export JAVA_HOME=/opt/module/jdk1.8.0_144

開發案例

監控端口數據官方案例
1)案例需求:首先,Flume監控本機44444端口,然後通過telnet工具向本機44444端口發送消息,最後Flume將監聽的數據實時顯示在控制檯。
2)需求分析:
在這裏插入圖片描述
3)實現步驟:
1.安裝telnet工具

yum install telnet

2.判斷44444端口是否被佔用

$ sudo netstat -tunlp | grep 44444

功能描述:netstat命令是一個監控TCP/IP網絡的非常有用的工具,它可以顯示路由表、實際的網絡連接以及每一個網絡接口設備的狀態信息。
基本語法:netstat [選項]
選項參數:
-t或–tcp:顯示TCP傳輸協議的連線狀況;
-u或–udp:顯示UDP傳輸協議的連線狀況;
-n或–numeric:直接使用ip地址,而不通過域名服務器;
-l或–listening:顯示監控中的服務器的Socket;
-p或–programs:顯示正在使用Socket的程序識別碼和程序名稱;

3.創建Flume Agent配置文件flume-telnet-logger.conf
在flume目錄下創建job文件夾並進入job文件夾。

$mkdir job
$ cd job/

在job文件夾下創建Flume Agent配置文件flume-telnet-logger.conf。

$ touch flume-telnet-logger.conf

在flume-telnet-logger.conf文件中添加如下內容。

vim flume-telnet-logger.conf

添加內容如下:

# Name the components on this agent     #a1表示agent名稱
a1.sources = r1 	   				    #r1:表示a1的輸入源
a1.sinks = k1						    #k1:表示a1的輸出目的地
a1.channels = c1 				 		#c1:表示a1的緩衝區

# Describe/configure the source
a1.sources.r1.type = netcat				#表示a1輸入源的類型爲netcat端口類型
a1.sources.r1.bind = localhost			#表示a1的監聽主機
a1.sources.r1.port = 44444				#表示a1的監聽端口

# Describe the sink
a1.sinks.k1.type = logger				#表示a1的輸出目的地控制檯logger類型

# Use a channel which buffers events in memory
a1.channels.c1.type = memory			#表示a1的channel類型是memory
a1.channels.c1.capacity = 1000			#表示a1的channel的總容量1000個event
a1.channels.c1.transactionCapacity = 100#表示a1的channel傳輸時收集到了100條event以後再去提交事務

# Bind the source and sink to the channel
a1.sources.r1.channels = c1				#表示將r1和c1連接起來
a1.sinks.k1.channel = c1				#表示將k1和c1連接起來

注:配置文件來源於官方手冊http://flume.apache.org/FlumeUserGuide.html

  1. 先開啓flume監聽端口
$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-telnet-logger.conf -Dflume.root.logger=INFO,console

參數說明:
–conf conf/ :表示配置文件存儲在conf/目錄
–name a1 :表示給agent起名爲a1
–conf-file job/flume-telnet.conf :flume本次啓動讀取的配置文件是在job文件夾下的flume-telnet.conf文件。
-Dflume.root.logger==INFO,console :-D表示flume運行時動態修改flume.root.logger參數屬性值,並將控制檯日誌打印級別設置爲INFO級別。日誌級別包括:log、info、warn、error。

5.使用telnet工具向本機的44444端口發送內容

$ telnet localhost 44444

實時讀取本地文件到HDFS案例

1)案例需求:實時監控Hive日誌,並上傳到HDFS中
2)需求分析:
在這裏插入圖片描述
3)實現步驟:
1.Flume要想將數據輸出到HDFS,必須持有Hadoop相關jar包
將commons-configuration-1.6.jar、
hadoop-auth-2.7.2.jar、
hadoop-common-2.7.2.jar、
hadoop-hdfs-2.7.2.jar、
commons-io-2.4.jar、
htrace-core-3.1.0-incubating.jar
拷貝到/opt/module/flume/lib文件夾下。
2.創建flume-file-hdfs.conf文件
創建文件

$ touch flume-file-hdfs.conf

注:要想讀取Linux系統中的文件,就得按照Linux命令的規則執行命令。由於Hive日誌在Linux系統中所以讀取文件的類型選擇:exec即execute執行的意思。表示執行Linux命令來讀取文件。

$ vim flume-file-hdfs.conf

添加如下內容

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2

# Describe/configure the source
#定義source類型爲exec可測性命令
a2.sources.r2.type = exec
#執行的命令 監聽文件
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log
執行shell腳本的絕對路徑
a2.sources.r2.shell = /bin/bash -c

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/flume/%Y%m%d/%H
#上傳文件的前綴
a2.sinks.k2.hdfs.filePrefix = logs-
#是否按照時間滾動文件夾
a2.sinks.k2.hdfs.round = true
#多少時間單位創建一個新的文件夾
a2.sinks.k2.hdfs.roundValue = 1
#重新定義時間單位
a2.sinks.k2.hdfs.roundUnit = hour
#是否使用本地時間戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a2.sinks.k2.hdfs.batchSize = 1000
#設置文件類型,可支持壓縮
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一個新的文件
a2.sinks.k2.hdfs.rollInterval = 600
#設置每個文件的滾動大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a2.sinks.k2.hdfs.rollCount = 0
#最小冗餘數
a2.sinks.k2.hdfs.minBlockReplicas = 1

# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

實時讀取目錄文件到HDFS案例

1)案例需求:使用Flume監聽整個目錄的文件
2)需求分析:
在這裏插入圖片描述
3)實現步驟:
1.創建配置文件flume-dir-hdfs.conf
創建一個文件

$ touch flume-dir-hdfs.conf

打開文件

$ vim flume-dir-hdfs.conf

添加如下內容

a3.sources = r3
a3.sinks = k3
a3.channels = c3

# Describe/configure the source
#定義source類型爲目錄
a3.sources.r3.type = spooldir
#定義監控命令
a3.sources.r3.spoolDir = /opt/module/flume/upload
#定義文件上傳完,後綴
a3.sources.r3.fileSuffix = .COMPLETED
#是否有文件頭
a3.sources.r3.fileHeader = true
#忽略所有以.tmp結尾的文件,不上傳
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H
#上傳文件的前綴
a3.sinks.k3.hdfs.filePrefix = upload-
#是否按照時間滾動文件夾
a3.sinks.k3.hdfs.round = true
#多少時間單位創建一個新的文件夾
a3.sinks.k3.hdfs.roundValue = 1
#重新定義時間單位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地時間戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a3.sinks.k3.hdfs.batchSize = 100
#設置文件類型,可支持壓縮
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一個新的文件
a3.sinks.k3.hdfs.rollInterval = 600
#設置每個文件的滾動大小大概是128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a3.sinks.k3.hdfs.rollCount = 0
#最小冗餘數
a3.sinks.k3.hdfs.minBlockReplicas = 1

# Use a channel which buffers events in memory
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

單數據源多出口案例(選擇器)

單Source多Channel、Sink如圖7-2所示。

在這裏插入圖片描述
圖7-2 單Source多Channel、Sink
1)案例需求:使用Flume-1監控文件變動,Flume-1將變動內容傳遞給Flume-2,Flume-2負責存儲到HDFS。同時Flume-1將變動內容傳遞給Flume-3,Flume-3負責輸出到Local FileSystem。
2)需求分析:
在這裏插入圖片描述
3)實現步驟:
0.準備工作
在/opt/module/flume/job目錄下創建group1文件夾

$ cd group1/

在/opt/module/datas/目錄下創建flume3文件夾

$ mkdir flume3

1.創建flume-file-flume.conf
配置1個接收日誌文件的source和兩個channel、兩個sink,分別輸送給flume-flume-hdfs和flume-flume-dir。
創建配置文件並打開

$ touch flume-file-flume.conf
$ vim flume-file-flume.conf

添加如下內容

# Name the components on this agent
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# 將數據流複製給所有channel
a1.sources.r1.selector.type = replicating

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/module/hive/logs/hive.log
a1.sources.r1.shell = /bin/bash -c

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop102 
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop102
a1.sinks.k2.port = 4142

# Describe the channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

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

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

注:Avro是由Hadoop創始人Doug Cutting創建的一種語言無關的數據序列化和RPC框架。
注:RPC(Remote Procedure Call)—遠程過程調用,它是一種通過網絡從遠程計算機程序上請求服務,而不需要了解底層網絡技術的協議。

2.創建flume-flume-hdfs.conf
配置上級Flume輸出的Source,輸出是到HDFS的Sink。
創建配置文件並打開

$ touch flume-flume-hdfs.conf
$ vim flume-flume-hdfs.conf

添加如下內容

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

# Describe/configure the source
a2.sources.r1.type = avro
a2.sources.r1.bind = hadoop102
a2.sources.r1.port = 4141

# Describe the sink
a2.sinks.k1.type = hdfs
a2.sinks.k1.hdfs.path = hdfs://hadoop102:9000/flume2/%Y%m%d/%H
#上傳文件的前綴
a2.sinks.k1.hdfs.filePrefix = flume2-
#是否按照時間滾動文件夾
a2.sinks.k1.hdfs.round = true
#多少時間單位創建一個新的文件夾
a2.sinks.k1.hdfs.roundValue = 1
#重新定義時間單位
a2.sinks.k1.hdfs.roundUnit = hour
#是否使用本地時間戳
a2.sinks.k1.hdfs.useLocalTimeStamp = true
#積攢多少個Event才flush到HDFS一次
a2.sinks.k1.hdfs.batchSize = 100
#設置文件類型,可支持壓縮
a2.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新的文件
a2.sinks.k1.hdfs.rollInterval = 600
#設置每個文件的滾動大小大概是128M
a2.sinks.k1.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a2.sinks.k1.hdfs.rollCount = 0
#最小冗餘數
a2.sinks.k1.hdfs.minBlockReplicas = 1

# Describe the channel
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100

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

3.創建flume-flume-dir.conf
配置上級Flume輸出的Source,輸出是到本地目錄的Sink。
創建配置文件並打開

$ touch flume-flume-dir.conf
$ vim flume-flume-dir.conf

添加如下內容

# Name the components on this agent
a3.sources = r1
a3.sinks = k1
a3.channels = c2

# Describe/configure the source
a3.sources.r1.type = avro
a3.sources.r1.bind = hadoop102
a3.sources.r1.port = 4142

# Describe the sink
a3.sinks.k1.type = file_roll
a3.sinks.k1.sink.directory = /opt/module/datas/flume3

# Describe the channel
a3.channels.c2.type = memory
a3.channels.c2.capacity = 1000
a3.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a3.sources.r1.channels = c2
a3.sinks.k1.channel = c2

提示:輸出的本地目錄必須是已經存在的目錄,如果該目錄不存在,並不會創建新的目錄。
4.執行配置文件
分別開啓對應配置文件:flume-flume-dir,flume-flume-hdfs,flume-file-flume。

$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/group1/flume-flume-dir.conf
$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/group1/flume-flume-hdfs.conf
$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/group1/flume-file-flume.conf

5.啓動Hadoop和Hive

$ sbin/start-dfs.sh
$ sbin/start-yarn.sh
$ bin/hive
hive (default)>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章