Flume NG 學習筆記(八)Interceptors(攔截器)測試

目錄(?)[+]

攔截器主要是對事件的header信息信息操作,要麼直接忽略他,要麼修改他的數據

一、Event Serializers

file_roll sink 和hdfs sink 都支持EventSerializer接口

1.1、Body Text Serializer

Body TextSerializer,別名:text。這個攔截器將把事件的body部分寫入到輸出流中而不需要任何轉換或者修改。事件的header將直接被忽略。

下面是官網配置:

Property Name

Default

Description

appendNewline

true

Whether a newline will be appended to each event at write time. The default of true assumes that events do not contain newlines, for legacy reasons.

下面是官網例子:appendNewline是選擇是否加入到新行去。默認是true,而false 就是換行,一般我們都選擇換行。

a1.sinks=k1

a1.sinks.k1.type=file_roll

a1.sinks.k1.channel=c1

a1.sinks.k1.sink.directory=/var/log/flume

a1.sinks.k1.sink.serializer=text

a1.sinks.k1.sink.serializer.appendNewline=false

下面是實際例子

因爲是考慮Body TextSerializer的特性,他會忽略header的信息,因此我們這邊要採用http source來接收定義的header 與body 的內容


[html] view plain copy

  1. #配置文件:body_case15.conf  

  2. # Name the components on this agent  

  3. a1.sources = r1  

  4. a1.sinks = k1  

  5. a1.channels = c1  

  6.    

  7. # Describe/configure the source  

  8. a1.sources.r1.type = http  

  9. a1.sources.r1.port = 50000  

  10. a1.sources.r1.host = 192.168.233.128  

  11. a1.sources.r1.channels = c1  

  12.    

  13. # Describe the sink  

  14. a1.sinks.k1.type = file_roll  

  15. a1.sinks.k1.channel = c1  

  16. a1.sinks.k1.sink.directory = /tmp/logs  

  17. a1.sinks.k1.sink.serializer = text  

  18. a1.sinks.k1.sink.serializer.appendNewline =false  

  19.    

  20. # Use a channel which buffers events inmemory  

  21. a1.channels.c1.type = memory  

  22. a1.channels.c1.capacity = 1000  

  23. a1.channels.c1.transactionCapacity = 100  



#敲命令

flume-ng agent -c conf -fconf/body_case15.conf -n a1 -Dflume.root.logger=INFO,console

啓動成功後

 

打開另一個終端輸入,往偵聽端口送數據

curl -X POST -d '[{"headers":{"looklook1" : "looklook1 isheader","looklook2": "looklook2 isheader"},"body" : "hellolooklook5"}]'http://192.168.233.128:50000

#在啓動源發送的代理終端查看console輸出




數據已經輸出,但只輸出了hello looklook5,即BODY這塊。

1.2、Avro Event Serializer

Avro Event Serializer別名:avro_event。這個攔截器將把事件序列化到一個Avro容器文件中。使用的模式和RPC Avro機制使用到的處理flume事件的機制一樣。這個序列化器繼承自AbstractAvroEventSerializer類。

官網例子

Property Name

Default

Description

syncIntervalBytes

2048000

Avro sync interval, in approximate bytes.

compressionCodec

null

Avro compression codec. For supported codecs, see Avro’s CodecFactory docs.

下面是官網例子

a1.sinks.k1.type=hdfs

a1.sinks.k1.channel=c1

a1.sinks.k1.hdfs.path=/flume/events/%y-%m-%d/%H%M/%S

a1.sinks.k1.serializer=avro_event

a1.sinks.k1.serializer.compressionCodec=snappy

例子這邊就不演示了,因爲和BodyText Serializer 差距不大。

 

二、Timestamp Interceptor

官網說Flume 可以在事件傳輸過程中對它進行修改與刪除,而這個都是通過Interceptor進行實現的,實際都是往事件的header裏插數據。而Timestamp Interceptor攔截器就是可以eventheader中插入關鍵詞爲timestamp的時間戳

下面是官網配置

Property Name

Default

Description

type

The component type name, has to be timestamp or the FQCN

preserveExisting

false

If the timestamp already exists, should it be preserved - true or false

以及官網例子

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

下面是測試例子


