一、Flume 安裝部署
1.1 安裝地址
1、Flume 官網地址
http://flume.apache.org/
2、下載地址
http://archive.apache.org/dist/flume/
3、文檔地址
http://flume.apache.org/FlumeUserGuide.html
1.2 安裝部署
1、將 apache-flume-1.7.0-bin.tar.gz 上傳到服務器的/opt/software 目錄下
2、解壓 apache-flume-1.7.0-bin.tar.gz 到/opt/module/目錄下
[test@hadoop151 software]$ tar -zxvf apache-flume-1.7.0-bin.tar.gz -C /opt/module/
3、修改 apache-flume-1.7.0-bin 的名稱爲 flume
[test@hadoop151 module]$ mv apache-flume-1.7.0-bin/ flume
4、將 flume/conf 下的 flume-env.sh.template 文件修改爲 flume-env.sh,並配置 flume-env.sh 文件
export JAVA_HOME=/opt/module/jdk1.8.0_144
二、入門案例 – 監控端口數據官方案例
2.1 案例介紹
1、案例需求
使用 Flume 監聽一個端口,收集該端口數據,並打印到控制檯。
2、需求分析
2.2 案例步驟
1、安裝 netcat 工具
[test@hadoop151 conf]$ sudo yum install -y nc
2、判斷 44444 端口是否被佔用
[test@hadoop151 conf]$ sudo netstat -tunlp | grep 44444
3、創建 Flume Agent 配置文件 flume-netcat-logger.conf
(1) 在 flume 目錄下創建 job 文件夾並進入 job 文件夾
[test@hadoop151 flume]$ mkdir job
[test@hadoop151 flume]$ cd job/
(2) 在 job 文件夾下創建 Flume Agent 配置文件 flume-netcat-logger.conf
[test@hadoop151 job]$ vim flume-netcat-logger.conf
(3) 在 flume-netcat-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 = netcat
a1.sources.r1.bind = localhost
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
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
4、配置文件解析
注:配置文件來源於官方手冊 http://flume.apache.org/FlumeUserGuide.html
5、開啓 flume 監聽端口
第一種寫法:
bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-netcat-logger.conf Dflume.root.logger=INFO,console
第二種寫法:
bin/flume-ng agent -c conf/ -n a1 -f job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
參數說明:
–conf/-c:表示配置文件存儲在 conf/目錄
–name/-n:表示給 agent 起名爲 a1
–conf-file/-f:flume 本次啓動讀取的配置文件是在 job 文件夾下的 flume-telnet.conf 文件。
-Dflume.root.logger=INFO,console :-D 表示 flume 運行時動態修改 flume.root.logger 參數屬性值,並將控制檯日誌打印級別設置爲 INFO 級別。日誌級別包括:log、info、warn、error。
6、使用 netcat 工具向本機的 44444 端口發送內容
[test@hadoop151 jdk1.8.0_144]$ nc localhost 44444
hello
OK
123456
OK
7、在 Flume 監聽頁面觀察接收數據情況
三、入門案例 – 實時監控單個追加文件
3.1 案例介紹
1、案例需求
實時監控 Hive 日誌,並上傳到 HDFS 中。
2、需求分析
3.2 案例步驟
1、Flume 要想將數據輸出到 HDFS,必須持有 Hadoop 相關 jar 包
將下面的 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 文件
創建文件
[test@hadoop151 job]$ vim flume-file-hdfs.conf
注:要想讀取 Linux 系統中的文件,就得按照 Linux 命令的規則執行命令。由於 Hive 日誌 在 Linux 系統中所以讀取文件的類型選擇:exec 即 execute 執行的意思。表示執行 Linux命令來讀取文件。
添加如下內容:
# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /opt/module/hive/logs/hive.log
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop151: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 = 30
# 設置每個文件的滾動大小
a2.sinks.k2.hdfs.rollSize = 134217700
# 文件的滾動與 Event 數量無關
a2.sinks.k2.hdfs.rollCount = 0
# 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
注意:
對於所有與時間相關的轉義序列,Event Header 中必須存在以 “timestamp” 的 key(除非hdfs.useLocalTimeStamp 設置爲 true,此方法會使用 TimestampInterceptor 自動添加 timestamp)。
a3.sinks.k3.hdfs.useLocalTimeStamp = true
3、運行 Flume
[test@hadoop151 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
4、開啓 Hadoop 和 Hive 並操作 Hive 產生日誌
[test@hadoop151 jdk1.8.0_144]$ start-dfs.sh
[test@hadoop152 ~]$ start-yarn.sh
hive (default)>
5、在 HDFS 上查看文件
四、入門案例 – 實時監控目錄下多個新文件
4.1 案例介紹
1、案例需求
使用 Flume 監聽整個目錄的文件,並上傳至 HDFS。
2、需求分析
4.2 案例步驟
1、創建配置文件
[test@hadoop151 job]$ vim flume-dir-hdfs.conf
添加如下內容:
a3.sources = r3
a3.sinks = k3
a3.channels = c3
#Describe/configure the 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://hadoop151: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 = 60
#設置每個文件的滾動大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滾動與 Event 數量無關
a3.sinks.k3.hdfs.rollCount = 0
# 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
2、參數解析
3、啓動監控文件夾命令
[test@hadoop151 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf
說明:在使用 Spooling Directory Source 時不要在監控目錄中創建並持續修改文件,上傳完成的文件會以.COMPLETED 結尾,被監控文件夾每 500 毫秒掃描一次文件變動。
4、向待監控文件夾中添加文件
[test@hadoop151 flume]$ mkdir upload
[test@hadoop151 flume]$ cd upload/
[test@hadoop151 upload]$ touch 1.txt
[test@hadoop151 upload]$ touch 2.tmp
[test@hadoop151 upload]$ touch 3.log
5、查看 HDFS 上的數據
五、入門案例 – 實時監控目錄下的多個追加文件
5.1 各個 Source 介紹
Exec source 適用於監控一個實時追加的文件,但不能保證數據不丟失;Spooldir Source 能夠保證數據不丟失,且能夠實現斷點續傳,但延遲較高,不能實時監控;而 Taildir Source 既能夠實現斷點續傳,又可以保證數據不丟失,還能夠進行實時監控。
5.2 案例分析
1、需求
使用 Flume 監聽整個目錄的實時追加文件,並上傳至 HDFS。
2、需求分析
5.3 項目步驟
1、創建配置文件 flume-taildir-hdfs.conf
在文件中添加如下內容:
a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = TAILDIR
a3.sources.r3.positionFile = /opt/module/flume/tail_dir.json
a3.sources.r3.filegroups = f1
a3.sources.r3.filegroups.f1 = /opt/module/flume/files/file.*
# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop151: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 = 60
#設置每個文件的滾動大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滾動與 Event 數量無關
a3.sinks.k3.hdfs.rollCount = 0
# 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
2、參數解釋
3、啓動監控文件夾命令
[test@hadoop151 flume]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-taildir-hdfs.conf
4、查看 HDFS 文件系統
Taildir 說明:
Taildir Source 維護了一個 json 格式的 position File,其會定期的往 position File
中更新每個文件讀取到的最新的位置,因此能夠實現斷點續傳。 Position File的格式如下: {“inode”:2496272,“pos”:12,“file”:"/opt/module/flume/files/file1.t xt"} {“inode”:2496275,“pos”:12,“file”:"/opt/module/flume/files/file2.t xt"}
注:Linux 中儲存文件元數據的區域就叫做 inode,每個 inode 都有一個號碼,操作系統 用 inode 號碼來識別不同的文件,Unix/Linux 系統內部不使用文件名,而使用 inode 號碼來識別文件。
六、軟件安裝包
flume 安裝包及所需 jar 包都在百度雲盤鏈接:
鏈接:https://pan.baidu.com/s/1irJ38usIHp6C0IkFjBIWxg
提取碼:5xor