Scala API Extensions

In order to keep a fair amount of consistency between the Scala and Java APIs, some of the features that allow a high-level of expressiveness in Scala have been left out from the standard APIs for both batch and streaming.

If you want to enjoy the full Scala experience you can choose to opt-in to extensions that enhance the Scala API via implicit conversions.

To use all the available extensions, you can just add a simple import for the DataSet API

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

or the DataStream API

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

Alternatively, you can import individual extensions a-là-carte to only use those you prefer.

Accept partial functions

Normally, both the DataSet and DataStream APIs don’t accept anonymous pattern matching functions to deconstruct tuples, case classes or collections, like the following:

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

This extension introduces new methods in both the DataSet and DataStream Scala API that have a one-to-one correspondence in the extended API. These delegating methods do support anonymous pattern matching functions.

DataSet API

MethodOriginalExample
mapWithmap (DataSet)
  1. data.mapWith {
  2. case (, value) => value.toString
  3. }
mapPartitionWithmapPartition (DataSet)
  1. data.mapPartitionWith {
  2. case head #:: => head
  3. }
flatMapWithflatMap (DataSet)
  1. data.flatMapWith {
  2. case (, name, visitTimes) => visitTimes.map(name -> )
  3. }
filterWithfilter (DataSet)
  1. data.filterWith {
  2. case Train(, isOnTime) => isOnTime
  3. }
reduceWithreduce (DataSet, GroupedDataSet)
  1. data.reduceWith {
  2. case ((, amount1), (, amount2)) => amount1 + amount2
  3. }
reduceGroupWithreduceGroup (GroupedDataSet)
  1. data.reduceGroupWith {
  2. case id #:: value #:: => id -> value
  3. }
groupingBygroupBy (DataSet)
  1. data.groupingBy {
  2. case (id, , ) => id
  3. }
sortGroupWithsortGroup (GroupedDataSet)
  1. grouped.sortGroupWith(Order.ASCENDING) {
  2. case House(, value) => value
  3. }
combineGroupWithcombineGroup (GroupedDataSet)
  1. grouped.combineGroupWith {
  2. case header #:: amounts => amounts.sum
  3. }
projectingapply (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. }
projectingapply (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

MethodOriginalExample
mapWithmap (DataStream)
  1. data.mapWith {
  2. case (, value) => value.toString
  3. }
flatMapWithflatMap (DataStream)
  1. data.flatMapWith {
  2. case (, name, visits) => visits.map(name -> )
  3. }
filterWithfilter (DataStream)
  1. data.filterWith {
  2. case Train(, isOnTime) => isOnTime
  3. }
keyingBykeyBy (DataStream)
  1. data.keyingBy {
  2. case (id, , ) => id
  3. }
mapWithmap (ConnectedDataStream)
  1. data.mapWith(
  2. map1 = case (, value) => value.toString,
  3. map2 = case (, , value, ) => value + 1
  4. )
flatMapWithflatMap (ConnectedDataStream)
  1. data.flatMapWith(
  2. flatMap1 = case (, json) => parse(json),
  3. flatMap2 = case (, , json, ) => parse(json)
  4. )
keyingBykeyBy (ConnectedDataStream)
  1. data.keyingBy(
  2. key1 = case (, timestamp) => timestamp,
  3. key2 = case (id, , ) => id
  4. )
reduceWithreduce (KeyedStream, WindowedStream)
  1. data.reduceWith {
  2. case ((, sum1), (, sum2) => sum1 + sum2
  3. }
foldWithfold (KeyedStream, WindowedStream)
  1. data.foldWith(User(bought = 0)) {
  2. case (User(b), (, items)) => User(b + items.size)
  3. }
applyWithapply (WindowedStream)
  1. data.applyWith(0)(
  2. foldFunction = case (sum, amount) => sum + amount
  3. windowFunction = case (k, w, sum) => // […]
  4. )
projectingapply (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. }

For more information on the semantics of each method, please refer to the DataSet and DataStream API documentation.

To use this extension exclusively, you can add the following import:

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

for the DataSet extensions and

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

The following snippet shows a minimal example of how to use these extension methods together (with the 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. }