Flume案例:本地檔案到HDFS
阿新 • • 發佈:2020-09-08
1)案例需求:實時監控Hive日誌,並上傳到HDFS中
2)需求分析:
3)實現步驟:
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檔案夾下。
提示:標紅的jar為1.99版本flume必須引用的jar。其他版本可以不引用。
建立flume-file-hdfs.conf檔案
[jason@hadoop102 job]$ vim flume-file-hdfs.conf
新增如下內容
# Name the components on this agent a2.sources = r2 #定義source a2.sinks = k2 #定義sink a2.channels = c2 #定義channel # Describe/configure the source a2.sources.r2.type = exec #定義source型別為exec可執行命令 a2.sources.r2.command= tail -F /opt/module/hive/logs/hive.log a2.sources.r2.shell = /bin/bash -c #執行shell指令碼的絕對路徑 # 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
注:要想讀取Linux系統中的檔案,就得按照Linux命令的規則執行命令。由於hive日誌在Linux系統中所以讀取檔案的型別選擇:exec即execute執行的意思。表示執行Linux命令來讀取檔案。
4)執行監控配置
[jason@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
5)開啟hadoop和hive並操作hive產生日誌
[jason@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh [jason@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh [jason@hadoop102 hive]$ bin/hive hive (default)>
6)在HDFS上檢視檔案