【Flume】Flume入門解析(二)

在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

(圖片來源於網絡,侵刪)


一、案例實戰

【1】Flume故障轉移(failover)

Failover Sink Processor能夠實現failover功能,具體流程類似load balance,但是內部處理機制與load balance完全不同
Failover Sink Processor維護一個優先級Sink組件列表,只要有一個Sink組件可用,Event就被傳遞到下一個組件。故障轉移機制的作用是將失敗的Sink降級到一個池,在這些池中它們被分配一個冷卻時間,隨着故障的連續,在重試之前冷卻時間增加。一旦Sink成功發送一個事件,它將恢復到活動池。 Sink具有與之相關的優先級,數量越大,優先級越高
例如,具有優先級爲100的sink在優先級爲80的Sink之前被激活。如果在發送事件時匯聚失敗,則接下來將嘗試下一個具有最高優先級的Sink發送事件。如果沒有指定優先級,則根據在配置中指定Sink的順序來確定優先級

整體步驟:

  • 1.根據環境編寫 flume配置文件
    節點1如下配置👇
    在這裏插入圖片描述
    上述代碼👆
#a1 name
a1.channels = c1
a1.sources = r1
a1.sinks = k1 k2
#
##set gruop
a1.sinkgroups = g1
##set sink group
a1.sinkgroups.g1.sinks = k1 k2

#
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /export/servers/apache-flume-1.8.0-bin/upload

#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
## set sink1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node02
a1.sinks.k1.port = 52020
#
## set sink2
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = node03
a1.sinks.k2.port = 52020
#
##set failover
a1.sinkgroups.g1.processor.type = failover
a1.sinkgroups.g1.processor.priority.k1 = 2
a1.sinkgroups.g1.processor.priority.k2 = 1
a1.sinkgroups.g1.processor.maxpenalty = 10000
#
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1

節點2如下配置(節點3和節點2配置一樣,就綁定的主機名不一致,修改一下即可)
在這裏插入圖片描述
上述代碼👆

#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1

## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = node02
a1.sources.r1.port = 52020

##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path= hdfs://node01:8020/flume/HA
a1.sinks.k1.hdfs.fileType = DataStream

a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
  • 2.啓動
先在節點2/3根目錄下啓動:
flume-ng agent -c conf/ -f job/HA-avro-flume-hdfs.conf -n a1

再先在節點1根目錄下啓動:
flume-ng agent -c conf/ -f job/HA-file-flume-hdfs.conf -n a1
  • 3.將節點2殺死 ,然後往被監控目錄添加文件
    在這裏插入圖片描述

  • 4.發現HDFS上產生了文件,表示節點2被殺死,節點3接替進行工作成功
    在這裏插入圖片描述

  • 5.再次啓動節點2,發現節點2接替節點3,繼續接收消息


【2】Flume故障轉移(failover)

負載均衡是用於解決一臺機器(一個進程)無法解決所有請求而產生的一種算法。Load balancing Sink Processor能夠實現load balance功能,如下圖Agent1是一個路由節點,負責將Channel暫存的Event均衡到對應的多個Sink組件上,而每個Sink組件分別連接到一個獨立的Agent上,示例配置,如下所示:
在這裏插入圖片描述

該方式會隨機將消息發到不同的Sink端中,實現負載均衡的目的

整體步驟:

  • 1.根據環境編寫 flume配置文件
    節點1如下配置👇
    在這裏插入圖片描述
    上述代碼👆
#a1 name
a1.channels = c1
a1.sources = r1
a1.sinks = k1 k2
#
##set gruop
a1.sinkgroups = g1
##set sink group
a1.sinkgroups.g1.sinks = k1 k2

#
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /export/servers/apache-flume-1.8.0-bin/upload

#
##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
## set sink1
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node02
a1.sinks.k1.port = 52021
#
## set sink2
a1.sinks.k2.type = avro
a1.sinks.k2.hostname = node03
a1.sinks.k2.port = 52021
#
##set failover
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true  #如果開啓,則將失敗的sink放入黑名單
# 另外還支持random
a1.sinkgroups.g1.processor.selector = round_robin
#在黑名單放置的超時時間,超時結束時,若仍然無法接收,則超時時間呈指數增長  
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000  

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c1

