Flink入门 - CoGroup和Join

拟墨画扇 提交于 2019-12-05 03:49:31
/* 
*CoGroup 
*/ 

final StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment(); 
        
        DataStream<Tuple2<String, String>> dataStream1 = streamExecutionEnvironment.socketTextStream("127.0.0.1", 9000) 
                .map(new MapFunction<String, Tuple2<String, String>>() { 
                    @Override 
                    public Tuple2<String, String> map(String s) throws Exception { 
                        List<String> strs = Arrays.asList(s.split(" ")); 
                        return new Tuple2<>(strs.get(0), strs.get(1)); 
                    } 
                }); 

        DataStream<Tuple2<String, String>> dataStream2 = streamExecutionEnvironment.socketTextStream("127.0.0.1", 9001) 
                .map(new MapFunction<String, Tuple2<String, String>>() { 
                    @Override 
                    public Tuple2<String, String> map(String s) throws Exception { 
                        List<String> strs = Arrays.asList(s.split(" ")); 
                        return new Tuple2<>(strs.get(0), strs.get(1)); 
                    } 
                }); 

        dataStream1.coGroup(dataStream2) 
                .where(new KeySelector<Tuple2<String, String>, String>() { 
                    @Override 
                    public String getKey(Tuple2<String, String> stringStringTuple2) throws Exception { 
                        return stringStringTuple2.f0; 
                    } 
                }) 
                .equalTo(new KeySelector<Tuple2<String, String>, String>() { 
                    @Override 
                    public String getKey(Tuple2<String, String> stringStringTuple2) throws Exception { 
                        return stringStringTuple2.f0; 
                    } 
                }) 
                .window(ProcessingTimeSessionWindows.withGap(Time.seconds(30))) 
                .trigger(CountTrigger.of(1)) 
                .apply(new CoGroupFunction<Tuple2<String, String>, Tuple2<String, String>, String>() { 
                    @Override 
                    public void coGroup(Iterable<Tuple2<String, String>> first, Iterable<Tuple2<String, String>> second, Collector<String> out) throws Exception { 
                        StringBuilder stringBuilder = new StringBuilder("Data Stream1: \n"); 
                        first.forEach(item -> stringBuilder.append(item.f0 + "<=>" + item.f1 + "\n")); 

                        stringBuilder.append("Data Stream2: \n"); 
                        second.forEach(item -> stringBuilder.append(item.f0 + "<=>" + item.f1 + "\n")); 
                        out.collect(stringBuilder.toString()); 
                    } 
                }).print(); 

        streamExecutionEnvironment.execute(); 
/** 
* Join 
* Join条件为两个流中的数据((String, String))的第一个元素相同 
*/ 
final StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment(); 

        DataStream<Tuple2<String, String>> dataStream1 = streamExecutionEnvironment.fromElements(new Tuple2<>("hello", "flink")); 

        DataStream<Tuple2<String, String>> dataStream2 = streamExecutionEnvironment.fromElements(new Tuple2<>("hello", "blink")); 

        dataStream1.join(dataStream2) 
                .where(new KeySelector<Tuple2<String, String>, String>() { 
                    @Override 
                    public String getKey(Tuple2<String, String> stringStringTuple2) throws Exception { 
                        return stringStringTuple2.f0; 
                    } 
                }) 
                .equalTo(new KeySelector<Tuple2<String, String>, String>() { 
                    @Override 
                    public String getKey(Tuple2<String, String> stringStringTuple2) throws Exception { 
                        return stringStringTuple2.f0; 
                    } 
                }) 
                .window(ProcessingTimeSessionWindows.withGap(Time.seconds(30))) 
                .trigger(CountTrigger.of(1)) 
                .apply(new JoinFunction<Tuple2<String, String>, Tuple2<String, String>, String>() { 
                    @Override 
                    public String join(Tuple2<String, String> first, Tuple2<String, String> second) throws Exception { 
                        return first.f1 + "<=>" + second.f1; 
                    } 
                }).print(); 
        streamExecutionEnvironment.execute(); 

// 运行结果 
2> flink<=>blink 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!