Flink-Shapeless replaces the default macro based implicit provider for
TypeInformation[T]
in Apache Flink's Scala API
with automatic type class derivation based on
Shapeless.
The primary use case of Flink-Shapeless is to enable custom implicit
TypeInformation
instances in scope to override the default.
// Import Flink's Scala API as usual
import org.apache.flink.api.scala._
// Replace the macro-based TypeInformation provider
import derived.auto._
// Override TypeInformation[String]
implicit val strTypeInfo = MyASCIIStringTypeInfo
// Strings below are serialized with ASCII encoding,
// even when nested in tuples, data structures, etc.
val env = ExecutionEnvironment.getExecutionEnvironment
val text = env.readTextFile("/path/to/file")
val counts = text
.flatMap(_.toLowerCase.split("\\W+"))
.filter(_.nonEmpty).map(_ -> 1)
.groupBy(0).sum(1)
There are a couple of advantages to automatic type class derivation over the default macro based approach.
Automatic derivation uses a modified version of the Scala implicit resolution mechanism with lowest priority. Thus it can be overridden for specific types by providing an implicit instance anywhere in scope, including in a companion object as idiomatic in Scala.
case class Foo(x: Int)
object Foo<