節點2/3如下配置👇
在這裏插入圖片描述
上述代碼👆

#set Agent name
a1.sources = r1
a1.channels = c1
a1.sinks = k1

## other node,nna to nns
a1.sources.r1.type = avro
a1.sources.r1.bind = node02
a1.sources.r1.port = 52021

##set channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

#
##set sink to hdfs
a1.sinks.k1.type=hdfs
a1.sinks.k1.hdfs.path= hdfs://node01:8020/flume/load_banlancer
a1.sinks.k1.hdfs.fileType = DataStream

a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
  • 2.啓動
先在節點2/3根目錄下啓動:
flume-ng agent -c conf/ -f job/load_banlancer_server.conf -n a1

再先在節點1根目錄下啓動:
flume-ng agent -c conf/ -f job/load_banlancer_client.conf -n a1
  • 3.往被監控目錄添加文件
    在這裏插入圖片描述
  • 4.查看HDFS上的文件
    在這裏插入圖片描述

二、過濾器

【1】Flume(interceptor)

整體步驟:

  • 1.根據環境編寫 flume配置文件
    節點1/2如下配置👇
    在這裏插入圖片描述
    上述代碼👆
# Name the components on this agent
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /export/servers/apache-flume-1.8.0-bin/upload/access/access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
##  static攔截器的功能就是往採集到的數據的header中插入自己定## 義的key-value對
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access

a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /export/servers/apache-flume-1.8.0-bin/upload/nginx/nginx.log
a1.sources.r2.interceptors = i2
a1.sources.r2.interceptors.i2.type = static
a1.sources.r2.interceptors.i2.key = type
a1.sources.r2.interceptors.i2.value = nginx

a1.sources.r3.type = exec
a1.sources.r3.command = tail -F /export/servers/apache-flume-1.8.0-bin/upload/web/web.log
a1.sources.r3.interceptors = i3
a1.sources.r3.interceptors.i3.type = static
a1.sources.r3.interceptors.i3.key = type
a1.sources.r3.interceptors.i3.value = web

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node03
a1.sinks.k1.port = 41414

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1

節點3如下配置👇
在這裏插入圖片描述
上述代碼👆

a1.sources = r1
a1.sinks = k1
a1.channels = c1
#定義source
a1.sources.r1.type = avro
a1.sources.r1.bind = node03
a1.sources.r1.port =41414

#添加時間攔截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder

#定義channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000

#定義sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path=hdfs://node01:8020/flume/interceptors/%{type}/%Y%m%d
a1.sinks.k1.hdfs.filePrefix =events
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
#時間類型
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件不按條數生成
a1.sinks.k1.hdfs.rollCount = 0
#生成的文件按時間生成
a1.sinks.k1.hdfs.rollInterval = 30
#生成的文件按大小生成
a1.sinks.k1.hdfs.rollSize  = 10485760
#批量寫入hdfs的個數
a1.sinks.k1.hdfs.batchSize = 10000
#flume操作hdfs的線程數(包括新建,寫入等)
a1.sinks.k1.hdfs.threadsPoolSize=10
#操作hdfs超時時間
a1.sinks.k1.hdfs.callTimeout=30000


#組裝source、channel、sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
說明:sources需要添加過濾器,類型必須是org.apache.flume.interceptor.TimestampInterceptor$Builder
​	  寫入數據時獲取前面設置的key ,使用%{type}**
  • 2.啓動
先在節點3根目錄下啓動:
flume-ng agent -c conf/ -f job/interceptor2.conf -n a1

再先在節點1/2根目錄下啓動:
flume-ng agent -c conf/ -f job/interceptor1.conf -n a1
  • 3.往被監控目錄添加文件
    在這裏插入圖片描述
  • 4.查看HDFS上的文件
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述

【2】Flume自定義攔截器

1.介紹

Flume是Cloudera提供的一個高可用的高可靠的分佈式的海量日誌採集、聚合和傳輸的系統,Flume支持在日誌系統中定製各類數據發送方,用於收集數據;同時,Flume提供對數據進行簡單處理,並寫到各種數據接受方(可定製)的能力。Flume有各種自帶的攔截器,比如:TimestampInterceptorHostInterceptorRegexExtractorInterceptor等,通過使用不同的攔截器,實現不同的功能

