flume 企业中的实例案例 默认拦截器
首先flume有一些自身的拦截器 如:TimestampInterceptor 、
HostInterceptor 、r RegexExtractorInterceptor 等, 通过使用不同的拦截器,
实现不同的功能
同时我们有也可以通过java代码 来自定义拦截器,这里先不讲 在hadoop-6
案例场景:A、B 两台日志服务机器实时生产日志主要类型为 access.log、nginx.log、
web.log
需求:
把 A、B 机器中的 access.log、nginx.log、web.log 采集汇总到 C 机器上
然后统一收集到 hdfs 中。
但是在 hdfs 中要求的目录为:
/source/logs/access/20160101/**
/source/logs/nginx/20160101/**
/source/logs/web/20160101/**
你会想到什么?
首先要获取这三组文件,但是要根据access /nginx/web 来分别存储到对应的目录文件下
我们要解决的就是 在flume中 传递type 分别为 access/nginx/web
上传hdfs 打印时 我们动态获取这个type 指定hdfs上根据type来存放在不同的目录下。
话不多说 直接代码:
-
在note1上
vi exec_source_avro_sink.conf#Name the components on this agent
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1
#Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs6/access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access
a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /root/logs6/nginx.log
a1.sources.r2.interceptors = i2
a1.sources.r2.interceptors.i2.type = static
a1.sources.r2.interceptors.i2.key = type
a1.sources.r2.interceptors.i2.value = nginx
a1.sources.r3.type = exec
a1.sources.r3.command = tail -F /root/logs6/web.log
a1.sources.r3.interceptors = i3
a1.sources.r3.interceptors.i3.type = static
a1.sources.r3.interceptors.i3.key = type
a1.sources.r3.interceptors.i3.value = web
#Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = note2
a1.sinks.k1.port = 41414
#Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1
2.note2上
vi avro_source_hdfs_sink.conf
#定义agent名, source、channel、sink的名称
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#定义source
a1.sources.r1.type = avro
a1.sources.r1.bind = note2
a1.sources.r1.port =41414
#添加时间拦截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder
#定义channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 2000
a1.channels.c1.transactionCapacity = 1000
#定义sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path=hdfs://note1:9000/source/logs/%{type}/%Y%m%d
a1.sinks.k1.hdfs.filePrefix =events
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
#时间类型
#a1.sinks.k1.hdfs.useLocalTimeStamp = true
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.rollInterval = 10
a1.sinks.k1.hdfs.rollSize =0
a1.sinks.k1.hdfs.minBlockReplicas=1
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
#批量写入hdfs的个数
a1.sinks.k1.hdfs.batchSize = 1
#flume操作hdfs的线程数(包括新建,写入等)
a1.sinks.k1.hdfs.threadsPoolSize=10
#操作hdfs超时时间
a1.sinks.k1.hdfs.callTimeout=30000
#组装source、channel、sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
3.分别启动note2和note1
bin/flume-ng agent -c conf -f conf/avro_source_hdfs_sink.conf -name a1 -Dflume.root.logger=DEBUG,console
bin/flume-ng agent -c conf -f conf/exec_source_avro_sink.conf -name a1 -Dflume.root.logger=DEBUG,console
4.在note1上自己创建对应的目录文件 然后持续添加数据
while true;do echo “access access…” >> /root/logs6/access.log;sleep 0.5;done
while true;do echo “web web…” >> /root/logs6/web.log;sleep 0.5;done
while true;do echo “nginx nginx…” >> /root/logs6/nginx.log;sleep 0.5;done
5.其中要注意一点 在小文件传输的过程中 同时会复制副本数 不配置默认是3
这可能导致我们的flume 不能正常运行 我们需要在 note2 中添加一行配置
a1.sinks.k1.hdfs.minBlockReplicas=1
即可。