備註:flume採集文件到hdfs還報錯,後續在更新。
自己還是喜歡+適合用寫文章的方式來學習,很就沒更新文章了。加油成爲更好的自己,努力學習、努力賺錢、努力理財
flume是一個分佈式的、高可靠的、高可用的將大批量的不同數據源的日誌數據收集、集合、移動到數據中心進行存儲的系統。即
日誌採集和彙總工具。
一、監控端口數據
案例需求:使用flume監控一個端口號,並打印到控制檯
1、cd /usr/flume/conf 在conf目錄下新建netcat-flume-logger.conf文件
# example.conf: A single-node Flume configuration
# 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
整個配置文件內容分爲三個大部分:
- 從整體上描述代理agent中sources、sinks、channels所涉及到的組件;
- 詳細描述agent中每一個
source、sink與channel
的具體實現;Source使用了netcat,指定綁定的主機以及端口號;Sink按照logger的方式進行配置 - 通過channel將source與sink連接起來
2、啓動Flume
cd /usr/flume,在flume目錄下執行命名
bin/flume-ng agent --conf conf --name a1 --conf-file job/netcat-flume-logger.conf -Dflume.root.logger=INFO,console
參數說明:
--conf : 表示配置文件存儲在conf目錄
--name : 表示給agent取一個名字
--conf-file : flume本次啓動讀取的配置文件是在job文件夾下面的netcat-flume-logger.conf
文件
-Dflume.root.logger=INFO,console : -D
表示flume運行時動態修改flume.root.logger
參數屬性值,並將控制檯日誌打印級別設置爲INFO
級別。
日誌級別包括:log、info 、warn
3、監聽端口並輸入一些測試信息
nc localhost 44444
二、實時監控單個追加的文件,並上傳到HDFS
案例需求:實時監控hive日誌並上傳到hdfs
步驟操作分爲兩步:首先把動態的文件打印到控制檯,因爲打印到控制檯比較熟悉;然後在監控動態的文件把數據傳到HDFS
第一步:把動態的文件打印到控制檯
1、cd /usr/flume/conf 在conf目錄下新建file-flume-logger.conf文件
#name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#source config
a1.sources.r1.type = exec
#監控本地文件tail -f,tail -f默認讀取文件的後10行。
a1.sources.r1.command = tail -f /home/atguigu/data/flume_test.txt
#channels config
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#sink cinfig
a1.sinks.k1.type = logger
#bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
- 可自行查看
tail -f
與tail -F
的區別
2 、啓動flume
1)cd /usr/flume,在flume目錄下執行命名
bin/flume-ng agent --conf conf --conf-file job/file-flume-logger.conf --name a1 -Dflume.root.logger=INFO,console
3、往文件追加內容 ,並輸入一些測試信息
echo "bbccdd" >> flume_test.txt
查看flume
第二步:監控動態的文件把數據傳到HDFS
有問題,上傳不到hdfs,還沒找到原因
flume想把數據寫到hdfs上,需要持有hadoop相關的jar包。flume啓動時會加載lib目錄下的所有jar包到環境變量中。
1、cd /usr/flume/conf 在conf目錄下新建file-flume-hdfs.conf文件
#1)name
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#2)sources configure
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/atguigu/data/flume_test.txt
#3)sinks configure
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://bigdata02:9092/flume/events/%y-%m-%d/%H
#文件的前綴
a1.sinks.k1.hdfs.filePrefix = log-
#是否按照時間滾動文件夾,設置爲按照時間滾動文件夾
a1.sinks.k1.hdfs.round = true
#多少時間單位創建一個新的文件夾
a1.sinks.k1.hdfs.roundValue = 1
#重新定義時間單位
a1.sinks.k1.hdfs.roundUnit = hour
#積攢多少個event才刷新到hdfs
a1.sinks.k1.hdfs.batchSize = 10
#文件的壓縮格式
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一個新文件,因爲是測試環境,設置爲60秒生成一個新文件
a1.sinks.k1.hdfs.rollInterval = 60
#設置每個文件的滾動大小
a1.sinks.k1.hdfs.hdfs.rollSize = 13421770
#設置文件的滾動與數量無關
a1.sinks.k1.hdfs.rollCount = 0
#是否使用本地時間戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#4)channels configure
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#5)bind configure
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
hdfs sink配置參數
參考文章:hdfs sink 使用技巧
1)文件滾動參數設置
如果要配置文件滾動,hdfs.rollInterval
、hdfs.rollSize
、hdfs.rollCount
這三個參數需要一起配置
hdfs.rollInterval :
基於時間間隔來進行文件滾動(多久生成一個新文件),默認是30,即每隔30秒滾動一個文件。0就是不使用這個策略。
hdfs.rollSize :
基於文件大小進行文件滾動,默認是1024,即當文件大於1024個字節時,關閉當前文件,創建新的文件。0就是不使用這個策略。
一般文件大小設置要比塊小
hdfs.rollCount
基於event數量進行文件滾動。默認是10,即event個數達到10時進行文件滾動。0就是不使用這個策略。
hdfs.idleTimeout
閒置N秒後,關閉當前文件(去掉.tmp後綴)
2)文件名策略
hdfs.filePrefix
文件前綴,默認是FlumeData
hdfs.fileSuffix
文件後綴,默認沒有
三、實時監控文件夾,並上傳到HDFS
使用組件:taildir source、memroy channel、hdfs sink
1、cd /usr/flume/conf 在conf目錄下新建taildir-file-hdfs.conf文件
#每行註釋在代碼下方
a3.sources = r3
#定義sources
a3.sinks = k3
#定義sinks
a3.channels = c3
#定義channels
# Describe/configure the source
a3.sources.r3.type = spooldir
#表示定義source的類型是目錄類型
a3.sources.r3.spoolDir = /home/study/hive_project/logstart
#定義監控目錄的具體位置
a3.sources.r3.fileSuffix = .COMPLETED
#文件上傳完畢後的後綴
a3.sources.r3.fileHeader = true
#表示是否有文件頭
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
#忽略所有以tmp結尾的文件,不上傳
# Describe the sink
a3.sinks.k3.type = hdfs
#sink的類型是hdfs
a3.sinks.k3.hdfs.path = /origin_data/mall/logstart/%Y-%m-%d
#數據目的地的具體路徑
a3.sinks.k3.hdfs.filePrefix = logstart-
#上傳的文件以logstart前綴
a3.sinks.k3.hdfs.round = true
#是否按照時間滾動文件
a3.sinks.k3.hdfs.roundValue = 1
#多少時間單位創建一個新的文件
a3.sinks.k3.hdfs.roundUnit = hour
#時間的單位 小時
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#是否使用本地的時間戳
a3.sinks.k3.hdfs.batchSize = 100
#積累了多少event才能刷寫到hdfs一次
a3.sinks.k3.hdfs.fileType = DataStream
#設置文件類型
a3.sinks.k3.hdfs.rollInterval = 600
#多久生成新文件
a3.sinks.k3.hdfs.rollSize = 134217700
#生成多大的新文件
a3.sinks.k3.hdfs.rollCount = 0
#多少enevt生成新文件
a3.sinks.k3.hdfs.minBlockReplicas = 1
#多少副本數
# Use a channel which buffers events in memory
a3.channels.c3.type = memory
#表示a3的類型是channel類型是menory(內存)類型
a3.channels.c3.capacity = 1000
#表示a3的channel的總容量爲1000個enevt
a3.channels.c3.transactionCapacity = 100
#表示a3的channel在傳輸的時候收集到100個enevt再去提交事務
# Bind the source and sink to the channel
a3.sources.r3.channels = c3
#表示把r3和c3連接起來
a3.sinks.k3.channel = c3
#表示將K3和c3連接起來
2 、啓動flume
cd /usr/flume,在flume目錄下執行命名
bin/flume-ng agent --conf conf --conf-file job/taildir-file-hdfs.conf --name a3
3、查看採集信息
1)監控文件夾
2)HDFS上傳數據