4. Flink traditionally gives parameters to operators through constructors "In-depth understanding of flink series"

1. Author of the book "In-depth Understanding of Flink Core Design and Practice Principles" 2.
Developer of the popular GitHub project fink-boot (800+), dedicated to the ecological integration of flink and spring 3. Former technical manager of a listed top consulting company, now a senior developer of a large factory 4. Certificate: software designer, Apache kylin administrator 5. Direction: java server development, distributed development, real-time computing, big data
development
engineer

passed to the operator via the constructor parameter

Flink provides three ways to simplify the process of passing parameters to user-defined functions: parameters can be passed to functions using the constructor or withParameters(Configuration) method or the ExecutionConfig interface.

1 via the constructor

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class ConstructorTemplate {

    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
       
        //生成一个1到20的数字序列的数据流。
        DataStream<Long> dataStream = env.generateSequence(1,20);

        //将ParamBean对象的参数传入Filter操作实例中
        DataStream<Long> resultStream=dataStream.filter(new FilterConsrtucted(new ParamBean("intsmaze",10)));

        resultStream.print("constructed stream is ");
        env.execute("ParamTemplate intsmaze");
    }
    
    //用户定义的Filter函数实现了FilterFunction接口
    private static class FilterConsrtucted implements FilterFunction<Long> {

        private final ParamBean paramBean;
        //Filter函数的构造函数参数为ParamBean对象,用于接收传入的参数值
        public FilterConsrtucted(ParamBean paramBean) {
            this.paramBean = paramBean;
        }

        @Override
        public boolean filter(Long value) {
            return value > paramBean.getFlag();
        }
    }
}

public class ParamBean implements Serializable {

    private String name;

    private int flag;

    public ParamBean(String name, int flag) {
        this.name = name;
        this.flag = flag;
    }
    ......get,set方法
}

The output results are as follows:

constructed stream is :2> 12
constructed stream is :1> 11
constructed stream is :2> 14
constructed stream is :1> 13
constructed stream is :2> 16
constructed stream is :1> 15
constructed stream is :2> 18
constructed stream is :1> 17
constructed stream is :2> 20
constructed stream is :1> 19

Note: If the parameter passed to the user-defined function is not a basic type but a custom type, then the type needs to implement the Serializable interface, otherwise the following exception will be reported when the program runs.

Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: The implementation of the RichFilterFunction is not serializable. The object probably contains or references non serializable fields.

Guess you like

Origin blog.csdn.net/hbly979222969/article/details/125039899