[html] view plain copy

  1. #配置文件:timestamp_case16.conf  

  2. # Name the components on this agent  

  3. a1.sources = r1  

  4. a1.sinks = k1  

  5. a1.channels = c1  

  6.    

  7. # Describe/configure the source  

  8. a1.sources.r1.type = syslogtcp  

  9. a1.sources.r1.port = 50000  

  10. a1.sources.r1.host = 192.168.233.128  

  11. a1.sources.r1.channels = c1  

  12.    

  13. a1.sources.r1.interceptors = i1  

  14. a1.sources.r1.interceptors.i1.preserveExistingfalse  

  15. a1.sources.r1.interceptors.i1.type = timestamp  

  16.    

  17.    

  18. # Describe the sink  

  19. a1.sinks.k1.type = hdfs  

  20. a1.sinks.k1.channel = c1  

  21. a1.sinks.k1.hdfs.path =hdfs://carl:9000/flume/%Y-%m-%d/%H%M  

  22. a1.sinks.k1.hdfs.filePrefix = looklook5.  

  23. a1.sinks.k1.hdfs.fileType=DataStream  

  24.    

  25. # Use a channel which buffers events inmemory  

  26. a1.channels.c1.type = memory  

  27. a1.channels.c1.capacity = 1000  

  28. a1.channels.c1.transactionCapacity = 100  


這裏拿header作爲文件夾目錄名稱。

#敲命令

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

啓動成功後

 

打開另一個終端輸入,往偵聽端口送數據

echo "TimestampInterceptor" | nc 192.168.233.128 50000

#在啓動源發送的代理終端查看console輸出

查看hdfs生成的文件,可以看到timestamp已經生成在header裏面,可以根據自定義的格式生成文件夾,數據也都傳輸過來了。



三、Host Interceptor


該攔截器可以往event的header中插入關鍵詞默認爲host主機名或者ip地址(注意是agent運行的機器的主機名或者ip地址)

下面是官網配置

Property Name

Default

Description

type

The component type name, has to be host

preserveExisting

false

If the host header already exists, should it be preserved - true or false

useIP

true

Use the IP Address if true, else use hostname.

hostHeader

host

The header key to be used.

以及官網例子

a1.sources=r1

a1.channels=c1

a1.sources.r1.interceptors=i1

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

a1.sources.r1.interceptors.i1.hostHeader=hostname

下面是測試例子


[html] view plain copy

  1. #配置文件:time_host_case17.conf  

  2. # Name the components on this agent  

  3. a1.sources = r1  

  4. a1.sinks = k1  

  5. a1.channels = c1  

  6.    

  7. # Describe/configure the source  

  8. a1.sources.r1.type = syslogtcp  

  9. a1.sources.r1.port = 50000  

  10. a1.sources.r1.host = 192.168.233.128  

  11. a1.sources.r1.channels = c1  

  12.    

  13. a1.sources.r1.interceptors = i1 i2  

  14. a1.sources.r1.interceptors.i1.preserveExistingfalse  

  15. a1.sources.r1.interceptors.i1.type =timestamp  

  16. a1.sources.r1.interceptors.i2.type = host  

  17. a1.sources.r1.interceptors.i2.hostHeader =hostname  

  18. a1.sources.r1.interceptors.i2.useIP = false  

  19.    

  20. # Describe the sink  

  21. a1.sinks.k1.type = hdfs  

  22. a1.sinks.k1.channel = c1  

  23. a1.sinks.k1.hdfs.path =hdfs://carl:9000/flume/%Y-%m-%d/%H%M  

  24. a1.sinks.k1.hdfs.filePrefix = %{hostname}  

  25. a1.sinks.k1.hdfs.fileType=DataStream  

  26.    

  27. # Use a channel which buffers events inmemory  

  28. a1.channels.c1.type = memory  

  29. a1.channels.c1.capacity = 1000  

  30. a1.channels.c1.transactionCapacity = 100  


增加一個攔截器,類型是host,h將hostname作爲文件前綴。

#敲命令

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

啓動成功後

 

打開另一個終端輸入,往偵聽端口送數據

echo "Time&hostInterceptor1" | nc 192.168.233.128 50000

echo "Time&hostInterceptor2" | nc 192.168.233.128 50000

 

#在啓動源發送的代理終端查看console輸出

查看hdfs生成的文件,可以看到host已經生成在header裏面,可以根據自定義的格式生成文件夾,數據也都傳輸過來了。



