實時監控,並上傳到 HDFS 中。
一、Flume 要想將數據輸出到 HDFS,須持有 Hadoop 相關 jar 包
若 Hadoop 環境和 Flume 在同一節點,那么只要配置 Hadoop 環境變量即可,不需要復制相關 jar 包。
# 將相關包拷貝到 flume 的 lib 目錄下 # commons-configuration-x.x.jar # hadoop-auth-x.x.x.jar、 # hadoop-common-x.x.x.jar、 # hadoop-hdfs-x.x.x.jar、 # commons-io-x.x.jar、 # htrace-core-x.x.x-incubating.jar cp /opt/hadoop-2.9.2/share/hadoop/hdfs/hadoop-hdfs-2.9.2.jar /opt/apache-flume-1.9.0-bin/lib/ cp /opt/hadoop-2.9.2/share/hadoop/common/hadoop-common-2.9.2.jar /opt/apache-flume-1.9.0-bin/lib/ cp /opt/hadoop-2.9.2/share/hadoop/common/lib/commons-io-2.4.jar /opt/apache-flume-1.9.0-bin/lib/ cp /opt/hadoop-2.9.2/share/hadoop/common/lib/hadoop-auth-2.9.2.jar /opt/apache-flume-1.9.0-bin/lib/ cp /opt/hadoop-2.9.2/share/hadoop/common/lib/commons-configuration-1.6.jar /opt/apache-flume-1.9.0-bin/lib/ cp /opt/hadoop-2.9.2/share/hadoop/common/lib/htrace-core4-4.1.0-incubating.jar /opt/apache-flume-1.9.0-bin/lib/
二、創建 flume-file-hdfs.conf 文件
https://flume.apache.org/FlumeUserGuide.html#exec-source
https://flume.apache.org/FlumeUserGuide.html#flume-sinks
要想讀取 Linux 系統中的文件,就得按照 Linux 命令的規則執行命令。由於 Hive 日志在 Linux 系統中所以讀取文件的類型選擇:exec 即 execute 執行的意思。表示執行 Linux 命令來讀取文件。
# Name the components on this agent # 定義 source a2.sources = r2 # 定義 sink a2.sinks = k2 # 定義 channel a2.channels = c2 # Describe/configure the source # 定義 source 類型為 exec 可執行命令 a2.sources.r2.type = exec a2.sources.r2.command = tail -F /tmp/tomcat.log # 執行 shell 腳本的絕對路徑 a2.sources.r2.shell = /bin/bash -c # Describe the sink a2.sinks.k2.type = hdfs a2.sinks.k2.hdfs.path = hdfs://h136: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 = 100 # 設置文件類型,可支持壓縮 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 的 channel 類型是 memory 內存型 a2.channels.c2.type = memory # 表示 a2 的 channel 總容量 1000 個 event a2.channels.c2.capacity = 1000 # 表示 a2 的 channel 傳輸時收集到了 100 條 event 以后再去提交事務 a2.channels.c2.transactionCapacity = 100 # Bind the source and sink to the channel # 表示將 r2 和 c2 連接起來 a2.sources.r2.channels = c2 # 表示將 k2 和 c2 連接起來 a2.sinks.k2.channel = c2
注意:a2.sinks.k2.hdfs.useLocalTimeStamp = true,對於所有與時間相關的轉義序列,Event Header 中必須存在以 “timestamp” 的 key(除非 hdfs.useLocalTimeStamp 設置為 true,此方法會使用 TimestampInterceptor 自動添加 timestamp)。
三、啟動
在啟動之前需要先啟動 Hadoop 環境。
cd /opt/apache-flume-1.9.0-bin/ bin/flume-ng agent --conf conf/ --name a2 --conf-file /tmp/flume-job/flume-file-hdfs.conf -Dflume.root.logger=INFO,console # 追加日志內容 echo 'add xxxxx' >> /tmp/tomcat.log
HDFS 上的文件