flume組件詳解與測試案例,安裝

Flume

  • 概念:
Flume是一種分佈式、可靠且可用的服務,用於高效地收集、聚合和移動大量日誌數據。它具有基於流數據流的簡單
靈活的體系結構。它具有健壯性和容錯性,具有可調的可靠性機制和許多故障轉移和恢復機制。它使用一個簡單的可
擴展數據模型,允許在線分析應用程序。
  • 架構:

在這裏插入圖片描述

  • 組件
source:數據源組件,用於讀取相應數據,並將數據傳到channel中
channel:管道,用於連接source和sink
sink:數據下沉組件,用於將channel中的數據持久化到對應的文件系統中或者流中
agent:flume的運行單元.裏面必須包含一個或者多個source,channel,sink,運行在單個jvm中
event:事件,是數據的描述
interceptor:攔截器,作用於source階段,用於過濾數據
selectorer:選擇器,作用於source階段,默認是replicating,也就是複用功能,mutiplxing
groupsinks:sink組,用於將多個sink選擇sinkf

常用的組件:
source常用的組件:exec、avro、source、spooling、dirctory、kafka、netcat、http、自定義
channel常用的組件:memory、file、kafka、jdbc
sink常用組件:avro、hdfs、hive、hbase、kafka
  • 安裝
1.解壓到指定目錄
2.cp conf/flume-env.sh.template conf/flume-env.sh
在flume-env.sh下配置JAVA_HOME

1.Flume Sources

1.1 EXEC Source

  • Exec source在啓動時運行一個給定的Unix命令,並期望該進程在標準輸出上不斷地生成數據(除非將屬性logStdErr設置爲true,否則將直接丟棄stderr)。如果進程出於任何原因退出,源也將退出,並且不會產生進一步的數據。這意味着像cat [named pipe]或tail - f [file]這樣的配置將產生預期的結果,而as date可能不會產生這種結果——前兩個命令將產生數據流,而後者將產生單個事件並退出。
  • 屬性詳解
必要屬性:
agent.sources=xx                 //當前source的名稱
agent.sources.xx.channels=xxx     //當前source連接的channel的名稱,因爲一個source可以連接多個channel,所以這的屬性爲channels
agent.sources.xx.type=exec      //當前source的類型
agent.sources.xx.command=xxxx    //當前source在exec類型下的執行命令
其他屬性:
shell               //用於運行命令的shell調用。例如/bin/sh - c。僅適用於依賴於通配符、反勾號、管道等shell特性的命令。
restartThrottle     //嘗試重新啓動前等待的時間(以毫秒爲單位)   默認值:10000
restart             //如果執行的cmd死亡,是否應該重新啓動它  默認值:false
logStdErr           //是否應該記錄命令的stderr  默認值:false
batchSize           //一次讀取和發送到通道的最大行數  默認值:20
batchTimeout        //如果沒有達到緩衝區大小,則在數據被向下推送之前等待的時間(以毫秒爲單位)  默認值:3000
selector.type       //replicating(複用) 或者 multiplexing(多路複用)
interceptors        //以空格分隔的攔截器列表
  • ExecSource和其他異步源的問題是,如果無法將事件放入客戶機知道的通道中,則源不能保證該事件。在這種情況下,數據將丟失。例如,最常見的請求特性之一是tail -F [file]類用例,在這種用例中,應用程序向磁盤上的日誌文件寫入數據,Flume跟蹤文件,將每一行作爲一個事件發送。雖然這是可能的,但有一個明顯的問題;如果通道被填滿,Flume不能發送事件,會發生什麼?Flume無法向編寫日誌文件的應用程序表明它需要保留日誌,或者由於某種原因沒有發送事件。如果這沒有意義,您只需要知道這一點:當使用單向異步接口(如ExecSource)時,您的應用程序永遠不能保證數據已被接收!作爲此警告的擴展(而且要完全清楚),在使用此源時絕對沒有事件交付的保證。要獲得更強的可靠性保證,可以考慮使用假脫機目錄源、Taildir源或通過SDK與Flume直接集成。

1.2 Spooling Directory Source

  • 通過將要攝取的文件放置到磁盤上的“假脫機”目錄中,該源允許您攝取數據。這個源將監視新文件的指定目錄,並在新文件出現時從其中解析事件。事件解析邏輯是可插入的。在將給定文件完全讀入通道後,默認情況下,通過重命名該文件來表示完成,或者可以刪除該文件,或者使用trackerDir跟蹤處理過的文件。
  • 與Exec源不同,該源是可靠的,即使重新啓動或關閉Flume,也不會丟失數據。爲了獲得這種可靠性,只能將不可變的、惟一命名的文件放入假脫機目錄。Flume試圖檢測這些問題條件,如果違反這些條件,將會失敗:
  • 屬性詳解
