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

来源:互联网 发布:共有计价软件下载 编辑:程序博客网 时间:2024/06/08 13:08

zipWithIndex

def zipWithUniqueId: DataSet[(Long, T)]Method that assigns a unique id to all the elements of the input data set.元素和随机唯一的ID进行zip操作。

执行程序:

//1.创建一个 DataSet其元素为String类型val input: DataSet[String] = benv.fromElements("A", "B", "C", "D", "E", "F")//2.元素和随机唯一的ID进行zip操作。val result: DataSet[(Long, String)] = input.zipWithUniqueId//3.显示结果result.collect

执行结果:

res137: Seq[(Long, String)] = Buffer((0,A), (1,B), (2,C), (3,D), (4,E), (5,F))

flink web ui中的执行效果:

这里写图片描述

二、Flink DataSet 扩展API

注意:必须引入

import org.apache.flink.api.scala.extensions._

mapWith

def mapWith[R](fun: (T) ⇒ R)(implicit arg0: TypeInformation[R], arg1: ClassTag[R]):DataSet[R]Applies a function fun to each item of the data set可以使用偏函数进行map操作。

mapWith示例一:全函数

执行程序:

//1.引入增强依赖import org.apache.flink.api.scala.extensions._//2.创建DataSet[Point]case class Point(x: Double, y: Double)val ds = benv.fromElements(Point(1, 2), Point(3, 4), Point(5, 6))//3.使用mapWith进行元素转化val r=ds.mapWith {    case Point(x, y) => Point( x*2,y+1)}//4.显示结果r.collect

执行结果:

res156: Seq[Point] = Buffer(Point(2.0,3.0), Point(6.0,5.0), Point(10.0,7.0))

mapWith示例二:偏函数

执行程序:

//1.引入增强依赖import org.apache.flink.api.scala.extensions._//2.创建DataSet[Point]case class Point(x: Double, y: Double)val ds = benv.fromElements(Point(1, 2), Point(3, 4), Point(5, 6))//3.使用mapWith进行元素转化val r=ds.mapWith {    case Point(x, _) => x*2}//4.显示结果r.collect

执行结果:

res155: Seq[Double] = Buffer(2.0, 6.0, 10.0)
阅读全文
0 0