译者:flink.sojb.cn

为了在Scala和Java API之间保持相当大的一致性,在批处理和流式传输的标准API中省略了一些允许Scala高级表达性的函数。

如果您想享受完整的Scala体验,可以选择选择关联通过隐式转换增强Scala API的扩展。

要使用所有可用的扩展,您只需import为DataSet API 添加一个简单的扩展

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

或DataStream API

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

或者,您可以导入单个扩展名a-là-carte以仅使用您喜欢的扩展名。

接受部分函数

通常,DataSet和DataStream API都不接受匿名模式匹配函数来解构元组,案例类或集合,如下所示:

  1. val data: DataSet[(Int, String, Double)] = // [...] data.map {
  2. case (id, name, temperature) => // [...]
  3. // The previous line causes the following compilation error:
  4. // "The argument types of an anonymous function must be fully known. (SLS 8.5)" }

此扩展在DataSet和DataStream Scala API中引入了新方法,这些方法在扩展API中具有一对一的对应关系。这些委托方法确实支持匿名模式匹配函数。

DataSet API


方法:mapWith

原生:map(DataSet)

DEMO:

  1. data.mapWith {
  2. case (_, value) => value.toString
  3. }

方法:mapPartitionWith

原生:mapPartition(DataSet)

DEMO:

  1. data.mapPartitionWith {
  2. case head #:: _ => head
  3. }

方法:flatMapWith

原生:flatMap(DataSet)

DEMO:

  1. data.flatMapWith {
  2. case (_, name, visitTimes) => visitTimes.map(name -> _)
  3. }

方法:filterWith

原生:filter(DataSet)

DEMO:

  1. data.filterWith {
  2. case Train(_, isOnTime) => isOnTime
  3. }

方法:reduceWith

原生:reduce(DataSet,GroupedDataSet)

DEMO:

  1. data.reduceWith {
  2. case ((_, amount1), (_, amount2)) => amount1 + amount2
  3. }

方法:reduceGroupWith

原生:reduceGroup(GroupedDataSet)

DEMO:

  1. data.reduceGroupWith {
  2. case id #:: value #:: _ => id -> value
  3. }

方法:groupingBy

原生:groupBy(DataSet)

DEMO:

  1. data.groupingBy {
  2. case (id, _, _) => id
  3. }

方法:sortGroupWith

原生:sortGroup(GroupedDataSet)

DEMO:

  1. grouped.sortGroupWith(Order.ASCENDING) {
  2. case House(_, value) => value
  3. }

方法:combineGroupWith

原生:combineGroup(GroupedDataSet)

DEMO:

  1. grouped.combineGroupWith {
  2. case header #:: amounts => amounts.sum
  3. }

方法:projecting

原生:apply(JoinDataSet,CrossDataSet)

DEMO:

  1. data1.join(data2).
  2. whereClause(case (pk, _) => pk).
  3. isEqualTo(case (_, fk) => fk).
  4. projecting {
  5. case ((pk, tx), (products, fk)) => tx -> products
  6. }
  7. data1.cross(data2).projecting {
  8. case ((a, _), (_, b) => a -> b
  9. }

方法:projecting

原生:apply(CoGroupDataSet)

DEMO:

  1. data1.coGroup(data2).
  2. whereClause(case (pk, _) => pk).
  3. isEqualTo(case (_, fk) => fk).
  4. projecting {
  5. case (head1 #:: _, head2 #:: _) => head1 -> head2
  6. }
  7. }

DataStream API


方法:mapWith

原生:map(DataStream)

DEMO:

  1. data.mapWith {
  2. case (_, value) => value.toString
  3. }

方法:mapPartitionWith

原生:mapPartition(DataStream)

DEMO:

  1. data.mapPartitionWith {
  2. case head #:: _ => head
  3. }

方法:flatMapWith

原生:flatMap(DataStream)

DEMO:

  1. data.flatMapWith {
  2. case (_, name, visits) => visits.map(name -> _)
  3. }

方法:filterWith

原生:filter(DataStream)

DEMO:

  1. data.filterWith {
  2. case Train(_, isOnTime) => isOnTime
  3. }

方法:keyingBy

原生:keyBy(DataStream)

DEMO:

  1. data.keyingBy {
  2. case (id, _, _) => id
  3. }

方法:mapWith

原生:map(ConnectedDataStream)

DEMO:

  1. data.mapWith(
  2. map1 = case (_, value) => value.toString,
  3. map2 = case (_, _, value, _) => value + 1
  4. )

方法:flatMapWith

原生:flatMap(ConnectedDataStream)

DEMO:

  1. data.flatMapWith(
  2. flatMap1 = case (_, json) => parse(json),
  3. flatMap2 = case (_, _, json, _) => parse(json)
  4. )

方法:keyingBy

原生:keyBy(ConnectedDataStream)

DEMO:

  1. data.keyingBy(
  2. key1 = case (_, timestamp) => timestamp,
  3. key2 = case (id, _, _) => id
  4. )

方法:reduceWith

原生:reduce(KeyedStream,WindowedStream)

DEMO:

  1. data.reduceWith {
  2. case ((_, sum1), (_, sum2) => sum1 + sum2
  3. }

方法:foldWith

原生:fold(KeyedStream,WindowedStream)

DEMO:

  1. data.foldWith(User(bought = 0)) {
  2. case (User(b), (_, items)) => User(b + items.size)
  3. }

方法:applyWith

原生:apply(WindowedStream)

DEMO:

  1. data.applyWith(0)(
  2. foldFunction = case (sum, amount) => sum + amount
  3. windowFunction = case (k, w, sum) => // [...] )

方法:projecting

原生:apply(JoinedStream)

DEMO:

  1. data1.join(data2).
  2. whereClause(case (pk, _) => pk).
  3. isEqualTo(case (_, fk) => fk).
  4. projecting {
  5. case ((pk, tx), (products, fk)) => tx -> products
  6. }

有关每种方法的语义的更多信息,请参阅 DataSetDataStream API文档。

要仅使用此扩展程序,您可以添加以下内容import

  1. import org.apache.flink.api.scala.extensions.acceptPartialFunctions

对于DataSet扩展和

  1. import org.apache.flink.streaming.api.scala.extensions.acceptPartialFunctions

以下代码段显示了如何一起使用这些扩展方法的最小示例(使用DataSet API):

  1. object Main {
  2. import org.apache.flink.api.scala.extensions._
  3. case class Point(x: Double, y: Double)
  4. def main(args: Array[String]): Unit = {
  5. val env = ExecutionEnvironment.getExecutionEnvironment
  6. val ds = env.fromElements(Point(1, 2), Point(3, 4), Point(5, 6))
  7. ds.filterWith {
  8. case Point(x, _) => x > 1
  9. }.reduceWith {
  10. case (Point(x1, y1), (Point(x2, y2))) => Point(x1 + y1, x2 + y2)
  11. }.mapWith {
  12. case Point(x, y) => (x, y)
  13. }.flatMapWith {
  14. case (x, y) => Seq("x" -> x, "y" -> y)
  15. }.groupingBy {
  16. case (id, value) => id
  17. }
  18. }
  19. }