flink学习(4)之join操作、笛卡尔操作、外连接操作

1、join操作

import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;

import java.util.ArrayList;
import java.util.List;

public class FlinkDemo2 {
    public static void main(String[] args) throws Exception {
        //获取flink的执行环境
        ExecutionEnvironment env=ExecutionEnvironment.getExecutionEnvironment();
        //准备数据1
        List<Tuple2<Integer,String>> data1=new ArrayList<Tuple2<Integer,String>>();
        data1.add(new Tuple2(1,"Tom"));
        data1.add(new Tuple2(2,"Lily"));
        data1.add(new Tuple2(3,"HanMeimei"));
        data1.add(new Tuple2(4,"Json"));
        //准备数据2
        List<Tuple2<Integer,String>> data2=new ArrayList<Tuple2<Integer,String>>();
        data2.add(new Tuple2(1,"Beijing"));
        data2.add(new Tuple2(2,"Shanghai"));
        data2.add(new Tuple2(3,"Hangzhou"));
        data2.add(new Tuple2(4,"Chongqin"));
        //通过俩个list的数据源创建flink的数据模型
        DataSet<Tuple2<Integer,String>> table1=env.fromCollection(data1);
        DataSet<Tuple2<Integer,String>> table2=env.fromCollection(data2);
        //做链接操作          第一张表的第一列  第二张表的第一个字段
        table1.join(table2).where(0).equalTo(0)
                .with(new JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,String>, Tuple3<Integer,String,String>>() {
                   //用等值连接后产生的table1和table2
                    public Tuple3 join(Tuple2 table1, Tuple2 table2) throws Exception {
                        return new Tuple3(table1.f0,table1.f1,table2.f1);
                    }
                }).print();
        env.execute("FlinkDemo2");
    }
}

2、笛卡尔 操作

System.out.println("****************笛卡尔操作***********************");
        table1.cross(table2).print();

3、外连接,全外连接

import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;

import java.util.ArrayList;
import java.util.List;

public class FlinkDemo3 {
    public static void main(String[] args) throws Exception {
        //获取flink的执行环境
        ExecutionEnvironment env=ExecutionEnvironment.getExecutionEnvironment();
        //准备数据1
        List<Tuple2<Integer,String>> data1=new ArrayList<Tuple2<Integer,String>>();
        data1.add(new Tuple2(1,"Tom"));
        data1.add(new Tuple2(3,"HanMeimei"));
        data1.add(new Tuple2(4,"Json"));
        //准备数据2
        List<Tuple2<Integer,String>> data2=new ArrayList<Tuple2<Integer,String>>();
        data2.add(new Tuple2(1,"Beijing"));
        data2.add(new Tuple2(2,"Shanghai"));
        data2.add(new Tuple2(4,"Chongqin"));
        //通过俩个list的数据源创建flink的数据模型
        DataSet<Tuple2<Integer,String>> table1=env.fromCollection(data1);
        DataSet<Tuple2<Integer,String>> table2=env.fromCollection(data2);
        System.out.println("*****************左外连接**********************");
        table1.leftOuterJoin(table2).where(0).equalTo(0)
                .with(new JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,String>, Tuple3<Integer,String,String>>() {
                    public Tuple3 join(Tuple2 table1, Tuple2 table2) throws Exception {
                        if (table2==null){
                            return new Tuple3(table1.f0,table1.f1,null);
                        }else {
                            return new Tuple3(table1.f0,table1.f1,table2.f1);
                        }
                    }
                }).print();
        System.out.println("*****************右外连接**********************");
        table1.rightOuterJoin(table2).where(0).equalTo(0)
                .with(new JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,String>, Tuple3<Integer,String,String>>() {
                    public Tuple3 join(Tuple2 table1, Tuple2 table2) throws Exception {
                        if (table1==null){
                            return new Tuple3(table2.f0,null,table2.f1);
                        }else {
                            return new Tuple3(table1.f0,table1.f1,table2.f1);
                        }
                    }
                }).print();
        System.out.println("*****************全连接**********************");
        table1.fullOuterJoin(table2).where(0).equalTo(0)
                .with(new JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,String>, Tuple3<Integer,String,String>>() {
                    public Tuple3 join(Tuple2 table1, Tuple2 table2) throws Exception {
                        if (table1==null){
                            return new Tuple3(table2.f0,null,table2.f1);
                        }else if (table2==null){
                            return new Tuple3(table1.f0,table1.f1,null);
                        }else {
                            return new Tuple3(table1.f0,table1.f1,table2.f1);
                        }
                    }
                }).print();
        env.execute("FlinkDemo3");
    }
}

外连接主要是用来俩张表用显示不能连接的数据,左连接用于显示左边有数据不能连接右边的数据,右链接是用来显示右边有数据不能连接右边的数据。全连接是用来显示俩边不能连接的数据都显示出来。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枣泥馅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值