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被认为是必需的通道,即使它被标记为必需和可选,并且写入此通道的失败将导致该事件 在为选择器配置的所有通道上重试。
请注意,如果标头没有任何所需的通道,则该事件将被写入默认通道,并将尝试写入该标头的可选通道。 如果未指定所需的通道,则指定可选通道仍会将事件写入默认通道。 如果没有默认通道和必需通道,则选择器将尝试将事件写入可选通道。 在这种情况下,任何失败都会被忽略。