• 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
    *
    *
    * */
  • 相关阅读:
    算法笔记 #003# 堆排序
    算法导论(第三版)练习 6.2-1 ~ 6.2-6
    Python开发【第六篇】:模块
    Python开发【第五篇】:Python基础之杂货铺
    Python开发【第四篇】:Python基础之函数
    Python开发【第三篇】:Python基本数据类型
    Python开发【第二篇】:初识Python
    python mysql
    跟着ttlsa一起学zabbix监控呗
    zabbix进程构成
  • 原文地址:https://www.cnblogs.com/7749ha/p/14135327.html
Copyright © 2020-2023  润新知