目录
5、Search and Replace Interceptor
6、Regex Filtering Interceptor
7、Regex Extractor Interceptor
1、Default Sink Processor
2、Failover Sink Processor
3、Load balancing Sink Processor
一、Selector(选择器)
Selector概述
Selector(选择器)可以工作在复制或多路复用(路由) 模式下 。
复制模式
Selector复制模式-属性说明
selector.type replicating 类型名称,默认是 replicating
selector.optional – 标志通道为可选
Selector复制模式-案例
#命名Agent组件
a1.sources=r1
a1.sinks=k1 k2
a1.channels=c1 c2
#描述/配置Source
a1.sources.r1.type=http
a1.sources.r1.port=8888
#描述Sink,输出为多个地址,扇出流
a1.sinks.k1.type=avro
a1.sinks.k1.hostname=192.168.242.138
a1.sinks.k1.port=9988
a1.sinks.k2.type=avro
a1.sinks.k2.hostname=192.168.242.135
a1.sinks.k2.port=9988
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1 c2
a1.sinks.k1.channel=c1
a1.sinks.k2.channel=c2
一到多(一个输入多个输出),即扇出流 。
多路复用(路由)模式
Selector多路复用(路由)模式-属性说明
selector.type 类型,必须是"multiplexing"
selector.header 指定要监测的头的名称
selector.default –
selector.mapping.* –
举例:
a1.sources = r1
a1.channels = c1 c2 c3 c4
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = state
a1.sources.r1.selector.mapping.CZ = c1
a1.sources.r1.selector.mapping.US = c2 c3
a1.sources.r1.selector.default = c4
Selector多路复用(路由)模式-案例
#配置Agent组件
a1.sources=r1
a1.sinks=k1 k2
a1.channels=c1 c2
#描述/配置Source,多路复用模式
a1.sources.r1.type=http
a1.sources.r1.port=8888
a1.sources.r1.selector.type=multiplexing
a1.sources.r1.selector.header=gender
a1.sources.r1.selector.mapping.male=c1
a1.sources.r1.selector.mapping.female=c2
a1.sources.r1.selector.default=c1
#描述Sink
a1.sinks.k1.type=avro
a1.sinks.k1.hostname=192.168.242.138
a1.sinks.k1.port=9988
a1.sinks.k2.type=avro
a1.sinks.k2.hostname=192.168.242.135
a1.sinks.k2.port=9988
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
a1.channels.c2.type=memory
a1.channels.c2.capacity=1000
a1.channels.c2.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1 c2
a1.sinks.k1.channel=c1
a1.sinks.k2.channel=c2
补充知识:扇入流
m3:
编写配置文件:
#命名Agent组件
a1.sources=r1
a1.sinks=k1
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=avro
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=4141
#描述Sink
a1.sinks.k1.type=logger
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
m1、m2:
编写配置文件:
#命名Agent组件
a1.sources=r1
a1.sinks=k1
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=http
a1.sources.r1.port=8888
#描述Sink
a1.sinks.k1.type=avro
a1.sinks.k1.hostname=192.168.242.135
a1.sinks.k1.port=4141
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
启动flume:
./flume-ng agent --conf ../conf --conf-file ../conf/template9.conf --name a1 -Dflume.root.logger=INFO,console
m1通过curl发送一条http请求,由于默认使用的是jsonHandler,数据格式必须是指定的json格式:
[root@localhost conf]# curl -XPOST -d '[{ "headers" :{"flag" : "c"},"body" : "idoall.org_body"}]' http://0.0.0.0:8888
m2通过curl发送一条http请求,由于默认使用的是jsonHandler,数据格式必须是指定的json格式:
[root@localhost conf]# curl -XPOST -d '[{ "headers" :{"flag" : "c"},"body" : "idoall.org_body"}]' http://0.0.0.0:8888
发现m3均能正确收到消息。
二、Interceptors(拦截器)
Interceptors概述
Flume有能力在运行阶段修改/删除Event,这是通过拦截器(Interceptors)来实现的。
拦截器需要实现org.apache.flume.interceptor.Interceptor接口。
拦截器可以修改或删除事件基于开发者在选择器中选择的任何条件。
拦截器采用了责任链模式,多个拦截器可以按指定顺序拦截。
一个拦截器返回的事件列表被传递给链中的下一个拦截器。
如果一个拦截器需要删除事件,它只需要在返回的事件集中不包含要删除的事件即可。
如果要删除所有事件,只需返回一个空列表。
1、Timestamp Interceptor
Timestamp Interceptor概述
这个拦截器在事件头中插入以毫秒为单位的当前处理时间。头的名字为timestamp,值为当前处理的时间戳。如果在之前已经有这个时间戳,则保留原有的时间戳。
Timestamp Interceptor属性说明
!interceptors.type – 类型名称,必须是timestamp或自定义类的全路径名
preserveExisting false 如果时间戳已经存在是否保留
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2、Host Interceptor
Host Interceptor概述
这个拦截器插入当前处理Agent的主机名或ip,头的名字为host或配置的名称,值是主机名或ip地址,基于配置。
Host Interceptor属性说明
!type – 类型名称,必须是host
preserveExisting false 如果主机名已经存在是否保留
useIP true 如果配置为true则用IP,配置为false则用主机名
hostHeader host 加入头时使用的名称
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1 i2
a1.sources.r1.interceptors.i1.type = timestamp
#ip是拦截者所在机器的ip
a1.sources.r1.interceptors.i2.type = host
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
3、Static Interceptor
Static Interceptor概述
此拦截器允许用户增加静态头信息使用静态的值到所有事件。目前的实现中不允许一次指定多个头。如果需要增加多个静态头可以指定多个Static interceptors。
Static Interceptor属性说明
!type – 类型,必须是static
preserveExisting true 如果配置头已经存在是否应该保留
key key 要增加的头名
value value 要增加的头值
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1 i2 i3
a1.sources.r1.interceptors.i1.type = timestamp
#ip是拦截者所在机器的ip
a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = country
a1.sources.r1.interceptors.i3.value = China
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
在Event的headers中有多了country=China信息
4、UUID Interceptor
UUID Interceptor概述
这个拦截器在所有事件头中增加一个全局一致性标志,其实就是UUID。
UUID Interceptor属性说明
!type – 类型名称,必须是org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
headerName id 头名称
preserveExisting true 如果头已经存在,是否保留
prefix “” 在UUID前拼接的字符串前缀
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1 i2 i3 i4
a1.sources.r1.interceptors.i1.type = timestamp
#ip是拦截者所在机器的ip
a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = country
a1.sources.r1.interceptors.i3.value = China
a1.sources.r1.interceptors.i4.type = org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
作用:标识一条日志
5、Search and Replace Interceptor
Search and Replace Interceptor概述
这个拦截器提供了简单的基于字符串的正则搜索和替换功能。可以修改body部分的内容
Search and Replace Interceptor属性说明
type – 类型名称,必须是"search_replace"
searchPattern – 要搜索和替换的正则表达式
replaceString – 要替换为的字符串
charset UTF-8 字符集编码,默认utf-8
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1 i2 i3 i4 i5
a1.sources.r1.interceptors.i1.type = timestamp
#ip是拦截者所在机器的ip
a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = country
a1.sources.r1.interceptors.i3.value = China
a1.sources.r1.interceptors.i4.type = org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
a1.sources.r1.interceptors.i5.type = search_replace
#将所有的数字替换成*
a1.sources.r1.interceptors.i5.searchPattern = [0-9]
a1.sources.r1.interceptors.i5.replaceString = *
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
6、Regex Filtering Interceptor
Regex Filtering Interceptor概述
此拦截器通过解析事件体去匹配给定正则表达式来筛选事件,所提供的正则表达式即可以用来包含或刨除事件。
Regex Filtering Interceptor属性说明
!type – 类型,必须设定为regex_filter
regex ”.*” 所要匹配的正则表达式
excludeEvents false 如果是true则排除匹配的事件,false则包含匹配的事件。
案例
#命名Agent a1的组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#描述/配置Source
a1.sources.r1.type = http
a1.sources.r1.port = 44444
a1.sources.r1.interceptors = i1 i2 i3 i4 i5 i6
a1.sources.r1.interceptors.i1.type = timestamp
#ip是拦截者所在机器的ip
a1.sources.r1.interceptors.i2.type = host
a1.sources.r1.interceptors.i3.type = static
a1.sources.r1.interceptors.i3.key = country
a1.sources.r1.interceptors.i3.value = China
a1.sources.r1.interceptors.i4.type = org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
a1.sources.r1.interceptors.i5.type = search_replace
#将所有的数字替换成*
a1.sources.r1.interceptors.i5.searchPattern = [0-9]
a1.sources.r1.interceptors.i5.replaceString = *
a1.sources.r1.interceptors.i6.type = regex_filter
#只要是a开头的抛弃
a1.sources.r1.interceptors.i6.regex = ^a.*
a1.sources.r1.interceptors.i6.excludeEvents = true
#描述Sink
a1.sinks.k1.type = logger
#描述内存Channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#为Channle绑定Source和Sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
7、Regex Extractor Interceptor
Regex Extractor Interceptor概述
使用指定正则表达式匹配事件,并将匹配到的组作为头加入到事件中,它也支持插件化的序列化器用来格式化匹配到的组在加入他们作为头之前。
Regex Extractor Interceptor属性说明
!type – 类型,必须是regex_extractor
!regex – 要匹配的正则表达式
!serializers – Space-separated list of serializers for mapping matches to header names and serializing their values. (See example below) Flume provides built-in support for the following serializers: org.apache.flume.interceptor.RegexExtractorInterceptorPassThroughSerializer org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer
serializers.<s1>.type default Must be default (org.apache.flume.interceptor.RegexExtractorInterceptorPassThroughSerializer), org.apache.flume.interceptor.RegexExtractorInterceptorMillisSerializer, or the FQCN of a custom class that implements org.apache.flume.interceptor.RegexExtractorInterceptorSerializer
serializers.<s1>.name –
serializers.* – Serializer-specific properties
三、Processor(处理器)
Processor概述
Sink Group允许用户将多个Sink组合成一个实体。
Flume Sink Processor 可以通过切换组内Sink用来实现负载均衡的效果,或在一个Sink故障时切换到另一个Sink。
sinks – 用空格分隔的Sink集合
processor.type default 类型名称,必须是 default、failover 或 load_balance
1、Default Sink Processor
Default Sink Processor 只接受一个 Sink。不要求用户为单一Sink创建processor
2、Failover Sink Processor
Failover Sink Processor 维护一个sink们的优先表。确保只要一个是可用的事件就可以被处理。
失败处理原理是,为失效的sink指定一个冷却时间,在冷却时间到达后再重新使用。
sink们可以被配置一个优先级,数字越大优先级越高。
如果sink发送事件失败,则下一个最高优先级的sink将会尝试接着发送事件。
如果没有指定优先级,则优先级顺序取决于sink们的配置顺序,先配置的默认优先级高于后配置的。
在配置的过程中,设置一个group processor ,并且为每个sink都指定一个优先级。
优先级必须是唯一的。
另外可以设置maxpenalty属性指定限定失败时间。
属性说明
sinks – Space-separated list of sinks that are participating in the group
processor.type default The component type name, needs to be failover
processor.priority.<sinkName> – Priority value. <sinkName> must be one of the sink instances associated with the current sink group A higher priority value Sink gets activated earlier. A larger absolute value indicates higher priority
processor.maxpenalty 30000 The maximum backoff period for the failed Sink (in millis)
案例
h1配置文件
#命名Agent组件
a1.sources=r1
a1.sinks=k1 k2
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=http
a1.sources.r1.port=44444
#描述Sink
a1.sinkgroups = g1
a1.sinkgroups.g1.sinks = k1 k2
a1.sinkgroups.g1.processor.type = failover
a1.sinkgroups.g1.processor.priority.k1=5
a1.sinkgroups.g1.processor.priority.k2=10
a1.sinks.k1.type =avro
a1.sinks.k1.hostname =hadoop02
a1.sinks.k1.port =44444
a1.sinks.k2.type =avro
a1.sinks.k2.hostname =hadoop03
a1.sinks.k2.port =44444
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
a1.sinks.k2.channel=c1
h2、h3配置文件
#命名Agent组件
a1.sources=r1
a1.sinks=k1
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port=44444
#描述Sink
a1.sinks.k1.type=logger
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
h1发送数据
curl -XPOST -d '[{ "headers" :{"flag" : "c"},"body" : "idoall.org_body"}]' http://0.0.0.0:44444
3、Load balancing Sink Processor
Load balancing Sink processor 提供了在多个sink之间实现负载均衡的能力。
它维护了一个活动sink的索引列表。
它支持轮询或随机方式的负载均衡,默认值是轮询方式,可以通过配置指定。
也可以通过实现AbstractSinkSelector接口实现自定义的选择机制。
属性说明
!processor.sinks – Space-separated list of sinks that are participating in the group
!processor.type default The component type name, needs to be load_balance
processor.backoff false Should failed sinks be backed off exponentially.
processor.selector round_robin Selection mechanism. Must be either round_robin, random or FQCN of custom class that inherits from AbstractSinkSelector
processor.selector.maxTimeOut 30000 Used by backoff selectors to limit exponential backoff (in milliseconds)
案例
h1配置文件
#命名Agent组件
a1.sources=r1
a1.sinks=k1 k2
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=http
a1.sources.r1.port=44444
#描述Sink 负载均衡方式
a1.sinkgroups = g1
a1.sinkgroups.g1.sinks = k1 k2
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.selector = random
a1.sinks.k1.type =avro
a1.sinks.k1.hostname =hadoop02
a1.sinks.k1.port =44444
a1.sinks.k2.type =avro
a1.sinks.k2.hostname =hadoop03
a1.sinks.k2.port =44444
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
a1.sinks.k2.channel=c1
h2、h3配置文件
#命名Agent组件
a1.sources=r1
a1.sinks=k1
a1.channels=c1
#描述/配置Source
a1.sources.r1.type=avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port=44444
#描述Sink
a1.sinks.k1.type=logger
#描述内存Channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=1000
#为Channel绑定Source和Sink
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
h1发送数据
curl -XPOST -d '[{ "headers" :{"flag" : "c"},"body" : "idoall.org_body"}]' http://0.0.0.0:44444
以上就是全部内容,如有疑问或者建议欢迎留言。
版权所有,转载请说明转载地址