flume介绍:
flume源码地址:https://github.com/apache/flume/
- flume的核心角色是agent,agent是一个java进程。agent相当于一个快递员。
- agent包括source,channel,sink,channel是为了防止source到sink数据丢失
- 他们之间流动传输的最小单元是event,如果是文件则一行数据就是一个event
- 一个event包括event headers,event body,event,其中event body是二进制数据
- flume的鲁棒性太差,一旦报错就停止采集
- flume的启动入口是:org.apache.flume.node.Application
- flume关闭顺序为source,sink,channel
- 启动顺序channel,sink,source
flume的容错
为了防止数据丢失采用事务机制,首先写到channel里,等到sink完成,才算结束。如果失败会保存在channel里。
- File Channel 写到磁盘里
- 如果使用memoryChannel,进程挂掉保存在memory中的数据会丢失,在源码中可以看到使用LinkedBlockingDeque双向阻塞队列来保存的数据
配置采集文件夹flume下沉到hdfs:
具体的采集可以参照:http://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#flume-sources
使用spooldir监控文件夹,如果文件夹下面有同名文件将会停止,无法启动
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
##注意:不能往监控目中重复丢同名文件
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /root/logs2
a1.sources.r1.fileHeader = true #保留文件的绝对路径
a1.sources.r1.fileHeaderKey=filename #设置header的key
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = %{filename} #存放时会按照源文件的绝对路径存放
a1.sinks.k1.hdfs.round = true #开启文件夹滚动
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3 #文件滚动,3m
a1.sinks.k1.hdfs.rollSize = 20 #文件滚动,字节大小
a1.sinks.k1.hdfs.rollCount = 5 #event事件数,这几个条件谁先达到就滚动
a1.sinks.k1.hdfs.batchSize = 1
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# 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.sinks.k1.channel = c1
flume中header的作用
header 中插入自定义的键值对,可以根据%{key}拿到对应的value
flume的拦截器
拦截器是处理从source到channel的的数据处理
-
静态拦截器:
#指定拦截器的名字 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
会将值加到headers中,可以通过%{type}获取对应的值
-
时间戳拦截器
a1.sinks.k1.hdfs.useLocalTimeStamp = true
Flume 的load-balance 、failover:
负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种
算法。
原理:
a1.sinkgroups = g1
a1.sinkgroups.g1.sinks = k1 k2 k3
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true #如果开启,则将失败的 sink 放入黑名单
a1.sinkgroups.g1.processor.selector = round_robin # 另外还支持 random
a1.sinkgroups.g1.processor.selector.maxTimeOut=10000 #在黑名单放置的超时时间,超时结
束时,若仍然无法接收,则超时时间呈指数增长
faiover
架构图和负载均衡一样
a1.sinkgroups = g1
a1.sinkgroups.g1.sinks = k1 k2 k3
a1.sinkgroups.g1.processor.type = failover
a1.sinkgroups.g1.processor.priority.k1 = 5 #优先级值, 绝对值越大表示优先级越高
a1.sinkgroups.g1.processor.priority.k2 = 7
a1.sinkgroups.g1.processor.priority.k3 = 6
a1.sinkgroups.g1.processor.maxpenalty = 20000 #失败的 Sink 的最大回退期(millis)