天天看點

flume案例--實時采集檔案的内容變化到hdfs

1 配置tail-hdfs.conf檔案

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/test.log

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H-%M/
a1.sinks.k1.hdfs.filePrefix = itcast-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 5
a1.sinks.k1.hdfs.rollSize = 1014
a1.sinks.k1.hdfs.rollCount = 3
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的檔案類型,預設是Sequencefile,可用DataStream,則為普通文本
a1.sinks.k1.hdfs.fileType = DataStream

# 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
           

2 啟動flume

bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1 -Dflume.root.logger=INFO,console
           

3 建立要監控的檔案夾

mkdir -p /root/logs
           

4 建立要監控的檔案

touch /root/logs/test.log
           

5 使用shell模拟資料源源不斷産生

while true;do date >> /root/logs/test.log;sleep 0.5;done

或者:
#/bin/bash
while true
do
  date >> /root/logs/test.log
  sleep 0.5
done  
           

6 測試

2020-06-23 04:53:02,932 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.HDFSDataStream.configure(HDFSDataStream.java:57)] Serializer = TEXT, UseRawLocalFileSystem = false
2020-06-23 04:53:02,943 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.BucketWriter.open(BucketWriter.java:251)] Creating /flume/tailout/20-06-23/04-50//itcast-.1592859182933.tmp
2020-06-23 04:53:07,963 (hdfs-k1-roll-timer-0) [INFO - org.apache.flume.sink.hdfs.BucketWriter.close(BucketWriter.java:393)] Closing /flume/tailout/20-06-23/04-50//itcast-.1592859182933.tmp
2020-06-23 04:53:07,975 (hdfs-k1-call-runner-3) [INFO - org.apache.flume.sink.hdfs.BucketWriter$8.call(BucketWriter.java:655)] Renaming /flume/tailout/20-06-23/04-50/itcast-.1592859182933.tmp to /flume/tailout/20-06-23/04-50/itcast-.1592859182933
2020-06-23 04:53:07,977 (hdfs-k1-roll-timer-0) [INFO - org.apache.flume.sink.hdfs.HDFSEventSink$1.run(HDFSEventSink.java:382)] Writer callback called.
           

7 擴充 conf中配置屬性解釋

#roll 滾動 寫入hdfs的資料以何種方式切換滾動
a1.sinks.k1.hdfs.rollInterval = 3  #以時間間隔控制 預設是30s  
a1.sinks.k1.hdfs.rollSize = 20     #以檔案大小控制  預設1024 bytes
a1.sinks.k1.hdfs.rollCount = 5     #以event個數控制 預設10個
#誰先滿足 觸發條件 就滾動
#如果不想一某個屬性滾動怎麼辦?  設定為0  删除沒有效果 因為有預設值。


#round 想一想hive中round()函數 取整
#功能:是否開啟時間上的舍棄 true表示開啟   通俗解釋:檔案夾以多少時間間隔切換
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute