Flume (六) Interceptors

Flume has the capability to modify/drop events in-flight. This is done with the help of interceptors. Interceptors are classes that implement org.apache.flume.interceptor.Interceptor interface. An interceptor can modify or even drop events based on any criteria chosen by the developer of the interceptor. Flume supports chaining of interceptors. This is made possible through by specifying the list of interceptor builder class names in the configuration. Interceptors are specified as a whitespace separated list in the source configuration. The order in which the interceptors are specified is the order in which they are invoked. The list of events returned by one interceptor is passed to the next interceptor in the chain. Interceptors can modify or drop events. If an interceptor needs to drop events, it just does not return that event in the list that it returns. If it is to drop all events, then it simply returns an empty list. Interceptors are named components, here is an example of how they are created through configuration:

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

a1.sources = r1
a1.sinks = k1
a1.channels = c1
a1.sources.r1.interceptors = i1 i2
a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.HostInterceptor$Builder
a1.sources.r1.interceptors.i1.preserveExisting = false
a1.sources.r1.interceptors.i1.hostHeader = hostname
a1.sources.r1.interceptors.i2.type = org.apache.flume.interceptor.TimestampInterceptor$Builder
a1.sinks.k1.filePrefix = FlumeData.%{CollectorHost}.%Y-%m-%d
a1.sinks.k1.channel = c1

請注意,攔截器構建器將傳遞給類型config參數。 攔截器本身是可配置的,可以像傳遞給任何其他可配置組件一樣傳遞配置值。 在上面的示例中,事件首先傳遞給HostInterceptor,然後HostInterceptor返回的事件傳遞給TimestampInterceptor。 您可以指定完全限定的類名(FQCN)或別名timestamp。 如果您有多個收集器寫入相同的HDFS路徑,那麼您也可以使用HostInterceptor。

Timestamp Interceptor

This interceptor inserts into the event headers, the time in millis at which it processes the event. This interceptor inserts a header with key timestamp (or as specified by the header property) whose value is the relevant timestamp. This interceptor can preserve an existing timestamp if it is already present in the configuration.
此攔截器插入到事件頭中,以毫秒爲單位處理事件。 此攔截器插入帶有鍵時間戳(或由header屬性指定)的標頭,其值爲相關時間戳。 如果配置中已存在時,此攔截器可以保留現有時間戳。

Property Name Default Description
type 組件類型名稱必須是timestamp或FQCNThe
header timestamp 用於放置生成的時間戳的標頭的名稱。
preserveExisting false 如果時間戳已存在,是否應保留 - true或false

Example for agent named a1:

a1.sources = r1
a1.channels = c1
a1.sources.r1.channels =  c1
a1.sources.r1.type = seq
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp

Host Interceptor

This interceptor inserts the hostname or IP address of the host that this agent is running on. It inserts a header with key host or a configured key whose value is the hostname or IP address of the host, based on configuration.
此攔截器插入運行此代理程序的主機的hostname 或IP地址。 它根據配置插入key爲host或者配置的key,其值爲主機的主機名或IP地址。

Property Name Default Description
type The component type name, has to be host
preserveExisting false 如果主機頭已存在,是否應保留 - true或false
useIP true 如果爲true,請使用IP地址,否則使用主機名。
hostHeader host The header key to be used.

Example for agent named a1:

a1.sources = r1
a1.channels = c1
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = host

Static Interceptor

靜態攔截器允許用戶將具有靜態值的靜態頭附加到所有事件。

當前實現不允許一次指定多個標頭。 相反,用戶可能會鏈接多個靜態攔截器,每個靜態攔截器都定義一個。

Property Name Default Description
type The component type name, has to be static
preserveExisting true 如果時間戳已存在,是否應保留 - true或false
key key 應創建的標頭的名稱
value value 應該創建的靜態值

Example for agent named a1:

a1.sources = r1
a1.channels = c1
a1.sources.r1.channels =  c1
a1.sources.r1.type = seq
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
a1.sources.r1.interceptors.i1.key = datacenter
a1.sources.r1.interceptors.i1.value = NEW_YORK

Remove Header Interceptor

This interceptor manipulates Flume event headers, by removing one or many headers. It can remove a statically defined header, headers based on a regular expression or headers in a list. If none of these is defined, or if no header matches the criteria, the Flume events are not modified.
此攔截器通過刪除一個或多個headers來操縱Flume事件headers。它可以刪除靜態header, 基於正在表達式的headers或者header列表。 如果未定義這些,或者沒有header符合條件,則不會修改Flume事件。

Note that if only one header needs to be removed, specifying it by name provides performance benefits over the other 2 methods.
請注意,如果只需要刪除一個標頭,則按名稱指定header相對於其他兩種方法的更有性能優勢。

Property Name Default Description
type The component type name, has to be remove_header
withName 要刪除的標頭的名稱
fromList 要刪除的標頭列表,使用fromListSeparator指定的分隔符分隔
fromListSeparator \s*,\s* 正則表達式用於分隔fromList指定的列表中的多個標題名稱。 默認值是由任意數量的空白字符包圍的逗號
matching 將刪除名稱與此正則表達式匹配的所有標頭

UUID Interceptor

此攔截器在所有截獲的事件上設置通用唯一標識符。 示例UUID是b5755073-77a9-43c1-8fad-b7a586fc1b97,表示128位值。

Consider using UUIDInterceptor to automatically assign a UUID to an event if no application level unique key for the event is available. It can be important to assign UUIDs to events as soon as they enter the Flume network; that is, in the first Flume Source of the flow. This enables subsequent deduplication of events in the face of replication and redelivery in a Flume network that is designed for high availability and high performance. If an application level key is available, this is preferable over an auto-generated UUID because it enables subsequent updates and deletes of event in data stores using said well known application level key.
如果沒有可用的事件的應用程序級別唯一鍵,請考慮使用UUIDInterceptor自動爲事件分配UUID。 一旦進入Flume網絡就將UUID分配給事件是非常重要;也就是說,在flow的第一個Flume Source中將UUID分配給事件。 這樣可以在Flume網絡中進行復制和重新傳輸時事件的後續重複數據刪除,該網絡旨在實現高可用性和高性能。 如果應用程序級別密鑰可用,則優於自動生成的UUID,因爲它使用所述公知的應用程序級別密鑰在數據存儲中啓用後續更新和事件刪除

Property Name Default Description
type The component type name, has to be remove_header
headerName id 要修改的Flume標頭的名稱
preserveExisting true 如果UUID標頭已存在,是否應保留 - true或false
prefix “” 前綴字符串常量,用於預先添加到每個生成的UUID

Morphline Interceptor

此攔截器通過morphline配置文件過濾事件,該文件定義了一系列轉換命令,用於將記錄從一個命令傳送到另一個命令。 例如,morphline可以忽略某些事件或通過基於正則表達式的模式匹配來更改或插入某些事件頭,或者它可以通過Apache Tika在截獲的事件上自動檢測和設置MIME類型。 例如,這種數據包嗅探可用於Flume拓撲中基於內容的動態路由。 MorphlineInterceptor還可以幫助實現到多個Apache Solr集合的動態路由(例如,用於多租戶)。

目前,存在一個限制,即攔截器的morphline不得爲每個輸入事件生成多個輸出記錄。 此攔截器不適用於重型ETL處理 - 如果您需要,請考慮將ETL處理從Flume Source移動到Flume Sink,例如 到MorphlineSolrSink。

Property Name Default Description
type 他的組件類型名稱必須是org.apache.flume.sink.solr.morphline.MorphlineInterceptor$Builder
morphlineFile 本地文件系統與morphline配置文件的相對或絕對路徑。 示例:/etc/flume-ng/conf/morphline.conf
morphlineId null 如果morphline配置文件中有多個morphlines,則用於標識morphline的可選名稱

Sample flume.conf file:

a1.sources.avroSrc.interceptors = morphlineinterceptor
a1.sources.avroSrc.interceptors.morphlineinterceptor.type = org.apache.flume.sink.solr.morphline.MorphlineInterceptor$Builder
a1.sources.avroSrc.interceptors.morphlineinterceptor.morphlineFile = /etc/flume-ng/conf/morphline.conf
a1.sources.avroSrc.interceptors.morphlineinterceptor.morphlineId = morphline1

Search and Replace Interceptor

此攔截器基於Java正則表達式提供簡單的基於字符串的搜索和替換功能。 還可以進行回溯/羣組捕捉 (Backtracking / group capture)。 此攔截器使用與Java Matcher.replaceAll()方法中相同的規則。

Property Name Default Description
type 組件類型名稱必須是search_replace
searchPattern The pattern to search for and replace.
replaceString The replacement string.
charset UTF-8 The charset of the event body. Assumed by default to be UTF-8.

Example configuration:

a1.sources.avroSrc.interceptors = search-replace
a1.sources.avroSrc.interceptors.search-replace.type = search_replace

# Remove leading alphanumeric characters in an event body.
a1.sources.avroSrc.interceptors.search-replace.searchPattern = ^[A-Za-z0-9_]+
a1.sources.avroSrc.interceptors.search-replace.replaceString =

Another example:

a1.sources.avroSrc.interceptors = search-replace
a1.sources.avroSrc.interceptors.search-replace.type = search_replace

# Use grouping operators to reorder and munge words on a line.
a1.sources.avroSrc.interceptors.search-replace.searchPattern = The quick brown ([a-z]+) jumped over the lazy ([a-z]+)
a1.sources.avroSrc.interceptors.search-replace.replaceString = The hungry $2 ate the careless $1

Regex Filtering Interceptor

此攔截器通過將事件主體解釋爲文本並將文本與已配置的正則表達式進行匹配來有選擇地過濾事件。 提供的正則表達式可用於包括事件或排除事件。

Property Name Default Description
type 組件類型名稱必須是regex_filter
regex “.*” 用於匹配事件的正則表達式
excludeEvents false 如果爲true,則regex確定要排除的事件,否則regex確定要包括的事件。

Regex Extractor Interceptor

此攔截器使用指定的正則表達式提取正則表達式匹配組,並將匹配組作爲標題追加到事件上。 它還支持可插入序列化程序,用於在將匹配組添加爲事件標頭之前對其進行格式化。

Property Name Default Description
type 組件類型名稱必須是regex_extractor
regex 用於匹配事件的正則表達式
serializers 以空格分隔的序列化程序列表,用於映射匹配標題名稱並序列化其值。 (參見下面的示例)Flume爲以下序列化程序提供內置支持:org.apache.flume.interceptor.RegexExtractorInterceptorPassThroughSerializer org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer
serializers.<s1>.type default 必須是default(org.apache.flume.interceptor.RegexExtractorInterceptorPassThroughSerializer),org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer,或實現org.apache.flume.interceptor.RegexExtractorInterceptorSerializer的自定義類的FQCN
serializers.<s1>.name
serializers.* 特定於序列化程序的屬性

序列化器用於將匹配映射到標題名稱和格式化標題值; 默認情況下,您只需指定標題名稱,將使用默認的org.apache.flume.interceptor.RegexExtractorInterceptorPassThroughSerializer。 此序列化程序只是將匹配映射到指定的標題名稱,並在正則表達式提取時傳遞值。 您可以使用完全限定的類名(FQCN)將自定義序列化程序實現插入到提取器中,以便以您喜歡的方式格式化匹配。

Example 1:
如果Flume事件主體包含 1:2:3.4foobar5 並且使用了以下配置

a1.sources.r1.interceptors.i1.regex = (\\d):(\\d):(\\d)
a1.sources.r1.interceptors.i1.serializers = s1 s2 s3
a1.sources.r1.interceptors.i1.serializers.s1.name = one
a1.sources.r1.interceptors.i1.serializers.s2.name = two
a1.sources.r1.interceptors.i1.serializers.s3.name = three

提取的事件將包含相同的主體,並且headers將添加one=>1, two=>2, three=>3

Example 2:
如果Flume事件主體包含 2012-10-18 18:47:57,614 some log line 並且使用了以下配置

a1.sources.r1.interceptors.i1.regex = ^(?:\\n)?(\\d\\d\\d\\d-\\d\\d-\\d\\d\\s\\d\\d:\\d\\d)
a1.sources.r1.interceptors.i1.serializers = s1
a1.sources.r1.interceptors.i1.serializers.s1.type = org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer
a1.sources.r1.interceptors.i1.serializers.s1.name = timestamp
a1.sources.r1.interceptors.i1.serializers.s1.pattern = yyyy-MM-dd HH:mm

提取的事件將包含相同的主體,並且headers將添加timestamp=>1350611220000

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