public class CoGroupDataSetTest {
public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
//注意:可啓用這行代碼看區別
//env.setParallelism(1);
DataSet<Tuple2<Long, String>> source1 = env.fromElements(
Tuple2.of(1L, "xiaoming"),
Tuple2.of(2L, "xiaowang"));
DataSet<Tuple2<Long, String>> source2 = env.fromElements(
Tuple2.of(2L, "xiaoli"),
Tuple2.of(1L, "shinelon"),
Tuple2.of(2L, "xiaohong"),
Tuple2.of(3L, "hhhhhh"));
source2.sortPartition(0, Order.ASCENDING).print();
//(1,shinelon)
//(2,xiaoli)
//(2,xiaohong)
//(3,hhhhhh)
System.out.println("------");
//先按第一個字段排升序,再按第二個字段排升序,= order by c1,c2 ;默認asc;
source2.sortPartition(0, Order.ASCENDING).sortPartition(1, Order.ASCENDING).print();
//(1,shinelon)
//(2,xiaohong)
//(2,xiaoli)
//(3,hhhhhh)
System.out.println("------");
//取前2個元素
source2.first(2).print();
//(2,xiaoli)
//(1,shinelon)
System.out.println("------");
source2.groupBy(1).sortGroup(1, Order.ASCENDING).first(2).print();
//(3,hhhhhh)
//(1,shinelon)
//(2,xiaohong)
//(2,xiaoli)
System.out.println("------");
source2.groupBy(0).sortGroup(0, Order.ASCENDING).first(2).print();
//(1,shinelon)
//(2,xiaoli)
//(2,xiaohong)
//(3,hhhhhh)
System.out.println("------");
source2.groupBy(0).sortGroup(0, Order.ASCENDING).first(1).print();
//默認12個並行度
//(3,hhhhhh)
//(1,shinelon)
//(2,xiaoli)
//1個並行度
//(1,shinelon)
//(2,xiaoli)
//(3,hhhhhh)
System.out.println("------");
//按第一個字段分組,每個組內按第二個字段升序排序,每個組取都第一條記錄
source2.groupBy(0).sortGroup(1, Order.ASCENDING).first(1).print();
//默認12個並行度(線程),局部有序,相同key的元素放在同一個線程下運行。
//(3,hhhhhh)
//(1,shinelon)
//(2,xiaohong)
//1個並行度,全局有序,
//(1,shinelon)
//(2,xiaohong)
//(3,hhhhhh)
}
}
flink 1.9.2,java1.8