Flink算子 CoMapOperator

package com.claroja;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;


public class CoMapOperator {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<Tuple2<String,Integer>> stream1 = env.fromElements(new Tuple2("wang",120),new Tuple2("wei",130));
        DataStream<Tuple2<String,Integer>> stream2 = env.fromElements(new Tuple2("wang",16),new Tuple2("wei",18));
        //两种不同的keyby方式
//        ConnectedStreams<Tuple2<String,Integer>,Tuple2<String,Integer>> connected = stream1.keyBy(0).connect(stream2.keyBy(0));
        ConnectedStreams<Tuple2<String,Integer>,Tuple2<String,Integer>> connected = stream1.connect(stream2).keyBy(0,0);
        connected.map(new MyCoMapFunction()).print();
        env.execute();

    }

    static class MyCoMapFunction implements CoMapFunction<Tuple2<String,Integer>,Tuple2<String,Integer>,String>{
        //map1来自第一条流的元素
        @Override
        public String map1(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
            return stringIntegerTuple2.getField(0) +"\t"+stringIntegerTuple2.getField(1);
        }
        //map2来自第二条流的元素
        @Override
        public String map2(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
            return stringIntegerTuple2.getField(0) +"\t"+stringIntegerTuple2.getField(1);
        }
    }
}

/*
6> wei	130
6> wei	18
1> wang	120
1> wang	16
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值