8、Flume高阶自定义组件_Flume自定义Sink

1.1.自定义Sink说明

sink是flume中用于指定数据下沉地的组件。自带的已经很多,对于某些sink如果没有我们想要的,也可以自定义sink实现将数据保存到我们想要的地方去,例如kafka,或者mysql,或者文件等等都可以

需求如下:从网络端口当中发送数据,自定义sink,使用sink从网络端口接收数据,然后将数据保存到本地文件当中去。

1.2.自定义Sink原理实现

自定义MySink

public class MySink extends AbstractSink implements Configurable {
    private Context context ;
    private String filePath = "";
    private String fileName = "";
    private File fileDir;

    //这个方法会在初始化调用,主要用于初始化我们的Context,获取我们的一些配置参数
    @Override
    public void configure(Context context) {
        try {
            this.context = context;
            filePath = context.getString("filePath");
            fileName = context.getString("fileName");
            fileDir = new File(filePath);
            if(!fileDir.exists()){
                fileDir.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //这个方法会被反复调用
    @Override
    public Status process() throws EventDeliveryException {
        Event event = null;
        Channel channel = this.getChannel();
        Transaction transaction = channel.getTransaction();
        transaction.begin();
        while(true){
            event = channel.take();
            if(null != event){
                break;
            }
        }
        byte[] body = event.getBody();
        String line = new String(body);
        try {
            FileUtils.write(new File(filePath+File.separator+fileName),line,true);
            transaction.commit();
        } catch (IOException e) {
            transaction.rollback();
            e.printStackTrace();
            return Status.BACKOFF;
        }finally {
            transaction.close();
        }
        return Status.READY;
    }
}

1.3 功能测试

将代码使用打包插件,打成jar包,注意一定要将commons-langs这个依赖包打进去,放到flume的lib目录下
开发flume的配置文件:

a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = node-1
a1.sources.r1.port = 5678
a1.sources.r1.channels = c1
# # Describe the sink
a1.sinks.k1.type = cn.itcast.flumesink.MySink
a1.sinks.k1.filePath=/export/servers
a1.sinks.k1.fileName=filesink.txt
# # 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

1.4启动flume,并且使用telnet测试:

bin/flume-ng agent -c conf -f conf/filesink.conf -n a1 -Dflume.root.logger=INFO,console

Telnet node-1 5678 连接到机器端口上输入数据。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • title: Flume构建日志采集系统date: 2018-02-03 19:45tags: [flume,k...
    溯水心生阅读 16,159评论 3 25
  • 1. Flume简介 Apache Flume是一个分布式的、可靠的、可用的,从多种不同的源收集、聚集、移动大量日...
    奉先阅读 4,529评论 2 5
  • (一)Flume概述 Flume是由Cloudera提供的一个分布式、高可靠、高可用的服务,用于分布式的海量日志的...
    Sam_L阅读 1,209评论 0 1
  • .Azkaban工作流引擎和Flume数据采集 Azkaban介绍 一、Azkaban简介 为什么需要工作流调度系...
    依天立业阅读 2,109评论 0 2
  • 刚刚姐姐家的儿子清锋打电话给我,他是9月份到嘉兴当兵的,从开始的不适应到现在安排到杭州,能感觉他慢慢适应了而且有点...
    心理咨询师萍阅读 233评论 0 1