Flume自探
自己的話:每天都要保持前進
多認識更加優秀的人,你會討厭現在的自己。
Flume實戰案例
一、監控端口數據官方案例
1. 案例需求:
首先,Flume 監控本機 44444 端口,然後通過 telnet 工具向本機 44444 端口發
送消息,最後 Flume 將監聽的數據實時顯示在控制檯。
2. 需求分析:
3. 實現步驟:
3.1 安裝telnet工具
yum -y install telnet
3.2 判斷44444端口是否被佔用
netstat -tunlp | grep 44444
功能描述:netstat 命令是一個監控 TCP/IP 網絡的非常有用的工具,它可以顯示路由表、
實際的網絡連接以及每一個網絡接口設備的狀態信息。
基本語法:netstat [選項]
選項參數:
- -t 或–tcp:顯示 TCP 傳輸協議的連線狀況;
- -u 或–udp:顯示 UDP 傳輸協議的連線狀況;
- -n 或–numeric:直接使用 ip 地址,而不通過域名服務器;
- -l 或–listening:顯示監控中的服務器的 Socket; - -p 或–programs:顯示正在使用 Socket 的程序識別碼和程序名稱
3.3 創建 Flume Agent 配置文件 flume-telnet-logger.conf
在 flume 目錄下創建 job 文件夾並進入 job 文件夾。
[hadoop1 flume]$ mkdir job
[hadoop1 flume]$ cd job/
在 job 文件夾下創建 Flume Agent 配置文件 flume-telnet-logger.conf。
[hadoop1 job]$ touch flume-telnet-logger.conf
在 flume-telnet-logger.conf 文件中添加如下內容。
[hadoop1 job]$ vim flume-telnet-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
配置文件解析:
3.4 先開啓 flume 監聽端口
[hadoop1 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。
3.5 使用 telnet 工具向本機的 44444 端口發送內容
[hadoop1 ~]$ telnet localhost 44444
3.6 在 Flume 監聽頁面觀察接收數據情況
二、實時讀取本地文件到 HDFS 案例
1. 案例需求:
實時監控 Hive 日誌,並上傳到 HDFS 中
2. 需求分析:
3. 實現步驟:
根據需求,首先定義以下3大要素:
- 採集源,即source——監控文件內容更新 : exec ‘tail -F file’
- 下沉目標,即sink——HDFS文件系統 : hdfs sink
- Source和sink之間的傳遞通道——channel,可用file channel 也可以用 內存channel
3.1 創建並編輯 flume-file-hdfs.conf 文件
[hadoop1 job]$ touch flume-file-hdfs.conf
注:要想讀取 Linux 系統中的文件,就得按照 Linux 命令的規則執行命令。由於 Hive
日誌在 Linux 系統中所以讀取文件的類型選擇:exec 即 execute 執行的意思。表示執行 Linux
命令來讀取文件。
[hadoop1 job]$ vim flume-file-hdfs.conf
添加如下內容:
# 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://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
3.2 執行監控配置
[hadoop1 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
3.3 開啓 Hadoop 和 Hive 並操作 Hive 產生日誌
[hadoop1 /]$ start-all.sh
[hadoop1 /]$ hive
hive (default)>