Flink自定义Source的四种实现方式

public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        /**
         * 自定义Source
         * 可以实现 SourceFunction 或者 RichSourceFunction , 这两者都是非并行的 source 算子
         * 也可实现 ParallelSourceFunction 或者 RichParallelSourceFunction , 这两者都是可并行的
         * source 算子
         * Rich-》带有open close getRuntimeContext方法
         * parllel=》可并行
         */
        //实现了SourceFunction的自定义Source并行度只能为1
        DataStreamSource<Student> stream1 = env.addSource(new MySource_01());
        DataStreamSource<Student> stream2 = env.addSource(new MySource_02()).setParallelism(2);
        DataStreamSource<Student> stream3 = env.addSource(new MySource_03());
        DataStreamSource<Student> stream4 = env.addSource(new MySource_04());
        stream3.map(JSON::toJSONString).print();


        env.execute();
    }










class MySource_01 implements SourceFunction<Student> {
    private Boolean flag = true;

    @Override
    public void run(SourceContext<Student> sourceContext) throws Exception {
        Student st = new Student();
        while (flag) {
            Thread.sleep(1000);
            st.setName(RandomStringUtils.randomAlphabetic(10).toUpperCase());
            st.setAge(RandomUtils.nextInt(10, 1000));
            st.setGender(RandomUtils.nextInt(10, 1000) > RandomUtils.nextInt(10, 1000) ? "male" : "female");
            sourceContext.collect(st);
        }
    }

    @Override
    public void cancel() {
    }
}


class MySource_02 implements ParallelSourceFunction<Student> {
    private Boolean flag = true;

    @Override
    public void run(SourceContext<Student> sourceContext) throws Exception {
        Student st = new Student();
        while (flag) {
            Thread.sleep(1000);
            st.setName(RandomStringUtils.randomAlphabetic(10).toUpperCase());
            st.setAge(RandomUtils.nextInt(10, 1000));
            st.setGender(RandomUtils.nextInt(10, 1000) > RandomUtils.nextInt(10, 1000) ? "male" : "female");
            sourceContext.collect(st);
        }
    }

    @Override
    public void cancel() {
    }
}

class MySource_03 extends RichSourceFunction<Student> {
    private Boolean flag = true;

    @Override
    public void open(Configuration parameters) throws Exception {
        RuntimeContext runtimeContext = getRuntimeContext();
        String taskName = runtimeContext.getTaskName();
        int indexOfThisSubtask = runtimeContext.getIndexOfThisSubtask();
        System.out.println(taskName + "-" + indexOfThisSubtask);
    }

    @Override
    public void run(SourceContext<Student> sourceContext) throws Exception {
        Student st = new Student();
        while (flag) {
            Thread.sleep(1000);
            st.setName(RandomStringUtils.randomAlphabetic(10).toUpperCase());
            st.setAge(RandomUtils.nextInt(10, 1000));
            st.setGender(RandomUtils.nextInt(10, 1000) > RandomUtils.nextInt(10, 1000) ? "male" : "female");
            sourceContext.collect(st);
        }
    }

    @Override
    public void cancel() {

    }

    @Override
    public void close() throws Exception {
        System.out.println("haha 关了");
    }
}


class MySource_04 extends RichParallelSourceFunction<Student> {
    private Boolean flag = true;

    @Override
    public void open(Configuration parameters) throws Exception {
        RuntimeContext runtimeContext = getRuntimeContext();
        String taskName = runtimeContext.getTaskName();
        int indexOfThisSubtask = runtimeContext.getIndexOfThisSubtask();
        System.out.println(taskName + "-" + indexOfThisSubtask);
    }

    @Override
    public void run(SourceContext<Student> sourceContext) throws Exception {
        Student st = new Student();
        while (flag) {
            Thread.sleep(1000);
            st.setName(RandomStringUtils.randomAlphabetic(10).toUpperCase());
            st.setAge(RandomUtils.nextInt(10, 1000));
            st.setGender(RandomUtils.nextInt(10, 1000) > RandomUtils.nextInt(10, 1000) ? "male" : "female");
            sourceContext.collect(st);
        }
    }

    @Override
    public void cancel() {

    }

    @Override
    public void close() throws Exception {
        System.out.println("haha 关了");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink是一个高性能流式处理引擎,可以读取各种各样的数据源,包括自定义的源。自定义源是使用Flink的一种方式,主要是为了读取一些非标准的数据源或者改善性能表现。 自定义source是一个接口,需要实现org.apache.flink.streaming.api.functions.source.SourceFunction接口。该接口只有两个方法,一个是run(),另一个是cancel()。在run()中实现数据读取的逻辑,cancel()用于取消读取。自定义source主要包括数据什么时候开始读取,如何读取数据及什么时候读取结束等。 实现自定义source需要在程序入口处调用StreamExecutionEnvironment对象中的addSource()方法,将自定义source添加到批处理中。示例如下: ```java DataStreamSource<String> dataSource = env.addSource(new MySource()); ``` 其中,MySource自定义的数据源。 在自定义source中,可以采用文件缓存方式来提升读取性能。通过FileChannel打开文件,使用ByteBuffer读取文件,然后将ByteBuffer通过Flink的DataStream传递给后续算子处理。这种方式可以大大提升文件读取的性能,减少文件IO的次数。示例如下: ```java try { FileInputStream inputStream = new FileInputStream(filePath); FileChannel inChannel = inputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 5); while (inChannel.read(buffer) != -1) { buffer.flip(); sourceContext.collect(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } ``` 自定义source实现需要根据具体的数据源进行,但总体来说,实现自定义源并不复杂,只需要理解Flink数据处理的机制,并编写封装好的代码即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值