必要屬性:
agent.sources=xx                  //當前source的名稱
agent.sources.xx.type=spooldir    //當前source的類型
agent.sources.xx.channels=xxx     //當前source連接的channel的名稱
agent.sources.xx.spoolDir=xxxxx	  //要從中讀取文件的目錄
其他屬性:
fileSuffix  			//附加到完全讀取的文件的後綴  默認值:.COMPLETED
deletePolicy            //何時刪除已完成的文件, never or immediate 默認值:never
fileHeader				//是否添加存儲絕對路徑文件名的頭文件  默認值:false
fileHeaderKey           //標題鍵,當將絕對路徑文件名附加到事件標題時使用 默認值:file
basenameHeader			//是否添加存儲文件基本名的頭文件。 默認值:false
basenameHeaderKey       //當將文件的基本名稱附加到事件標頭時使用的標頭鍵。  默認值:basename
includePattern			//指定要包含哪些文件的正則表達式。它可以與ignorePattern一起使用。如果文件同時匹配ignorePattern和includePattern regex,則忽略該文件。  默認值:^.*$
ignorePattern			//指定要忽略(跳過)哪些文件的正則表達式。可與其它助劑配套使用。如果文件同時匹配ignorePattern和includePattern regex,則忽略該文件。 默認值: ^$
trackerDir		        //目錄用於存儲與處理文件相關的元數據。如果這條路徑不是絕對路徑,那麼它將被解釋爲spoolDir。 默認值:.flumespool
trackingPolicy          //跟蹤策略定義如何跟蹤文件處理。它可以是“rename”或“tracker_dir”。只有當deletePolicy爲“never”時,此參數纔有效。“rename”——在處理文件之後,根據fileSuffix參數對文件進行重命名。“tracker_dir”——文件沒有重命名,但是在trackerDir中創建了一個新的空文件。新的跟蹤器文件名派生自已攝入的文件和fileSuffix。
inputCharset            //將輸入文件視爲文本的反序列化器使用的字符集。  默認值:UTF-8
pollDelay               //輪詢新文件時的延遲(以毫秒爲單位)  默認值:500
maxBackoff              //如果通道已滿,在連續嘗試寫入通道之間等待的最長時間(以毫秒爲單位)。源將從一個較低的回退開始,並在每次通道拋出ChannelException時以指數級增長,直到該參數指定的值爲止。 默認值:4000
recursiveDirectorySearch//是否要監視新文件讀取的子目錄。 默認值:false
batchSize				//批處理傳輸到通道的粒度  默認值:100
decodeErrorPolicy       //當我們在輸入文件中看到一個不可解碼的字符時該怎麼辦?FAIL:拋出異常並無法解析文件。REPLACE:用“替換字符”字符替換不可解析的字符,通常是Unicode U+FFFD。IGNORE:刪除不可解析的字符序列。 默認值:FAIL
deserializer			//指定用於將文件解析爲事件的反序列化器。默認情況下將每一行解析爲一個事件。指定的類必須實現eventdeseriizer . builder  默認值:LINE
deserializer.*   		//不同的事件反序列化器。
selector.type           //選擇器的類型,replicating or multiplexing  默認值:replicating
selector.* 	 	 	 	//取決於選擇器。類型值
interceptors            //以空格分隔的攔截器列表

