Scala API 扩展

为了在Scala和JavaAPI之间保持一定的一致性,在批处理和流的标准API中忽略了一些在Scala中允许高表达性的特性。

如果要 享受完整的Scala体验,您可以选择选择-intoExtensions(通过隐式转换来增强ScalaAPI)。

要使用所有可用的扩展,您可以为数据集API添加一个简单的import

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

或Datastream API

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

或者,您可以导入单独的Extensions a-là-carte 仅使用您更喜欢的那些。

接受部分函数

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

  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和DataS treamScalaAPI中都引入了新的方法,这些方法在扩展API中具有一对一的对应关系。这些委托方法确实支持匿名模式匹配函数。

DataSet API

Method Original Example
mapWith map (DataSet)
  1. data.mapWith {
  2. case (_, value) => value.toString
  3. }

| | mapPartitionWith | mapPartition (DataSet) |

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

| | flatMapWith | flatMap (DataSet) |

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

| | filterWith | filter (DataSet) |

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

| | reduceWith | reduce (DataSet, GroupedDataSet) |

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

| | reduceGroupWith | reduceGroup (GroupedDataSet) |

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

| | groupingBy | groupBy (DataSet) |

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

| | sortGroupWith | sortGroup (GroupedDataSet) |

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

| | combineGroupWith | combineGroup (GroupedDataSet) |

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

| | projecting | apply (JoinDataSet, CrossDataSet) |

  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) |

  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

Method Original Example
mapWith map (DataStream)
  1. data.mapWith {
  2. case (_, value) => value.toString
  3. }

| | mapPartitionWith | mapPartition (DataStream) |

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

| | flatMapWith | flatMap (DataStream) |

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

| | filterWith | filter (DataStream) |

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

| | keyingBy | keyBy (DataStream) |

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

| | mapWith | map (ConnectedDataStream) |

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

| | flatMapWith | flatMap (ConnectedDataStream) |

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

| | keyingBy | keyBy (ConnectedDataStream) |

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

| | reduceWith | reduce (KeyedStream, WindowedStream) |

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

| | foldWith | fold (KeyedStream, WindowedStream) |

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

| | applyWith | apply (WindowedStream) |

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

| | projecting | apply (JoinedStream) |

  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. }

|

有关每个方法的语义的详细信息,请参阅数据集数据流API文档。

要专门使用此扩展名,您可以添加以下import

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

对于数据集扩展,

  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. }