flume高级组件-拦截器&采集到的数据按天按类型分目录存储

155 阅读7分钟

1.高级组件介绍(据在官网查询到的资料->我是搬运工)

  • Source Interceptors:Source可以指定一个或者多个拦截器按先后顺序依次对采集到的数据进行处理。
  • Channel Selectors:Source发往多个Channel的策略设置,如果source后面接了多个channel,到底是给所有的channel都发,还是根据规则发送到不同的channel,这些都是由Channel Selectors来控制的。
  • Sink Processors:Sink 发送数据的策略设置,一个channel后面可以接多个sink,channel中的数据是被哪个sink获取,这个是由Sink Processors控制的。
  • Event:Event是Flume传输数据的基本单位,也是事务的基本单位,在文本文件中,通常一行记录就是一个Event
    Event中包含header和body
  • body是采集到的那一行记录的原始内容
  • header类型为Map<String, String>,里面可以存储一些属性信息,方便后面使用。
    我们可以在Source中给每一条数据的header中增加key-value,在Channel和Sink中使用header中的值了。

 

2.关于Source Interceptors的一些知识:

第一个高级组件,Source Interceptors<br>
系统中已经内置提供了很多Source Interceptors
常见的Source Interceptors类型:Timestamp Interceptor、Host Interceptor、Search and ReplaceInterceptor 、Static Interceptor、Regex Extractor Interceptor 等。

  • Timestamp Interceptor:向event中的header里面添加timestamp 时间戳信息

  • Host Interceptor:向event中的header里面添加host属性,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添加到header中。

3.既然看了那么多,那么来浅浅的总结一下:

Timestamp Interceptor、Host Interceptor、Static Interceptor、Regex Extractor Interceptor是向event中的header里面添加key-value类型的数据,方便后面的channel和sink组件使用,对采集到的原始数据内容没有任何影响。
Search and Replace Interceptor是会根据规则修改event中body里面的原始数据内容,对header没有任何影响,使用这个拦截器需要特别小心,因为他会修改原始数据内容。
这里面这几个拦截器其中Search and Replace InterceptorRegex Extractor Interceptor 我们在工作中使用的比较多一些。

4.本文重点->对采集到的数据按天按类型分目录存储

原始数据下:
video_info: {"id":"14943445328940974601","uid":"840717325115457536","lat":"53.530598","lnt":"-2.5620373","hots":0,"title":"0","status":"1","topicId":"0","end_time":"1494344570","watch_num":0,"share_num":"1","replay_url":null,"replay_num":0,"start_time":"1494344544","timestamp":1494344571,"type":"video_info"}
user_info: {"uid":"861848974414839801","nickname":"mick","usign":"","sex":1,"birthday":"","face":"","big_face":"","email":"abc@qq.com","mobile":"","reg_type":"102","last_login_time":"1494344580","reg_time":"1494344580","last_update_time":"1494344580","status":"5","is_verified":"0","verified_info":"","is_seller":"0","level":1,"exp":0,"anchor_level":0,"anchor_exp":0,"os":"android","timestamp":1494344580,"type":"user_info"}
gift_record: {"send_id":"834688818270961664","good_id":"223","video_id":"14943443045138661356","gold":"10","timestamp":1494344574,"type":"gift_record"}
做案例之前先分析一下:
这份数据中有三种类型的数据,视频信息、用户信息、刷礼物信息,数据都是json格式的,这些数据还有一个共性就是里面都有一个type字段,type字段的值代表数据类型。
当我们的直播平台正常运行的时候,会实时产生这些日志数据,我们希望把这些数据采集到hdfs上进行存储,并且要按照数据类型进行分目录存储,视频数据放一块、用户数据放一块、刷礼物数据放一块。
针对这个需求配置agent的话,source使用基于文件的execsource、channel使用基于文件的channel,我们希望保证数据的完整性和准确性,sink使用hdfs sink。
但是注意了,hdfs sink中的path不能写死,首先是按天,就是需要动态获取日期,然后是因为不同类型的数据要存储到不同的目录中。那也就意味着path路径中肯定要是有变量,除了日期变量还要有数据类型变量,这里的数据类型的格式都是单词中间有一个下划线,但是我们的要求是目录中的单词不要出现下划线,使用驼峰的命名格式。
所以最终在hdfs中需要生成的目录大致是这样的
hdfs://bigdata01:9000/moreType/20230301/videoInfo
hdfs:// bigdata01:9000/moreType/20230301/userInfo
hdfs:// bigdata01:9000/moreType/20230301/giftRecord


那么我们整体的流程就是:Exec Source -> Search and Replace Interceptor->Regex Extractor Interceptor->File Channel->HDFS Sink
那么我们开始实践吧~
首先启动Hadoop集群
(tips:启动start-dfs.sh就可以了,因为没必要用到yarn)

image.png
那下面我们来配置Agent,在bigdata01机器上创建 file-to-hdfs-moreType.conf

image.png 配置的file-to-hdfs-moreType.confbe like:

# 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 /home/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+)"
a1.sources.r1.interceptors.i4.serializers = s1
a1.sources.r1.interceptors.i4.serializers.s1.name = logType
# 配置channel组件
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /home/softwares/flume /data/moreType/checkpoint
a1.channels.c1.dataDirs = /home/softwares/flume/data /moreType/data
# 配置sink组件
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://bigdata01: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.rollCount = 0
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#增加文件前缀和后缀
a1.sinks.k1.hdfs.filePrefix = data
a1.sinks.k1.hdfs.fileSuffix = .log
# 把组件连接起来
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

注意:这里面的拦截器,拦截器可以设置一个或者多个,source采集的每一条数据都会经过所有的拦截器进行处理,多个拦截器按照顺序执行。
关键操作步骤:
先生成测试数据,提前手工把数据添加到 moreType.log 文件中
[root@bigdata01 ~]# cd /home/data/log

image.png
[root@bigdata01 log]# vi moreType.log

image.png 配置moreType.logbe like:

{"id":"14943445328940974601","uid":"840717325115457536","lat":"53.530598","
lnt":"-2.5620373","hots":0,"title":"0","status":"1","topicId":"0","end_time
":"1494344570","watch_num":0,"share_num":"1","replay_url":null,"replay_num"
:0,"start_time":"1494344544","timestamp":1494344571,"type":"video_info"}
{"uid":"861848974414839801","nickname":"mick","usign":"","sex":1,"birthday"
:"","face":"","big_face":"","email":"abc@qq.com","mobile":"","reg_type":"10
2","last_login_time":"1494344580","reg_time":"1494344580","last_update_time
":"1494344580","status":"5","is_verified":"0","verified_info":"","is_seller
":"0","level":1,"exp":0,"anchor_level":0,"anchor_exp":0,"os":"android","tim
estamp":1494344580,"type":"user_info"}
{"send_id":"834688818270961664","good_id":"223","video_id":"149434430451386
61356","gold":"10","timestamp":1494344574,"type":"gift_record"}

5.启动Agent:

[root@bigdata01 flume]# bin/flume-ng agent --name a1 --conf conf --conf-file conf/file-to-hdfs-moreType.conf -Dflume.root.logger=INFO,console

image.png 验证结果,在bigdata01节点上查看:

image.png 打开web UI9870端口可以看见

image.png

image.png

image.png

image.png

或者我们在Linux界面上查看

image.png

image.png

image.png 到此使用-cat命令查看具体文件即可,采集到的数据按天按类型分目录存储案例到此已完成。