但是以上的這些攔截器,不能改變原有日誌數據的內容或者對日誌信息添加一定的處理邏輯,當一條日誌信息有幾十個甚至上百個字段的時候,在傳統的Flume處理下,收集到的日誌還是會有對應這麼多的字段,也不能對你想要的字段進行對應的處理

2.自定義攔截器

根據實際業務的需求,爲了更好的滿足數據在應用層的處理,通過自定義Flume攔截器,過濾掉不需要的字段,並對指定字段加密處理,將源數據進行預處理。減少了數據的傳輸量,降低了存儲的開銷

3.功能實現

【1】編寫Java代碼,自定義攔截器

  • 1.定義一個類CustomParameterInterceptor實現Interceptor接口。
  • 2.在CustomParameterInterceptor類中定義變量,這些變量是需要到 Flume的配置文件中進行配置使用的。每一行字段間的分隔符(fields_separator)、通過分隔符分隔後,所需要列字段的下標(indexs)、多個下標使用的分隔符(indexs_separator)
  • 3.添加CustomParameterInterceptor的有參構造方法。並對相應的變量進行處理。將配置文件中傳過來的unicode編碼進行轉換爲字符串
  • 4.寫具體的要處理的邏輯intercept()方法,一個是單個處理的,一個是批量處理的。
  • 5.接口中定義了一個內部接口Builder,在configure方法中,進行一些參數配置。並給出,在flume的conf中沒配置一些參數時,給出其默認值。通過其builder方法,返回一個CustomParameterInterceptor對象。
  • 6.定義一個靜態類,類中封裝MD5加密方法
    在這裏插入圖片描述
    1. 通過以上步驟,自定義攔截器的代碼開發已完成,然後打包成jar, 放到Flume的根目錄下的lib

【2】修改Flume的配置信息👇

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

#channel
a1.channels.c1.type = memory
a1.channels.c1.capacity=100000
a1.channels.c1.transactionCapacity=50000

#source
a1.sources.r1.channels = c1
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /root/data/
a1.sources.r1.batchSize= 50
a1.sources.r1.inputCharset = UTF-8

a1.sources.r1.interceptors =i1 i2
a1.sources.r1.interceptors.i1.type =cn.itcast.interceptor.CustomParameterInterceptor$Builder
a1.sources.r1.interceptors.i1.fields_separator=\\u0009
a1.sources.r1.interceptors.i1.indexs =0,1,3,5,6
a1.sources.r1.interceptors.i1.indexs_separator =\\u002c
a1.sources.r1.interceptors.i1.encrypted_field_index =0

a1.sources.r1.interceptors.i2.type = org.apache.flume.interceptor.TimestampInterceptor$Builder

#sink
a1.sinks.s1.channel = c1
a1.sinks.s1.type = hdfs
a1.sinks.s1.hdfs.path =hdfs://192.168.100.111:8020/flume/%Y%m%d
a1.sinks.s1.hdfs.filePrefix = event
a1.sinks.s1.hdfs.fileSuffix = .log
a1.sinks.s1.hdfs.rollSize = 10485760
a1.sinks.s1.hdfs.rollInterval =20
a1.sinks.s1.hdfs.rollCount = 0
a1.sinks.s1.hdfs.batchSize = 1500
a1.sinks.s1.hdfs.round = true
a1.sinks.s1.hdfs.roundUnit = minute
a1.sinks.s1.hdfs.threadsPoolSize = 25
a1.sinks.s1.hdfs.useLocalTimeStamp = true
a1.sinks.s1.hdfs.minBlockReplicas = 1
a1.sinks.s1.hdfs.fileType =DataStream
a1.sinks.s1.hdfs.writeFormat = Text
a1.sinks.s1.hdfs.callTimeout = 60000
a1.sinks.s1.hdfs.idleTimeout =60

都看到這裏了,點贊評論一下吧!!!

在這裏插入圖片描述

完結撒花!!!

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