Exec source 適用於監控一個實時追加的文件,但不能保證數據不丟失;Spooldir Source 能夠保證數據不丟失,且能夠實現斷點續傳,但延遲較高,不能實時監控;而 Taildir Source 既能夠實現斷點續傳,又可以保證數據不丟失,還能夠進行實時監控。
一、創建配置文件 flume-taildir-hdfs.conf
https://flume.apache.org/FlumeUserGuide.html#taildir-source
監控 /tmp/upload/ 目錄下以 COMPLETED 結尾的文件
a3.sources = r3 a3.sinks = k3 a3.channels = c3 # Describe/configure the source a3.sources.r3.type = TAILDIR a3.sources.r3.filegroups = f1 a3.sources.r3.filegroups.f1 = /tmp/upload/.*COMPLETED a3.sources.r3.positionFile = /opt/apache-flume-1.9.0-bin/tail_dir.json # Describe the sink a3.sinks.k3.type = hdfs a3.sinks.k3.hdfs.path = hdfs://h136:9000/flume/tailDir/%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
二、啟動
cd /opt/apache-flume-1.9.0-bin/ bin/flume-ng agent --conf conf/ --name a3 --conf-file /tmp/flume-job/flume-taildir-hdfs.conf -Dflume.root.logger=INFO,console
三、改動監視文件
echo '123' >> /tmp/upload/123.txt.COMPLETED echo '456' >> /tmp/upload/456.txt.COMPLETED echo '789' >> /tmp/upload/789.txt.COMPLETED
查看 HDFS 上的文件
Taildir 說明:Taildir Source 維護了一個 json 格式的 position File,其會定期的往 position File 中更新每個文件讀取到的最新的位置,因此能夠實現斷點續傳。Position File 的格式如下:
[ {"inode":1717446,"pos":8,"file":"/tmp/upload/456.txt.COMPLETED"}, {"inode":1717449,"pos":8,"file":"/tmp/upload/789.txt.COMPLETED"}, {"inode":1717442,"pos":12,"file":"/tmp/upload/123.txt.COMPLETED"} ]
Linux 中儲存文件元數據的區域就叫做 inode,每個 inode 都有一個號碼,操作系統用 inode 號碼來識別不同的文件,Unix/Linux 系統內部不使用文件名,而使用 inode 號碼來識別文件。