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

本文介绍了如何在ApacheFlink中使用自定义的SourceFunction和ParallelSourceFunction创建数据流,展示了如何设置并行度以及RichSourceFunction的额外特性,如open和close方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 关了");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值