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");
}
}
外连接主要是用来俩张表用显示不能连接的数据,左连接用于显示左边有数据不能连接右边的数据,右链接是用来显示右边有数据不能连接右边的数据。全连接是用来显示俩边不能连接的数据都显示出来。