1.3 Syslog Tcp Source

  • 讀取syslog數據並生成Flume事件。UDP源將整個消息視爲單個事件。TCP源爲每個用換行符(’ n ')分隔的字符串創建一個新事件。
  • 屬性詳解
必有屬性:
agent.sources=xx;              //當前source的名稱
agent.sources.xx.type=syslogtcp//當前source的類型
agent.sources.xx.port=xxxx	   //要綁定的端口號
agent.sources.xx.host=xxxx     //要綁定到的主機名或IP地址
agent.sources.xx.channels=xxx  //當前source連接的channel的名稱

1.4 HTTP Source

  • 一個通過HTTP POST接受Flume事件的源。應該只用於實驗。HTTP請求通過一個可插入的“處理程序”轉換成flume事件,它必須實現HTTPSourceHandler接口。這個處理程序使用一個HttpServletRequest,並返回一個flume事件列表。從一個Http請求處理的所有事件都在一個事務中提交給通道,從而允許在像文件通道這樣的通道上提高效率。如果處理程序拋出異常,此源將返回400的HTTP狀態。如果通道是完整的,或者源無法向通道附加事件,源將返回HTTP 503 -暫時不可用狀態。
  • 屬性詳解
必有屬性:
agent.sources=xx;              //當前source的名稱
agent.sources.xx.type=http//當前source的類型
agent.sources.xx.port=xxxx	   //要綁定的端口號
agent.sources.xx.channels=xxx  //當前source連接的channel的名稱
其他屬性:
bind			//要監聽的主機名或IP地址
handler         //處理程序類的FQCN  默認值:        org.apache.flume.source.http.JSONHandler 所以測試的數據需要用json格式
ssl             //將屬性設置爲true,以啓用SSL。HTTP源不支持SSLv3。 默認值:false

1.5 Avro Source

  • 監聽Avro端口並從外部Avro客戶端流接收事件。當與另一個(前一層)flume agent上的內置Avro接收器相結合時,它可以創建分層收集拓撲
  • 屬性詳解
必有屬性:
agent.sources=xx;              //當前source的名稱
agent.sources.xx.type=avro//當前source的類型
agent.sources.xx.bind=xxx  //要監聽的主機名或IP地址
agent.sources.xx.port=xxxx	   //要綁定的端口號
agent.sources.xx.channels=xxx  //當前source連接的channel的名稱
其他屬性:
selector.type     //選擇器類型
selector.*		  //選擇器其他屬性
interceptors	  //過濾器名稱
interceptors.*    //過濾器其他屬性
.....

1.6 NetCat TCP Source

  • 一個類似netcat的源程序,它監聽給定的端口並將每一行文本轉換爲一個事件。類似於nc -k -l [host] [port]。換句話說,它打開指定的端口並偵聽數據。期望提供的數據是換行分隔的文本。每一行文本都被轉換成一個Flume事件,並通過連接的通道發送。
  • 屬性詳解
必有屬性:
agent.sources=xx;              //當前source的名稱
agent.sources.xx.type=netcat//當前source的類型
agent.sources.xx.bind=xxx  //要綁定到的主機名或IP地址
agent.sources.xx.port=xxxx	   //要綁定的端口號
agent.sources.xx.channels=xxx  //當前source連接的channel的名稱
其他屬性
selector.type     //選擇器類型
selector.*		  //選擇器其他屬性
interceptors	  //過濾器名稱
interceptors.*    //過濾器其他屬性
.....

2.Flume Channels

2.1 Memory Channel

  • 屬性詳解
//必有屬性
agent.channels=xx				//指定當前channel的名稱
agent.channels.xx.type=memory
//其他屬性
capacity  //存儲在通道中的最大事件數量 默認值:100
transactionCapacity  //通道在每個事務中將從源獲取或提供給接收器的最大事件數 默認值:100
keep-alive //超時(以秒爲單位),用於添加或刪除事件 默認值:3
byteCapacityBufferPercentage //定義字節碼容量與估計的通道中所有事件的總大小之間的緩衝區百分比,以解釋報頭中的數據。 默認值:20
byteCapacity  //允許的最大內存字節數,作爲該通道中所有事件的總和。實現只計算事件體,這也是提供bytecapacitybufferpercent配置參數的原因。默認計算值等於JVM可用的最大內存的80%(即在命令行上傳遞的-Xmx值的80%)。請注意,如果您在一個JVM上有多個內存通道,並且它們碰巧包含相同的物理事件(例如,如果您使用來自單個源的複製通道選擇器),那麼出於通道字節碼的目的,這些事件大小可能會被重複計算。將該值設置爲0將導致該值回落到大約200gb的硬內部限制。



2.2 File Channel

  • 屬性詳解
必有屬性:
agent.channels=xx				//指定當前channel的名稱
agent.channels.xx.type=file     //當前channel的類型
其他屬性:
checkpointDir       //checkpoint文件的存儲路徑  默認值:~/.flume/file-channel/checkpoint
useDualCheckpoints  //備份的檢查站。如果將其設置爲true,則必須設置backupCheckpointDir  默認值:false
backupCheckpointDir  //checkpoint文件備份到的目錄。此目錄不能與dataDir或checkpointDir相同
dataDirs    //目錄列表,用於存儲日誌文件。在單獨的磁盤上使用多個目錄可以提高文件通道的性能  默認值:~/.flume/file-channel/data
transactionCapacity  //通道支持的最大事務大小  默認值 10000
checkpointInterval   //checkpoints的時間間隔,以毫秒爲單位  默認值:30000
maxFileSize     //單個日誌文件的最大大小(以字節爲單位)  默認值:	2146435071 相當於2G
minimumRequiredSpace    //最少需要的空閒空間(以字節爲單位)。爲避免數據損壞,當空閒空間低於此值時,文件通道停止接受take/put請求   默認值:524288000  相當於500M
capacity   //通道的最大容量  默認值:1000000
keep-alive  //等待操作的時間,以秒爲單位  默認值:3
checkpointOnClose   //控制在通道關閉時是否創建檢查點。在關閉時創建檢查點可以避免重播,從而加快文件通道的後續啓動。  默認值:true
encryption.activeKey   //用於加密新數據的密鑰名稱
encryption.cipherProvider  //密碼提供程序類型,支持的類型:AESCTRNOPADDING
encryption.keyProvider   //密鑰提供程序類型,支持的類型:JCEKSFIL
encryption.keyProvider.keyStoreFile   //密鑰存儲文件的路徑
encrpytion.keyProvider.keyStorePasswordFile  //密鑰存儲密碼文件的路徑
encryption.keyProvider.keys  //所有鍵的列表(例如activeKey設置的歷史記錄)



  • 默認情況下,文件通道使用檢查點和數據目錄的路徑,這些目錄位於用戶主目錄中,如上所述。因此,如果在代理中有多個活動的文件通道實例,則只有一個能夠鎖定目錄並導致其他通道初始化失敗。因此,有必要爲所有已配置的通道提供顯式路徑,最好是在不同的磁盤上。此外,由於文件通道在每次提交之後都將同步到磁盤,因此可能需要將其與接收器/源耦合,以便在檢查點和數據目錄無法使用多個磁盤的情況下提供良好的性能。

3.Flume Sinks

3.1 HDFS Sink

  • 這個接收器將事件寫入Hadoop分佈式文件系統(HDFS)。它目前支持創建文本和序列文件。它支持這兩種文件類型的壓縮。可以根據經過的時間、數據大小或事件數量定期滾動文件(關閉當前文件並創建一個新文件)。它還根據時間戳或事件起源的機器等屬性對數據進行存儲/分區。HDFS目錄路徑可能包含格式化轉義序列,這些轉義序列將被HDFS接收器替換,以生成一個目錄/文件名來存儲事件。使用這個接收器需要安裝hadoop,以便Flume可以使用hadoop jar與HDFS集羣通信。注意,需要一個支持sync()調用的Hadoop版本。
  • 支持的轉移序列
Alias	Description
%{host}	Substitute value of event header named “host”. Arbitrary header names are supported.
%t	Unix time in milliseconds
%a	locale’s short weekday name (Mon, Tue, ...)
%A	locale’s full weekday name (Monday, Tuesday, ...)
%b	locale’s short month name (Jan, Feb, ...)
%B	locale’s long month name (January, February, ...)
%c	locale’s date and time (Thu Mar 3 23:05:25 2005)
%d	day of month (01)
%e	day of month without padding (1)
%D	date; same as %m/%d/%y
%H	hour (00..23)
%I	hour (01..12)
%j	day of year (001..366)
%k	hour ( 0..23)
%m	month (01..12)
%n	month without padding (1..12)
%M	minute (00..59)
%p	locale’s equivalent of am or pm
%s	seconds since 1970-01-01 00:00:00 UTC
%S	second (00..59)
%y	last two digits of year (00..99)
%Y	year (2010)
%z	+hhmm numeric timezone (for example, -0400)
%[localhost]	Substitute the hostname of the host where the agent is running
%[IP]	Substitute the IP address of the host where the agent is running
%[FQDN]	Substitute the canonical hostname of the host where the agent is running
  • 屬性詳解
必有屬性:
agent.sinks=xx                       //當前sink的名稱
agent.sinks.xx.type = hdfs             //當前sink的類型
agent.sinks.xx.channel=xxx            //與當前sink連接的channel的名稱,因爲一個sink只能連接一個channel,所以當前的屬性爲channel
agent.sinks.xx.hdfs.path=$HDFS_PATH    //hdfs上的目錄路徑
其他屬性:
hdfs.filePrefix   //Flume在hdfs創建的目錄中創建的文件路徑的上一級
hdfs.fileSuffix   //附加到文件的後綴(例如:.avro -注意:句點不會自動添加)
hdfs.inUsePrefix  //前綴,用於flume主動寫入的臨時文件
hdfs.inUseSuffix  //後綴,用於flume主動寫入的臨時文件 默認值:.tmp
hdfs.emptyInUseSuffix //if false,在編寫輸出時使用inUseSuffix中設置的參數,關閉輸出hdfs之後。從輸出文件名中刪除inUseSuffix設置的參數。if true,則inUseSuffix參數被忽略,取而代之的是一個空字符串。  默認值:false
hdfs.rollInterval //在滾動當前文件之前等待的秒數(0 =根據時間間隔永不滾動)  默認值:30
hdfs.rollSize  //要觸發滾動的文件大小,以字節爲單位(0:永遠不要基於文件大小進行滾動)  默認值:1024
hdfs.rollCount  //滾前寫入文件的事件數(0 =從不基於事件數滾) 默認值:10
hdfs.idleTimeout //關閉非活動文件的超時(0 =禁用自動關閉空閒文件) 默認值:0
hdfs.batchSize  //在將文件刷新到HDFS之前寫入文件的事件數  默認值:100
hdfs.codeC  //壓縮編解碼器:gzip, bzip2, lzo, lzop, snappy
hdfs.fileType //文件格式:SequenceFile, DataStream或CompressedStream (1)DataStream不會壓縮輸出文件,請不要設置codeC (2)CompressedStream需要設置hdfs。帶有可用編解碼器的編解碼器  默認值:SequenceFile
hdfs.maxnOpenFiles //只允許這個開放文件的數量。如果超過這個數字,則關閉最古老的文件。  默認值:5000
hdfs.minBlockReplicas  //指定每個HDFS塊的最小複製數。如果沒有指定,它來自類路徑中的默認Hadoop配置
hdfs.writeFormat //序列文件(SequenceFile)記錄的格式。Text或Writable在使用Flume創建數據文件之前設置爲Text,否則這些文件不能被Apache Impala(孵化)或Apache Hive讀取。 默認值:Writable
hdfs.threadsPoolSize //用於HDFS IO操作(打開、寫入等)的每個HDFS接收器的線程數 默認值:10
hdfs.rollTimerPoolSize //每個HDFS接收器用於調度定時文件滾動的線程數 默認值:1
hdfs.kerberosPrincipal //訪問安全HDFS的Kerberos用戶主體
hdfs.kerberosKeytab  //Kerberos keytab用於訪問安全的HDFS
hdfs.round  //是否應該將時間戳四捨五入(如果爲真,則影響除%t之外的所有基於時間的轉義序列) 默認值:false
hdfs.roundUnit  //四捨五入的單位,時間,分鐘,或小時   默認值:second
hdfs.roundValue  //將此(在使用hdfs.roundUnit配置的單元中)分爲以上多個(在單元配置的單元中),小於當前時間  默認值:1
hdfs.timeZone  //用於解析目錄路徑的時區的名稱,例如:America/Los Angeles。  默認值:Local Time
hdfs.useLocalTimeStamp  //在替換轉義序列時,使用本地時間(而不是事件頭的時間戳)  默認值:false
hdfs.closeTries  //設置爲0:有節點掛掉後,不會給file重命名,並且文件可能一直處於打開狀態,並且後綴爲自己設置的後綴;設置爲1:有節點掛掉後,會嘗試給file重命名,但是次數有限制;如果嘗試失敗,文件會保持開啓,但會在flume再次啓動時關閉.
 默認值:0
hdfs.retryInterval  //在連續試圖關閉文件之間的時間。每個關閉調用都要花費多個RPC往返於Namenode,因此設置太低會導致名稱節點上的大量負載。如果設置爲0或更少,sink將不會試圖關閉該文件  默認值:180
serializer  //其他可能的選項包括avro_event或EventSerializer.BUilder實現的完全限定類名  默認值:TEXT

3.2 Logger Sink

  • 記錄信息級別的事件。通常用於測試/調試目的。所需屬性爲粗體。這個接收器是惟一不需要在Logging raw data中解釋的額外配置的異常
  • 屬性詳解
必有屬性
agent.sinks=xx                //當前sink的名稱
agent.sinks.xx.channel=xxx    //與當前sink連接的channel的名稱
agent.sinks.xx.type=logger   //當前sink的類型
其他屬性
maxBytesToLog   //要記錄的事件主體的最大字節數  默認值:16

3.3 Avro Sink

  • 這個接收器構成了Flume分層收集支持的一半。發送到此接收器的Flume事件被轉換爲Avro事件併發送到配置的主機名/端口對。事件從已配置的通道中以已配置的批大小批量獲取
  • 屬性詳解
必有屬性:
agent.sinks=xx                //當前sink的名稱
agent.sinks.xx.channel=xxx    //與當前sink連接的channel的名稱
agent.sinks.xx.type=avro   //當前sink的類型
agent.sinks.xx.hostname=xx  //要綁定到的主機名或IP地址
agent.sinks.xx.port==xx      //監聽端口
其他屬性:
batch-size           //要批量發送的事件數 默認值:100
....

4.Flume Interceptors(攔截器)

  • Flume具有修改/刪除運行中的事件的功能。這是在攔截器的幫助下完成的。攔截器是實現org.apache.flume.interceptor.Interceptor接口的類。攔截器可以根據攔截器開發人員選擇的任何標準修改甚至刪除事件。flume支持攔截器鏈。這可以通過在配置中指定截取程序構建器類名列表來實現。截取程序在源配置中指定爲空格分隔的列表。指定攔截器的順序就是調用它們的順序。一個攔截器返回的事件列表將傳遞給鏈中的下一個攔截器。攔截器可以修改或刪除事件。如果攔截器需要刪除事件,它只是在返回的列表中不返回該事件。如果要刪除所有事件,則只返回一個空列表。攔截器是命名組件

4.1 Timestamp Interceptor

  • 這個攔截器將插入到事件頭中,即它處理事件的時間(單位爲millis)。此攔截器插入具有鍵時間戳(或由header屬性指定)的標頭,其值爲相關的時間戳。如果已經存在於配置中,則此攔截器可以保存現有的時間戳。
  • 屬性詳解
必有屬性:
agent.sources=xx				//指定當前source的名稱
agent.sources.xx.interceptors=xxx           //指定當前interceptors的名稱
agent.sources.xx.interceptors.xxx.type=timestamp  //指定當前interceptors的類型
其他屬性:
headerName    //用於放置生成的時間戳的標題的名稱  默認值:timestamp
preserveExisting  //如果時間戳已經存在,應該保留它嗎?true 或 false
默認值:false

4.2 Host Interceptor

  • 此攔截器插入此代理正在運行的主機的主機名或IP地址。它插入一個帶有密鑰主機或已配置密鑰的頭,根據配置,密鑰的值是主機的主機名或IP地址。
  • 屬性詳解
必有屬性:
agent.sources=xx       //指定當前source的名稱
agent.sources.xx.interceptors=xxx   //指定當前interceptors的名稱
agent.sources.xx.interceptors.xxx.type=host  //指定當前interceptors的類型
其他屬性:
hostheader     //要使用的頭鍵  默認值:host
useIP          //如果爲真,則使用IP地址,否則使用主機名 默認值:true
preserveExisting  //如果hostheader已經存在,應該保留它嗎?是真還是假  默認值:false

4.3 Static Interceptor

  • 靜態攔截器允許用戶將帶有靜態值的靜態頭附加到所有事件
  • 當前實現不允許同時指定多個頭。相反,用戶可以鏈接多個靜態攔截器,每個攔截器定義一個靜態頭。
  • 屬性詳解
必有屬性:
agent.sources=xx       //指定當前source的名稱
agent.sources.xx.interceptors=xxx   //指定當前interceptors的名稱
agent.sources.xx.interceptors.xxx.type=static  //指定當前interceptors的類型
其他屬性:
key      //應該創建的標題的名稱  默認值:key
value    //應該創建的靜態值    默認值:value
preserveExisting  //如果配置的頭已經存在,應該保留它-true或false 默認值:true

4.4 Regex Filtering Interceptor

  • 攔截器通過將事件體解釋爲文本並根據已配置的正則表達式匹配文本,選擇性地過濾事件。所提供的正則表達式可用於包含事件或排除事件。
  • 屬性詳解
必有屬性:
agent.sources=xx       //指定當前source的名稱
agent.sources.xx.interceptors=xxx   //指定當前interceptors的名稱
agent.sources.xx.interceptors.xxx.type=regex_filter  //指定當前interceptors的類型
其他屬性:
regex     //用於匹配事件的正則表達式  默認值:".*"
exclude   //if true:則regex確定要排除的事件,否則regex確定要包含的事件。  默認值:false

5. Flume Channel Selectors

5.1 Replicating Channel Selector (default)

  • 屬性詳解
必有屬性:
agent.sources=xx       //指定當前source的名稱
agent.sources.xx.selector.type=replicating  //指定當前selector的類型  可以不配置,默認是replicating
其他屬性:
selector.optional      //一組要標記爲可選的通道
//對於被標記的通道,無法寫入此通道將被忽略;沒有被標記爲選擇器的通道,無法寫入此通道將報錯

5.2 Multiplexing Channel Selector

  • 多路選擇還有一組屬性用於將流分叉。這需要指定事件屬性到channel集合的映射。選擇器檢查事件頭中配置的每個屬性。如果匹配指定的值,則將該事件發送到映射到該值的所有通道。如果沒有匹配,則將事件發送到設置爲默認值的通道集:

  • 屬性詳解

必有屬性:
agent.sources=xx       //指定當前source的名稱
agent.sources.xx.selector.type=multiplexing  //指定當前selector的類型  可以不配置,默認是replicating
其他屬性:
selector.header    //默認值:flume.selector.header
selector.default   //默認選擇某一個channel
selector.mapping.*   //配置不同的鍵值對,對應不同的channel

6.案例

6.1 exec -->memory -->logger


在/home下創建一個f_log隨便寫入
在conf下創建一個test文件夾,創建first.job進行配置

#定義source|channel|sink組件
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#配置r1的屬性
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/f_log

#配置sinks的屬性
a1.sinks.k1.type = logger

#配置channel的屬性
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 10000

#綁定source與sink於channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啓動agent:
flume-ng agent -c conf/  -f ./conf/test/first.conf -n a1 -Dflume.root.logger=INFO,console

測試:
echo "123" >> /home/f_log

6.2 exec -->memory -->hdfs

在conf下創建一個test文件夾,創建exememhdf.conf進行配置

#定義source|channel|sink組件
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#配置r1的屬性
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /home/f_log

#配置sinks的屬性
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/events/dt=%y-%m-%d
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.writeFormat=Text
a1.sinks.k1.hdfs.roundUnit=DataStream
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.useLocalTimeStamp=true


#配置channel的屬性
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 10000

#綁定source與sink於channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

啓動agent:
flume-ng agent -c conf/ -f conf/test/exememhdf.conf -n a1 -Dflume.root.logger=INFO,console

測試:
echo "123" >> /home/f_log

6.3 spooldir -->memory -->logger

創建/home/flumedata/spool1 目錄
在conf下創建一個test文件夾,創建spomemlog.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=spoolDir
a1.sources.r1.spoolDir=/home/flumedata/spool1
a1.sources.r1.fileHeader=true
a1.sources.r1.fileHeaderKey=file

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000


a1.sinks.s1.type = logger

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/spomemlog.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
echo "123" >> /home/flumedata/spool1/a.txt
echo "456" >> /home/flumedata/spool1/b.txt
因爲spool讀取的是文件,讀完之後默認將原文件重命名爲xxx.COMPLETED,所以每次添加文件的名稱不能相同,否則會卡住

6.4 syslogtcp -->memory -->logger

在conf下創建一個test文件夾,創建sysmemlog.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=syslogtcp
a1.sources.r1.port=6666
a1.sources.r1.host=hadoop111

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/sysmemlog.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
需要先安裝nc命令   
yum install nc

echo "hello world" | nc hadoop111 6666

6.5 http -->memory -->logger

在conf下創建一個test文件夾,創建httmemlog.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=org.apache.flume.source.http.HTTPSource
a1.sources.r1.port=6666
a1.sources.r1.bind=hadoop111

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/httmemlog.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
curl -X POST -d '[{"headers":{"time":"2008-08-08"},"body":"this is http test"}]' http://hadoop111:6666

//Http Source默認的Hander類型是JSONHandler 所以測試需要傳入json格式的數據 

6.6 exec -->file -->hdfs

在conf下創建一個test文件夾,創建exefilhdf.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata

a1.channels.c1.type=file
a1.channels.c1.checkpointDir=/home/flumedata/checkpoint
a1.channels.c1.dataDirs=/home/flumedata/data

a1.sinks.s1.type = hdfs
a1.sinks.s1.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s1.hdfs.filePrefix = zxh-
a1.sinks.s1.hdfs.fileSuffix=.log
a1.sinks.s1.hdfs.inUseSuffix=.tmp
a1.sinks.s1.hdfs.rollInterval=2
a1.sinks.s1.hdfs.rollSize=1024
a1.sinks.s1.hdfs.fileType=DataStream
a1.sinks.s1.hdfs.writeFormat=Text
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundValue = 1
a1.sinks.s1.hdfs.roundUnit = second
a1.sinks.s1.hdfs.useLocalTimeStamp=true


a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/exefilhdf.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
echo "123" >> /home/flumedata/exedata

6.7 exec + interceptors -->memory -->hdfs

  • 時間戳攔截器
  • 靜態攔截器
  • 主機攔截器
在conf下創建一個test文件夾,創建exeintmemhdf.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata

a1.sources.r1.interceptors = i1 i2 i3
a1.sources.r1.interceptors.i1.type = timestamp
a1.sources.r1.interceptors.i1.preserveExisting=true

a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i2.hostHeader = hostname
a1.sources.r1.interceptors.i2.preserveExisting=true

a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = city
a1.sources.r1.interceptors.i3.value = NEW_YORK

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = hdfs
a1.sinks.s1.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s1.hdfs.filePrefix = %{hostname}-
a1.sinks.s1.hdfs.fileSuffix=.log
a1.sinks.s1.hdfs.inUseSuffix=.tmp
a1.sinks.s1.hdfs.rollInterval=2
a1.sinks.s1.hdfs.rollSize=1024
a1.sinks.s1.hdfs.fileType=DataStream
a1.sinks.s1.hdfs.writeFormat=Text
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundValue = 1
a1.sinks.s1.hdfs.roundUnit = second
a1.sinks.s1.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/exeintmemhdf.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
echo "123" >> /home/flumedata/exedata

6.8 exec + interceptors -->memory -->logger

  • 正則攔截器
在conf下創建一個test文件夾,創建exeint2memlog.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = regex_filter
#定義正則,以數字開頭
a1.sources.r1.interceptors.i1.regex=^[0-9].*$
a1.sources.r1.interceptors.i1.excludeEvents=false
#false匹配正則

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/exeint2memlog.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
- 數字開頭:
echo "123" >> /home/flumedata/exedata
日誌打印 可以被收集
- 非數字開頭:
echo "abc123" >> /home/flumedata/exedata
日誌不打印 不收集

6.9 exec +selector–>memory -->hdfs

  • 複製選擇器
在conf下創建一個test文件夾,創建exeselmemhdf.conf進行配置

a1.sources=r1
a1.channels=c1 c2
a1.sinks=s1 s2

a1.sources.r1.type=exec
a1.sources.r1.command= tail -f /home/flumedata/exedata
a1.sources.r1.selector.type = replicating
a1.sources.r1.selector.optional = c2


a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=100
a1.channels.c2.keep-alive=3
a1.channels.c2.byteCapacityBufferPercentage = 20
a1.channels.c2.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sinks.s2.type = hdfs
a1.sinks.s2.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s2.hdfs.filePrefix = event-
a1.sinks.s2.hdfs.fileSuffix=.log
a1.sinks.s2.hdfs.inUseSuffix=.tmp
a1.sinks.s2.hdfs.rollInterval=2
a1.sinks.s2.hdfs.rollSize=1024
a1.sinks.s2.hdfs.fileType=DataStream
a1.sinks.s2.hdfs.writeFormat=Text
a1.sinks.s2.hdfs.round = true
a1.sinks.s2.hdfs.roundValue = 1
a1.sinks.s2.hdfs.roundUnit = second
a1.sinks.s2.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1 c2
a1.sinks.s1.channel=c1
a1.sinks.s2.channel=c2

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/exeselmemhdf.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
echo "123456" >> /home/flumedata/exedata

6.10 exec+selector -->memory -->hdfs

  • 複用選擇器
在conf下創建一個test文件夾,創建httselmemhdf.conf進行配置

a1.sources=r1
a1.channels=c1 c2
a1.sinks=s1 s2

a1.sources.r1.type=http
a1.sources.r1.port=6666
a1.sources.r1.bind=hadoop111
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = status
a1.sources.r1.selector.mapping.CZ = c1
a1.sources.r1.selector.mapping.US = c2
a1.sources.r1.selector.default = c1

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=100
a1.channels.c2.keep-alive=3
a1.channels.c2.byteCapacityBufferPercentage = 20
a1.channels.c2.byteCapacity = 800000

a1.sinks.s1.type = logger

a1.sinks.s2.type = hdfs
a1.sinks.s2.hdfs.path = /flume/events/%y-%m-%d/%H%M/%S
a1.sinks.s2.hdfs.filePrefix = zxh
a1.sinks.s2.hdfs.fileSuffix=.log
a1.sinks.s2.hdfs.inUseSuffix=.tmp
a1.sinks.s2.hdfs.rollInterval=2
a1.sinks.s2.hdfs.rollSize=1024
a1.sinks.s2.hdfs.fileType=DataStream
a1.sinks.s2.hdfs.writeFormat=Text
a1.sinks.s2.hdfs.round = true
a1.sinks.s2.hdfs.roundValue = 1
a1.sinks.s2.hdfs.roundUnit = second
a1.sinks.s2.hdfs.useLocalTimeStamp=true

a1.sources.r1.channels=c1 c2
a1.sinks.s1.channel=c1
a1.sinks.s2.channel=c2

啓動agent:
flume-ng agent -c ./conf -f ./conf/test/httselmemhdf.conf -n a1 -Dflume.root.lgger=INFO,console

測試:
curl -X POST -d '[{"headers":{"status":"2017-06-13"},"body":"this is default"}]' http://hadoop111:6666
curl -X POST -d '[{"headers":{"status":"CZ"},"body":"this is CZ"}]' http://hadoop111:6666
curl -X POST -d '[{"headers":{"status":"US"},"body":"this is US"}]' http://hadoop111:6666
curl -X POST -d '[{"headers":{"status":"ss"},"body":"this is ss"}]' http://hadoop111:6666


6.11 netcat–>memory–>kafka

在conf下創建kafka.conf進行如下配置


a1.sources=r1
a1.sinks=k1
a1.channels=c1
#用於描述source的,類型是netcat網絡,telnet
a1.sources.r1.type=netcat
#source監聽的網絡ip地址和端口號
a1.sources.r1.bind=hadoop111
a1.sources.r1.port=44444
#用於描述channel,在內存中做數據的臨時的存儲
a1.channels.c1.type=memory
#該內存中最大的存儲容量,1000個events事件
a1.channels.c1.capacity=1000
#能夠同時對100個events事件監管事務
a1.channels.c1.transactionCapacity=100
#用於描述sink,類型是日誌格式,用於定製消息發佈方的參數
a1.sinks.k1.type=org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.topic=spark
a1.sinks.k1.brokerList=hadoop111:9092
a1.sinks.k1.requiredAcks=1
a1.sinks.k1.batchSize=20
#將a1中的各個組件建立關聯關係,將source和sink都指向了同一個channel
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1

啓動agent:
flume-ng agent -c ./conf -f ./conf/kafka.conf -n a1
kafka實時消費:
kafka-console-consumer.sh --topic spark --bootstrap-server hadoop111:9092 --from-beginning
開啓44444端口,並傳輸數據
nc -lk hadoop111 44444
輸入數據,觀察kafka-consumer
nihao
hello

6.11 flume集羣搭建

  • 原理
通過用avro sink配置一些一級代理,可以在Flume中實現這一點,它們都指向一個代理的avro源(同樣,您可以在這樣的場景中使用節約來源/ sink /客戶機)。第二層代理的這個源將被接收的事件合併到一個單獨的通道中,它被一個接收器消耗到它的最終目的地。
  • 此處我用僞分佈式舉例
配置第一個agent 
在conf下創建一個test文件夾,創建agent1.conf進行配置

a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=netcat
a1.sources.r1.port=6666
a1.sources.r1.bind=hadoop111

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type =avro
a1.sinks.s1.hostname=hadoop111
a1.sinks.s1.port=8888

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
配置第二個agent
在conf下創建一個test文件夾,創建agent2.conf進行配置


a1.sources=r1
a1.channels=c1
a1.sinks=s1

a1.sources.r1.type=netcat
a1.sources.r1.port=6667
a1.sources.r1.bind=hadoop111

a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.keep-alive=3
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000

a1.sinks.s1.type =avro
a1.sinks.s1.hostname=hadoop111
a1.sinks.s1.port=8888

a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
配置第三個agent
在conf下創建一個test文件夾,創建agent3.conf進行配置

agent.sources=r1
agent.channels=c1
agent.sinks=s1

agent.sources.r1.type=avro
agent.sources.r1.port=8888
agent.sources.r1.bind=hadoop111

agent.channels.c1.type=memory
agent.channels.c1.capacity=1000
agent.channels.c1.transactionCapacity=100
agent.channels.c1.keep-alive=3
agent.channels.c1.byteCapacityBufferPercentage = 20
agent.channels.c1.byteCapacity = 800000

agent.sinks.s1.type =logger

agent.sources.r1.channels=c1
agent.sinks.s1.channel=c1


agent1,agent2接收的數據都會發送給8888端口,由8888端口去處理
啓動agent:
1.先啓動avro resource所在的端口
flume-ng agent -c ./conf -f ./conf/test/agent3.conf -n a1 -Dflume.root.lgger=INFO,console 
2.再啓動兩個avro sink所在的端口
flume-ng agent -c ./conf -f ./conf/test/agent1.conf -n a1 -Dflume.root.lgger=INFO,console 
flume-ng agent -c ./conf -f ./conf/test/agent2.conf -n a1 -Dflume.root.lgger=INFO,console 

測試1:
echo "hello world"  | nc hadoop111 6666
測試2:
echo "hello world2" | nc hadoop111 6667

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章