Configuration
正如前面部分所述,Flume代理程序配置是從類似於具有分層屬性設置的Java屬性文件格式的文件中讀取的。
Defining the flow
要在單個代理中定義流,您需要通過channel連接sources 和sinks 。 您需要列出給定agent的sources,sinks 和channels,然後將sources和sinks指向channels。 source實例可以指定多個channel,但sink實例只能指定一個channel。 格式如下:
# list the sources, sinks and channels for the agent
<Agent>.sources = <Source>
<Agent>.sinks = <Sink>
<Agent>.channels = <Channel1> <Channel2>
# set channel for source
<Agent>.sources.<Source>.channels = <Channel1> <Channel2> ...
# set channel for sink
<Agent>.sinks.<Sink>.channel = <Channel1>
例如,名爲agent_foo的代理正在從外部avro客戶端讀取數據並通過內存通道將其發送到HDFS。 配置文件weblog.config可能如下所示:
# list the sources, sinks and channels for the agent
agent_foo.sources = avro-appserver-src-1
agent_foo.sinks = hdfs-sink-1
agent_foo.channels = mem-channel-1
# set channel for source
agent_foo.sources.avro-appserver-src-1.channels = mem-channel-1
# set channel for sink
agent_foo.sinks.hdfs-sink-1.channel = mem-channel-1
這將使事件從avro-AppSrv-source流向hdfs-Cluster1-sink,通過內存通道mem-channel-1。 當使用weblog.config作爲其配置文件啓動代理程序時,它將實例化該流程。
Configuring individual components
定義流後,您需要設置每個源,接收器和通道的屬性。 這是以相同的分層命名空間方式完成的,您可以在配置中設置組件類型以及特定於每個組件的屬性的其他值:
# properties for sources
<Agent>.sources.<Source>.<someProperty> = <someValue>
# properties for channels
<Agent>.channel.<Channel>.<someProperty> = <someValue>
# properties for sinks
<Agent>.sources.<Sink>.<someProperty> = <someValue>
需要爲Flume的每個組件設置屬性“type”,以瞭解它需要的對象類型。 每個源,接收器和通道類型都有自己的一組屬性,使其能夠按預期運行。 所有這些都需要根據需要進行設置。 在前面的示例中,我們有一個從avro-AppSrv-source到hdfs-Cluster1-sink的流程,通過內存通道mem-channel-1。 這是一個示例,顯示了每個組件的配置:
agent_foo.sources = avro-AppSrv-source
agent_foo.sinks = hdfs-Cluster1-sink
agent_foo.channels = mem-channel-1
# set channel for sources, sinks
# properties of avro-AppSrv-source
agent_foo.sources.avro-AppSrv-source.type = avro
agent_foo.sources.avro-AppSrv-source.bind = localhost
agent_foo.sources.avro-AppSrv-source.port = 10000
# properties of mem-channel-1
agent_foo.channels.mem-channel-1.type = memory
agent_foo.channels.mem-channel-1.capacity = 1000
agent_foo.channels.mem-channel-1.transactionCapacity = 100
# properties of hdfs-Cluster1-sink
agent_foo.sinks.hdfs-Cluster1-sink.type = hdfs
agent_foo.sinks.hdfs-Cluster1-sink.hdfs.path = hdfs://namenode/flume/webdata
Adding multiple flows in an agent
單個Flume代理可以包含多個獨立流。 您可以在配置中列出多個源,接收器和通道。 可以鏈接這些組件以形成多個流:
# list the sources, sinks and channels for the agent
<Agent>.sources = <Source1> <Source2>
<Agent>.sinks = <Sink1> <Sink2>
<Agent>.channels = <Channel1> <Channel2>
然後,您可以將源和接收器鏈接到其通道(用於接收器)的相應通道(用於源)以設置兩個不同的流。 例如,如果您需要在代理中設置兩個流,一個從外部avro客戶端到外部HDFS,另一個從尾部輸出到avro接收器,那麼這是一個配置來執行此操作:
# list the sources, sinks and channels in the agent
agent_foo.sources = avro-AppSrv-source1 exec-tail-source2
agent_foo.sinks = hdfs-Cluster1-sink1 avro-forward-sink2
agent_foo.channels = mem-channel-1 file-channel-2
# flow #1 configuration
agent_foo.sources.avro-AppSrv-source1.channels = mem-channel-1
agent_foo.sinks.hdfs-Cluster1-sink1.channel = mem-channel-1
# flow #2 configuration
agent_foo.sources.exec-tail-source2.channels = file-channel-2
agent_foo.sinks.avro-forward-sink2.channel = file-channel-2
Configuring a multi agent flow
要設置多層流,您需要有第一個hop 的avro/thrift
接收器指向下一個hop 的avro/thrift
源。 這將導致第一個Flume代理將事件轉發到下一個Flume代理。 例如,如果您使用avro客戶端定期向本地Flume代理髮送文件(每個事件1個文件),則此本地代理可以將其轉發到已安裝存儲的另一個代理。
Weblog agent config:
# list sources, sinks and channels in the agent
agent_foo.sources = avro-AppSrv-source
agent_foo.sinks = avro-forward-sink
agent_foo.channels = file-channel
# define the flow
agent_foo.sources.avro-AppSrv-source.channels = file-channel
agent_foo.sinks.avro-forward-sink.channel = file-channel
# avro sink properties
agent_foo.sinks.avro-forward-sink.type = avro
agent_foo.sinks.avro-forward-sink.hostname = 10.1.1.100
agent_foo.sinks.avro-forward-sink.port = 10000
# configure other pieces
#...
HDFS agent config:
# list sources, sinks and channels in the agent
agent_foo.sources = avro-collection-source
agent_foo.sinks = hdfs-sink
agent_foo.channels = mem-channel
# define the flow
agent_foo.sources.avro-collection-source.channels = mem-channel
agent_foo.sinks.hdfs-sink.channel = mem-channel
# avro source properties
agent_foo.sources.avro-collection-source.type = avro
agent_foo.sources.avro-collection-source.bind = 10.1.1.100
agent_foo.sources.avro-collection-source.port = 10000
# configure other pieces
#...
在這裏,我們將weblog代理的avro-forward-sink鏈接到hdfs代理的avro-collection-source。 這將導致來自外部應用程序服務器源的事件最終存儲在HDFS中。
Fan out flow 扇出流
如前一節所述,Flume支持從一個源扇出流到多個通道。 扇出有兩種模式,複製和多路複用(replicating and multiplexing)。 在複製流程中,事件將發送到所有已配置的通道。 在多路複用的情況下,事件僅被髮送到合格信道的子集。 要扇出流,需要指定源的通道列表以及扇出它的策略。 這是通過添加可以複製或多路複用的channel “selector”來完成的。 如果它是多路複用器,則進一步指定選擇規則。 如果您沒有指定選擇器,那麼默認情況下它會複製:
# List the sources, sinks and channels for the agent
<Agent>.sources = <Source1>
<Agent>.sinks = <Sink1> <Sink2>
<Agent>.channels = <Channel1> <Channel2>
# set list of channels for source (separated by space)
<Agent>.sources.<Source1>.channels = <Channel1> <Channel2>
# set channel for sinks
<Agent>.sinks.<Sink1>.channel = <Channel1>
<Agent>.sinks.<Sink2>.channel = <Channel2>
<Agent>.sources.<Source1>.selector.type = replicating
多路複用選擇具有另一組屬性以使流分叉。 這需要指定事件屬性到通道集的映射。 選擇器檢查事件頭中的每個已配置屬性。 如果它與指定的值匹配,則該事件將發送到映射到該值的所有通道。 如果沒有匹配,則將事件發送到默認配置的通道集:
# Mapping for multiplexing selector
<Agent>.sources.<Source1>.selector.type = multiplexing
<Agent>.sources.<Source1>.selector.header = <someHeader>
<Agent>.sources.<Source1>.selector.mapping.<Value1> = <Channel1>
<Agent>.sources.<Source1>.selector.mapping.<Value2> = <Channel1> <Channel2>
<Agent>.sources.<Source1>.selector.mapping.<Value3> = <Channel2>
#...
<Agent>.sources.<Source1>.selector.default = <Channel2>
映射允許爲每個值重疊通道。
以下示例具有多路複用到兩個路徑的單個流。 名爲agent_foo的代理具有單個avro源和兩個鏈接到兩個接收器的通道:
# list the sources, sinks and channels in the agent
agent_foo.sources = avro-AppSrv-source1
agent_foo.sinks = hdfs-Cluster1-sink1 avro-forward-sink2
agent_foo.channels = mem-channel-1 file-channel-2
# set channels for source
agent_foo.sources.avro-AppSrv-source1.channels = mem-channel-1 file-channel-2
# set channel for sinks
agent_foo.sinks.hdfs-Cluster1-sink1.channel = mem-channel-1
agent_foo.sinks.avro-forward-sink2.channel = file-channel-2
# channel selector configuration
agent_foo.sources.avro-AppSrv-source1.selector.type = multiplexing
agent_foo.sources.avro-AppSrv-source1.selector.header = State
agent_foo.sources.avro-AppSrv-source1.selector.mapping.CA = mem-channel-1
agent_foo.sources.avro-AppSrv-source1.selector.mapping.AZ = file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.mapping.NY = mem-channel-1 file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.default = mem-channel-1
選擇器檢查名爲“State”的標頭。 如果值爲“CA”,則將其發送到mem-channel-1,如果其爲“AZ”,則將其發送到file-channel-2,或者如果其爲“NY”則將其發送到mem-channel-1和file-channel-2。 如果“State”未設置或與三者中的任何一個都不匹配,則它將轉到mem-channel-1,其被指定爲“default”。
選擇器還支持可選通道。 要爲標頭指定可選通道,可通過以下方式使用配置參數“optional”:
# channel selector configuration
agent_foo.sources.avro-AppSrv-source1.selector.type = multiplexing
agent_foo.sources.avro-AppSrv-source1.selector.header = State
agent_foo.sources.avro-AppSrv-source1.selector.mapping.CA = mem-channel-1
agent_foo.sources.avro-AppSrv-source1.selector.mapping.AZ = file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.mapping.NY = mem-channel-1 file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.optional.CA = mem-channel-1 file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.mapping.AZ = file-channel-2
agent_foo.sources.avro-AppSrv-source1.selector.default = mem-channel-1
選擇器將首先嚐試寫入所需的通道,如果其中一個通道無法消費事件,則會使事務失敗。 在所有channel上重試。 一旦所有必需的channel消耗了事件,則選擇器將嘗試寫入可選通道。 所有在可選通道消費事件失敗的,都會簡單的忽略它,並且不會重試。
如果optional channels與特定報頭的required channels之間存在重疊,則認爲該信道是必需的,並且信道中的故障將導致重試所有必需信道集。 例如,在上面的示例中,對於標題“CA”,mem-channel-1被認爲是必需的通道,即使它被標記爲必需和可選,並且寫入此通道的失敗將導致該事件 在爲選擇器配置的所有通道上重試。
請注意,如果標頭沒有任何所需的通道,則該事件將被寫入默認通道,並將嘗試寫入該標頭的可選通道。 如果未指定所需的通道,則指定可選通道仍會將事件寫入默認通道。 如果沒有默認通道和必需通道,則選擇器將嘗試將事件寫入可選通道。 在這種情況下,任何失敗都會被忽略。