【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战005--DateSet实用API详解005
来源:互联网 发布:管家婆软件手机版 编辑:程序博客网 时间:2024/06/08 11:08
DateSet的API详解五
groupBy
def groupBy(firstField: String, otherFields: String*): GroupedDataSet[T]def groupBy(fields: Int*): GroupedDataSet[T]def groupBy[K](fun: (T) ⇒ K)(implicit arg0: TypeInformation[K]): GroupedDataSet[T]Creates a GroupedDataSet which provides operations on groups of elements.暗示第二个输入较小的交叉。拿第一个输入的每一个元素和第二个输入的每一个元素进行交叉操作。
groupBy示例一:使用一个Case Class Fields
执行程序:
//1.定义 classcase class WC(val word: String, val salary: Int) //2.定义DataSet[WC]val words: DataSet[WC] = benv.fromElements(WC("LISI",600),WC("LISI",400),WC("WANGWU",300),WC("ZHAOLIU",700))//3.使用自定义的reduce方法,使用key-expressions val wordCounts1 = words.groupBy("word").reduce { (w1, w2) => new WC(w1.word, w1.salary + w2.salary)}//4.使用自定义的reduce方法,使用key-selectorval wordCounts2 = words.groupBy { _.word } reduce { (w1, w2) => new WC(w1.word, w1.salary + w2.salary)}//5.显示结果wordCounts1.collectwordCounts2.collect
执行结果:
Scala-Flink> wordCounts1.collectres5: Seq[WC] = Buffer(WC(LISI,1000), WC(WANGWU,300), WC(ZHAOLIU,700))Scala-Flink> wordCounts1.collec2res6: Seq[WC] = Buffer(WC(LISI,1000), WC(WANGWU,300), WC(ZHAOLIU,700))
web ui中的执行效果:
groupBy示例二:使用多个Case Class Fields
执行程序:
//1.定义 case classcase class Student(val name: String, addr: String, salary: Double)//2.定义DataSet[Student]val tuples:DataSet[Student] = benv.fromElements(Student("lisi","shandong",2400.00),Student("zhangsan","henan",2600.00),Student("lisi","shandong",2700.00),Student("lisi","guangdong",2800.00))//3.使用自定义的reduce方法,使用多个Case Class Fields nameval reducedTuples1 = tuples.groupBy("name", "addr").reduce { (s1, s2) => Student(s1.name+"-"+s2.name,s1.addr+"-"+s2.addr,s1.salary+s2.salary)}//4.使用自定义的reduce方法,使用多个Case Class Fields indexval reducedTuples2 = tuples.groupBy(0, 1).reduce { (s1, s2) => Student(s1.name+"-"+s2.name,s1.addr+"-"+s2.addr,s1.salary+s2.salary)}//5.使用自定义的reduce方法,name和index混用val reducedTuples3 = tuples.groupBy(0, 1).reduce { (s1, s2) => Student(s1.name+"-"+s2.name,s1.addr+"-"+s2.addr,s1.salary+s2.salary)}//6.显示结果reducedTuples1.collectreducedTuples2.collectreducedTuples3.collect
执行结果:
Scala-Flink> reducedTuples1.collectres96: Seq[Student] = Buffer(Student(lisi,guangdong,2800.0),Student(lisi-lisi,shandong-shandong,5100.0), Student(zhangsan,henan,2600.0))Scala-Flink> reducedTuples2.collectres97: Seq[Student] = Buffer(Student(lisi,guangdong,2800.0),Student(lisi-lisi,shandong-shandong,5100.0), Student(zhangsan,henan,2600.0))Scala-Flink> reducedTuples3.collectres98: Seq[Student] = Buffer(Student(lisi,guangdong,2800.0),Student(lisi-lisi,shandong-shandong,5100.0), Student(zhangsan,henan,2600.0))
web ui中的执行效果:
阅读全文
0 0
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战005--DateSet实用API详解005
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战001--DateSet实用API详解001
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战002--DateSet实用API详解002
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战003--DateSet实用API详解003
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战004--DateSet实用API详解004
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战006--DateSet实用API详解006
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战007--DateSet实用API详解007
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战008--DateSet实用API详解008
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战009--DateSet实用API详解009
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战010--DateSet实用API详解010
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战011--DateSet实用API详解011
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战012--DateSet实用API详解012
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战013--DateSet实用API详解013
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战014--DateSet实用API详解014
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战015--DateSet实用API详解015
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战016--DateSet实用API详解016
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战017--DateSet实用API详解017
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战018--DateSet实用API详解018
- 包与访问权限
- Reverse Integer
- 为什么 qt 成为 c++ 界面编程的第一选择?
- 数据结构与算法(Java)-001-数学知识回顾
- 详解log4j2(下)
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战005--DateSet实用API详解005
- 神经网络模糊测试:将DNN应用于软件安全测试
- mysql 执行计划详解
- 3D打印切片软件Cura及CuraEngine原理分析
- 标准I/O库
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/MultiKeyMap
- 第一个只出现一次的字符
- 多线程--非阻塞同步,CAS 原理分析
- 学习嵌套结构