【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战014--DateSet实用API详解014

来源:互联网 发布:c语言读写ini配置文件 编辑:程序博客网 时间:2024/06/05 01:33

DateSet的API详解十四

Union

def union(other: DataSet[T]): DataSet[T]Creates a new DataSet containing the elements from both this DataSet and the other DataSet.合并多个DataSet。

执行程序:

//1.定义 case classcase class Student(val name: String, addr: String, salary: Double)//2.定义三个DataSet[Student]val tuples1 = benv.fromElements(Student("lisi-1","shandong",2400.00),Student("zhangsan-1","henan",2600.00))val tuples2 = benv.fromElements(Student("lisi-2","shandong",2400.00),Student("zhangsan-2","henan",2600.00))val tuples3 = benv.fromElements(Student("lisi-3","shandong",2400.00),Student("zhangsan-3","henan",2600.00))//3.将三个DataSet合并起来val unioned = tuples1.union(tuples2).union(tuples3)//4.显示结果unioned.collect

执行结果:

res113: Seq[Student] = Buffer(Student(lisi-1,shandong,2400.0), Student(zhangsan-1,henan,2600.0), Student(lisi-2,shandong,2400.0), Student(zhangsan-2,henan,2600.0), Student(lisi-3,shandong,2400.0), Student(zhangsan-3,henan,2600.0))

web ui中的执行效果:
这里写图片描述

first

def first(n: Int): DataSet[T]Creates a new DataSet containing the first n elements of this DataSet取前n个元素

执行程序:

//1.定义 case classcase class Student(val name: String, addr: String, salary: Double)//2.定义DataSet[Student]val in: DataSet[Student] = benv.fromElements(Student("lisi","shandong",2400.00),Student("zhangsan","hainan",2600.00),Student("wangwu","shandong",2400.00),Student("zhaoliu","hainan",2600.00),Student("xiaoqi","guangdong",2400.00),Student("xiaoba","henan",2600.00))//3.取前2个元素val out1 = in.first(2)out1.collect//3.取前2个元素 ???val out2 = in.groupBy(0).first(2)out2.collect//3.取前3个元素 ???val out3 = in.groupBy(0).sortGroup(1, Order.ASCENDING).first(3)out3.collect

执行结果:

Scala-Flink> out1.collectres126: Seq[Student] = Buffer(Student(lisi,shandong,2400.0), Student(zhangsan,hainan,2600.0))Scala-Flink> out2.collectres127: Seq[Student] = Buffer(Student(lisi,shandong,2400.0), Student(wangwu,shandong,2400.0), Student(xiaoba,henan,2600.0),Student(xiaoqi,guangdong,2400.0), Student(zhangsan,hainan,2600.0), Student(zhaoliu,hainan,2600.0))Scala-Flink> out3.collectres128: Seq[Student] = Buffer(Student(lisi,shandong,2400.0), Student(wangwu,shandong,2400.0), Student(xiaoba,henan,2600.0),Student(xiaoqi,guangdong,2400.0), Student(zhangsan,hainan,2600.0), Student(zhaoliu,hainan,2600.0))
阅读全文
0 0