Flume开发 -- 自定义sink
程序员文章站
2022-06-14 13:39:55
...
一、介绍
Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个 Flume Agent。
Sink 是完全事务性的。在从 Channel 批量删除数据之前,每个 Sink 用 Channel 启动一个事务。批量事件一旦成功写出到存储系统或下一个 Flume Agent,Sink 就利用 Channel 提交事务。事务一旦被提交,该 Channel 从自己的内部缓冲区删除事件。
Sink 组件目的地包括 hdfs、logger、avro、thrift、ipc、file、null、HBase、solr、自定义。官方提供的 Sink 类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些 Sink。
官方也提供了自定义 sink 的接口:
https://flume.apache.org/FlumeDeveloperGuide.html#sink
根据官方说明自定义MySink 需要继承 AbstractSink 类并实现 Configurable 接口。
实现相应方法:
configure(Context context) //初始化 context(读取配置文件内容)
process() //从 Channel 读取获取数据(event),这个方法将被循环调用。
使用场景:读取 Channel 数据写入 MySQL 或者其他文件系统。
二、需求
使用 flume 接收数据,并在 sink 端给每条数据添加前缀和后缀,输出到控制台。前后缀可在 flume 任务配置文件中配置。
三、流程
四、案例实操
4.1 环境准备
创建一个 maven 项目,添加 pom 依赖:
<dependencies>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
4.2 编写代码
package customersink;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @description:
* @author: hyr
* @time: 2020/3/4 9:16
*/
public class MySink extends AbstractSink implements Configurable {
// 创建 Logger 对象
private static final Logger LOG = LoggerFactory.getLogger(MySink.class);
private String prefix;
private String suffix;
/**
* 初始化配置信息
*/
@Override
public void configure(Context context) {
// 读取配置文件内容,有默认值
prefix = context.getString("prefix", "hello:");
// 读取配置文件内容,无默认值
suffix = context.getString("suffix");
}
/**
* 1、获取 Channel
* 2、从 Channel 获取事务以及数据
* 3、发送数据
*/
@Override
public Status process() {
// 声明返回值状态信息
Status status;
// 获取当前 Sink 绑定的 Channel
Channel ch = getChannel();
// 获取事务
Transaction txn = ch.getTransaction();
// 声明事件
Event event;
// 开启事务
txn.begin();
try {
// 读取 Channel 中的事件,直到读取到事件结束循环
// 防止在后面开启和关闭事务的过程中,出现异常
while (true) {
event = ch.take();
if (event != null) {
break;
}
}
// 处理事件(打印)
LOG.info(prefix + new String(event.getBody()) + suffix);
// 事务提交
txn.commit();
// 状态为 READY
status = Status.READY;
} catch (ChannelException e) {
e.printStackTrace();
// 遇到异常,事务回滚
txn.rollback();
status = Status.BACKOFF;
} finally {
// 关闭事务
txn.close();
}
return status;
}
}
4.3 打包代码
用 maven 将代码打成 jar 包,并且上传到 flume 下的 lib 文件夹。
4.4 编写 mySink.conf
# 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 = localhost
a1.sources.r1.port = 44444
# Describe the sink
a1.sinks.k1.type = customersink.MySink
a1.sinks.k1.prefix = hello-
a1.sinks.k1.suffix = -hello
# 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
4.5 开启任务
[aaa@qq.com flume]$ bin/flume-ng agent -c conf/ -f job/mySink.conf -n a1 -Dflume.root.logger=INFO,console
[aaa@qq.com ~]$ nc localhost 44444
hyr
OK
aaa
OK
bbb
OK