Flume的组件之一 Source
Source:
简介
Source,是agent的接受消息来源组件,负责将Event传递给channel;
源码
@InterfaceAudience.Public
@InterfaceStability.Stable
public interface Source extends LifecycleAware, NamedComponent {
public void setChannelProcessor(ChannelProcessor channelProcessor);
public ChannelProcessor getChannelProcessor();
}
从源码中可以看到,两个抽象方法 set/getChannelProcessor ,我们大概可以了解到,source是通过ChannelProcessor, 来实现传递Event的;
1. AVRO
过程:
- 将数据用AVRO序列化之后在进行收集,结合AVRO Sink可以实现多级流、扇入流和扇出流。
- 监听Avro端口来接受外部avro客户端的事件流。
- avro-source接受到的是经过avro序列化后的数据,然后反序列化继续传输。
配置:
- type: Avro source的别名是avro,也可以使用完整类别名称,org.apache.flume.source.AvroSource;
- bind: 绑定的IP地址或主机名。使用0.0.0.0绑定机器所有端口
- port: 绑定监听端口端口
#给Agent,以及其中的sources、channels和sinks命名
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#定制source,绑定channel、主机以及端口
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 8090
#设置sinks的类型
a1.sinks.k1.type = logger
#use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.transactionCapacity = 100
#Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
agent-avro客户端指令:
[root@master bin]# flume-ng agent -n a1 -c /usr/flume/conf/ --conf-file /usr/flume/conf/avro.conf -Dflume.root.logger=INFO,console
2. Spooling Directory
-可以持续监控指定的目录。当前目录在添加文件的时候会自动收集该文件中 的内容,然后解析该文件的内容写入到channel中,当sink处理完成之后,说明一个文件被收集完成,自动在晚间末尾添加 .COMPLETED后缀;
- 但是如果以追加的方式处理文件,source并不能识别;
一旦文件被放到spool收集的文件中,便不能修改,否则flume会报错;
有重名的文件也会报错
#配置Agent a1 的组件
a1.sources=r1
a1.channels=c1
a1.sinks=s1
#描述/配置a1的source1
a1.sources.r1.type=spooldir
a1.sources.r1.spoolDir=/home/work/data
#描述sink
a1.sinks.s1.type=logger
#描述内存channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
#为channel 绑定 source和sink
a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
3. Http
- 启动后会拉起一个Web服务来监听指定的端口、IP,接收HTTP请求,只能接收GET或者POST请求,但是注意GET请求不稳定,使用POST请求居多;
- flume解析http请求(请求头、响应体等信息),需要提供一个可插拔的处理器来将请求转化为事件对象event,这个类必须实现HttpSourcehandler接口;
- 这个处理器能够接受一个httpServletRequest对象,并返回一个Flume Event对象;
- 如果不指定默认是 JsonHandler,默认编码为 UTF-8
- 格式 - BlobHandler 适合大文件传输的处理器
a1.sources=r1
a1.sinks=s1
a1.channels=c1
#描述/配置a1的source1
a1.sources.r1.type=http
a1.sources.r1.port=8888
#描述sink
a1.sinks.s1.type=logger
#描述内存channel
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
#为channel 绑定 source和sink
a1.sources.r1.channels=c1
a1.sinks.s1.channel=c1
模拟一次http的Post请求:curl -X POST -d ‘[{“headers”:{“a”:“a1”,“b”:“b1”},“body”:“hello http-flume”}]’ http://0.0.0.0:8090
4. 自定义Source
- Source是顶级接口
- 实现类EventDrivenSource 事件驱动Source 被动型Source,使用过程线程都需要自己定义;
- 实现类 PollableSource 拉取Source-主动型Source,自己提供线程数据放入Channel中;
- 实现 Configurable
5. Exec source
- 可以将命令的产生作为信息来源来进行传递,指定的执行名令,输出的结果作为日志;
- 配置项 说明
channels 绑定的通道
type exec
command 要执行的命令
selector.* 选择器配置
interceptors.* 拦截器列表配置
6. Netcat Source
- 一个NetCat Source用来监听一个指定端口,并接收监听到的数据
- 接收的数据是字符串形式
- 配置项 说明
channels 绑定通道
type netcat
port 指定要绑定到的端口号
selector.* 选择器配置
interceptors.* 拦截器配置 - 启动后 通过nc 来访问。