• flink 使用processFunction函数的sideOutPut实现filter操作(java版)


    import org.apache.flink.api.common.functions.MapFunction;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.DataStreamSource;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.ProcessFunction;
    import org.apache.flink.util.Collector;
    import org.apache.flink.util.OutputTag;
    
    /**
     * # _*_ coding:utf-8 _*_
     * # Author:xiaoshubiao
     * # Time : 2020/12/14 19:14
     **/
    public class processFunction_test {
        public static void main(String[] args) throws Exception{
            StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
    
            DataStreamSource<String> localhost = executionEnvironment.socketTextStream("localhost", 1111);
            // 输入a,1这样的数据,通过ProcessFunction的sideOutPut实现filter的操作
            // 区分是否属于a
            SingleOutputStreamOperator<Tuple2<String, Integer>> map = localhost.map(
                    new MapFunction<String, Tuple2<String, Integer>>() {
                        @Override
                        public Tuple2<String, Integer> map(String s) throws Exception {
                            String[] split = s.split(",");
                            return new Tuple2<>(split[0], Integer.valueOf(split[1]));
                        }
                    }
            );
    
            SingleOutputStreamOperator<String> process = map.process(
                    /*
                    *参数一:输入数据类型。比如(a,1)
                    * 参数二:输出数据类型。比如(属于a)
                    * */
                    new ProcessFunction<Tuple2<String, Integer>, String>() {
                        @Override
                        public void processElement(Tuple2<String, Integer> stringIntegerTuple2, Context context, Collector<String> collector) throws Exception {
    
                            if (stringIntegerTuple2.f0.equals("a")) {
                                // 直接返回
                                collector.collect("属于a");
                            } else {
                                // 通过上下文输出,定义输出标签和值
                                context.output(new OutputTag<String>("is_not_a"){}, "不属于a");
                            }
                        }
                    }
            );
            process.print("is_a");
            process.getSideOutput(new OutputTag<String>("is_not_a"){}).print("is_not_a");
    
            executionEnvironment.execute();
        }
    }
    /*
    * 输入      输出
    * a,1       属于a
    * b,1       不属于a
    * c,1       不属于a
    *
    *
    * */
  • 相关阅读:
    大型网站技术架构-阅读笔记1
    如何发挥一个字节的极限,存储大量内容
    利用easyui创建一个简单的登录页面
    linux tomcat 快捷操作
    linux 安装jdk
    Linux-查看服务器的信息
    HTTP协议(1)
    Linux-ps命令
    Linux-tcpdump命令
    转载-测试新人培训方法之目标法
  • 原文地址:https://www.cnblogs.com/7749ha/p/14135327.html
Copyright © 2020-2023  润新知