前言
记录 在学习大数据技术过程中的学习笔记
目录
案例二:多Channel之Replicating Channel Selector
案例四:多Channel之Multiplexing Channel Selector
高级组件
Source Interceptors:Source可以指定一个或者多个拦截器按先后顺序依次采集到的数据进行处理
Channel Selectors:Source发往多个Channel的策略设置
Sink Processors:Sink发送数据的策略设置
Event
Event是Flume是传输数据的基本单位,也是事务的基本单位,在文本文件中,通常一行记录就是一个Event
Event里有header和body;header类型为Map<String,String>
我们可以在Source中增加header的<key,value>,在Channel和Sink中使用header中的值
Source Interceptors 拦截器
常见的Interceptors类型:
Timestamp Interceptor(时间戳拦截器) 主要用于往Event的header里面添加timestamp时间戳信息的、
Host Interceptor 主要用于向Event中的header里面去添加host属性,这个属性的值一般就是当前机器的一个主机名或者IP信息、
Search and Replace Interceptor 根据指定的规则查询Event中的body里面的那个原始数据,然后根据这个规则进行替换,它会修改Event中body中的值,也就是说它会修改你采集到的原始数据、
Static Interceptor 静态的,它可以固定的往Event的header里面添加一些key和value、
Regex Extractor Interceptor 根据指定的规则从Event的body里面去抽取数据 ,然后生成key和value,在把key和value添加到Event的header里面
等
案例一:对采集到的数据按天按类型分目录存储
Exec Source -> Search and Replace Interceptor -> Regx Extractor
Interceptor -> File Channel -> HDFS Sink
在第四台机器上创建文件
vi file-to-hdfs-more.conf
# agent的名称是a1
# 指定source组件、channel组件和Sink组件的名称
a1.sources = r1
a1.channels = c1
a1.sinks = k1
# 配置source组件
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /data/log/moreType.log
# 配置拦截器 [多个拦截器按照顺序依次执行]
a1.sources.r1.interceptors = i1 i2 i3 i4
#拦截器类型
a1.sources.r1.interceptors.i1.type = search_replace
#根据正则匹配
a1.sources.r1.interceptors.i1.searchPattern = "type":"video_info"
#匹配到后替换成
a1.sources.r1.interceptors.i1.replaceString = "type":"videoInfo"
#拦截器类型
a1.sources.r1.interceptors.i2.type = search_replace
#根据正则匹配
a1.sources.r1.interceptors.i2.searchPattern = "type":"user_info"
#匹配到后替换成
a1.sources.r1.interceptors.i2.replaceString = "type":"userInfo"
a1.sources.r1.interceptors.i3.type = search_replace
a1.sources.r1.interceptors.i3.searchPattern = "type":"gift_record"
a1.sources.r1.interceptors.i3.replaceString = "type":"giftRecord"
# 正则抽取拦截器
a1.sources.r1.interceptors.i4.type = regex_extractor
# 正则匹配到的内容会被抽取出来
a1.sources.r1.interceptors.i4.regex = "type":"(\\w+)"
# 定义变量s1 值为 抽取的内容
a1.sources.r1.interceptors.i4.serializers = s1
# 在header里面生成键值(<logType,s1>)
a1.sources.r1.interceptors.i4.serializers.s1.name = logType
# 配置channel组件
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /data/soft/apache-flume-1.9.0-bin/data/moreType/checkpoint
a1.channels.c1.dataDirs = /data/soft/apache-flume-1.9.0-bin/data/moreType/data
# 配置sink组件
a1.sinks.k1.type = hdfs
#这里的logType就是上面拦截器生成的
a1.sinks.k1.hdfs.path = hdfs://192.168.10.100:9000/moreType/%Y%m%d/%{logType}
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
a1.sinks.k1.hdfs.rollInterval = 3600
a1.sinks.k1.hdfs.rollSize = 134217728
a1.sinks.k1.hdfs.