Flume(3)source组件之NetcatSource使用介绍

1、概述:

本节首先提供一个基于netcat的source+channel(memory)+sink(logger)的数据传输过程。而后剖析一下NetcatSource中的代码执行逻辑。java

2、flume配置文件:

下面的配置文件netcat.conf中定义了source使用netcat,它会监听44444端口。node

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = locahost
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = logger

# 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

3、命令行启动:

切换到flume的安装目录下,执行下述代码:apache

bin/flume-ng agent --conf conf --conf-file study/netcat.conf --name a1 -Dflume.root.logger=INFO,console

4、利用telnet来直接访问且发送数据:

在命令行中键入如下代码:其中node5是flume所在的主机名。编程

telnet node5 44444

在telnet命令行输入信息:eclipse

image

在flume的启动界面就会输出接收到的数据:socket

image

由此,使用netcat做为source的功能即演示成功了。this

除了利用telnet来发送数据之外,也能够本身实现一个socket编程来向node5主机的44444端口发送数据。spa

固然,咱们发现了一个问题,明明在telnet中发送的数据是:This is flume netcat source!,接收到的数据倒是This is flume ne。数据不完整。后面经过分析一下源码,看能不能找到缘由。命令行

出现上述的显示不完整的状况,是由于咱们使用的是LoggerSink组件,它内部的实现逻辑致使了仅打印了16个字符。线程

image

 

5、agent启动的基本步骤:

image

6、NetcatSource源码剖析:

该类的全路径为org.apache.flume.source.NetcatSource,继承了AbstractSource 并实现了Configurable接口。

因为NetcatSource一个监听服务,因此它是经过EventDrivenSourceRunner来启动一个线程,调用其start()方法的。

image

首先在正式启动source以前,会首先执行configure方法,初始化配置文件中提供的参数:bind\port\ack-every-event\max-line-length。

start()方法以下:

image

该方法内建立一个AcceptHandler内部类实例,实际的监听工做就是在该类的run方法中来实现的。

image

相关文章
相关标签/搜索