四、Static Interceptor


Static Interceptor攔截器允許用戶增加一個static的header併爲所有的事件賦值。範圍是所有事件。

 

官網配置

Property Name

Default

Description

type

The component type name, has to be static

preserveExisting

true

If configured header already exists, should it be preserved - true or false

key

key

Name of header that should be created

value

value

Static value that should be created

其中參數key與value等於類似json格式裏的"headers":{" key":" value"}

下面是官網例子

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

以及實際的列子


[html] view plain copy

  1. #配置文件:static_case18.conf  

  2. # Name the components on this agent  

  3. a1.sources = r1  

  4. a1.sinks = k1  

  5. a1.channels = c1  

  6.    

  7. # Describe/configure the source  

  8. a1.sources.r1.type = syslogtcp  

  9. a1.sources.r1.port = 50000  

  10. a1.sources.r1.host = 192.168.233.128  

  11. a1.sources.r1.channels = c1  

  12. a1.sources.r1.interceptors = i1  

  13. a1.sources.r1.interceptors.i1.type = static  

  14. a1.sources.r1.interceptors.i1.key = looklook5  

  15. a1.sources.r1.interceptors.i1.value =looklook10  

  16.    

  17. # Describe the sink  

  18. a1.sinks.k1.type = logger  

  19.    

  20. # Use a channel which buffers events inmemory  

  21. a1.channels.c1.type = memory  

  22. a1.channels.c1.capacity = 1000  

  23. a1.channels.c1.transactionCapacity = 100  

  24.    

  25. # Bind the source and sink to the channel  

  26. a1.sources.r1.channels = c1  

  27. a1.sinks.k1.channel = c1  


#敲命令

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

啓動成功後

 

打開另一個終端輸入,往偵聽端口送數據

echo "statInterceptor1" | nc 192.168.233.128 50000

 

#在啓動源發送的代理終端查看console輸出

可以看到輸出的header信息裏自定義部分正確輸出,body部分也輸出正確。



五、Regex FilteringInterceptor


Regex Filtering Interceptor攔截器用於過濾事件,篩選出與配置的正則表達式相匹配的事件。可以用於包含事件和排除事件。常用於數據清洗,通過正則表達式把數據過濾出來。

官網配置

Property Name

Default

Description

type

The component type name has to be regex_filter

regex

”.*”

Regular expression for matching against events

excludeEvents

false

If true, regex determines events to exclude, otherwise regex determines events to include.

excludeEvents 爲true的時候爲排除所有匹配正則表達式的數據。

下面是測試例子


[html] view plain copy

  1. #配置文件:regex_filter_case19.conf  

  2. # Name the components on this agent  

  3. a1.sources = r1  

  4. a1.sinks = k1  

  5. a1.channels = c1  

  6.    

  7. # Describe/configure the source  

  8. a1.sources.r1.type = syslogtcp  

  9. a1.sources.r1.port = 50000  

  10. a1.sources.r1.host = 192.168.233.128  

  11. a1.sources.r1.channels = c1  

  12. a1.sources.r1.interceptors = i1  

  13. a1.sources.r1.interceptors.i1.type =regex_filter  

  14. a1.sources.r1.interceptors.i1.regex =^[0-9]*$  

  15. a1.sources.r1.interceptors.i1.excludeEvents =true  

  16.    

  17. # Describe the sink  

  18. a1.sinks.k1.type = logger  

  19.    

  20. # Use a channel which buffers events inmemory  

  21. a1.channels.c1.type = memory  

  22. a1.channels.c1.capacity = 1000  

  23. a1.channels.c1.transactionCapacity = 100  

  24.    

  25. # Bind the source and sink to the channel  

  26. a1.sources.r1.channels = c1  

  27. a1.sinks.k1.channel = c1  


我們對開頭字母是數字的數據,全部過濾。

#敲命令

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

啓動成功後

 

打開另一個終端輸入,往偵聽端口送數據

echo "a" | nc192.168.233.128 50000

echo "1222" |nc 192.168.233.128 50000

echo "a222" |nc 192.168.233.128 50000

 

#在啓動源發送的代理終端查看console輸出



可以看出1222 被認爲是無效的數據沒有發出來。

Regex Filtering Interceptor測試成功。


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