Flume监听文件夹中的文件变化,并把文件下沉到hdfs
生活随笔
收集整理的這篇文章主要介紹了
Flume监听文件夹中的文件变化,并把文件下沉到hdfs
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、采集目錄到HDFS
采集需求:某服務器的某特定目錄下,會不斷產生新的文件,每當有新文件出現,就需要把文件采集到HDFS中去
根據需求,首先定義以下3大要素
采集源,即source——監控文件目錄 : spooldir
下沉目標,即sink——HDFS文件系統 : hdfs sink
source和sink之間的傳遞通道——channel,可用file channel 也可以用內存channel
配置文件spooldir-hdfs.conf編寫:
# Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1# Describe/configure the source ##注意:不能往監控目中重復丟同名文件 ## 通過spooldir來監控文件內容的變化 a1.sources.r1.type = spooldir a1.sources.r1.spoolDir = /home/tuzq/software/flumedata a1.sources.r1.fileHeader = true# Describe the sink ## 表示下沉到hdfs,下面配置的類型不同,type下面的參數就不同 a1.sinks.k1.type = hdfs #sinks.k1只能連接一個channel,source可以配置多個 a1.sinks.k1.channel = c1 #下面的配置告訴用hdfs去寫文件的時候寫到什么位置,下面的表示不是寫死的,而是動態變化的。表示輸出的目錄名稱是可變的 a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/ #表示文件的前綴 a1.sinks.k1.hdfs.filePrefix = events- #表示到了需要觸發的時間時,是否要更新文件夾,true:表示要更新 a1.sinks.k1.hdfs.round = true ##表示每隔1分鐘改變一下文件夾 a1.sinks.k1.hdfs.roundValue = 1 ##切換文件的時候單位是分鐘 a1.sinks.k1.hdfs.roundUnit = minute ##表示只要過了3秒鐘,就切換生成一個新的文件 a1.sinks.k1.hdfs.rollInterval = 3 ##如果記錄的文件大于20字節時切換一次 a1.sinks.k1.hdfs.rollSize = 20 ##當寫了5個事件時觸發 a1.sinks.k1.hdfs.rollCount = 5 ##收到了多少條消息往hdfs中追加內容 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 = c1Channel參數解釋:
capacity:默認該通道中最大的可以存儲的event數量
trasactionCapacity:每次最大可以從source中拿到或者送到sink中的event數量
keep-alive:event添加到通道中或者移出的允許時間
執行命令
[root@hadoop1 apache-flume-1.6.0-bin]#cd /home/tuzq/software/apache-flume-1.6.0-bin [root@hadoop1 apache-flume-1.6.0-bin]#bin/flume-ng agent -c ./conf -f ./agentconf/spool-logger.conf -n a1 -Dflume.root.logger=INFO,console;接著往/home/tuzq/software/flumedata文件夾中扔文件
[root@hadoop1 flumedata]# pwd /home/tuzq/software/flumedata [root@hadoop1 flumedata]# echo 111111111 >> 1.txt [root@hadoop1 flumedata]# ls 1.txt.COMPLETED test.log.COMPLETED [root@hadoop1 flumedata]# echo 22222222 >> 2.txt [root@hadoop1 flumedata]# echo 33333333 >> 3.txt [root@hadoop1 flumedata]# echo 44444444 >> 4.txt [root@hadoop1 flumedata]# ls 1.txt.COMPLETED 2.txt.COMPLETED 3.txt.COMPLETED 4.txt.COMPLETED test.log.COMPLETED [root@hadoop1 flumedata]#扔了之后,現象是
1、/home/tuzq/software/flumedata文件文件夾下的文件倍加了一個一個后綴.COMPLETED,
2、在flume的監控位置,出現類似下圖一樣的文件:
3、到hdfs上查看文件:
綜上所述:說明通過flume已經把新增的文件下沉到了hdfs中。
總結
以上是生活随笔為你收集整理的Flume监听文件夹中的文件变化,并把文件下沉到hdfs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蟹黄为什么是苦的?
- 下一篇: 糖尿病人打胰岛素血糖还是高该怎么办(表嫂