If you use a custom type in your Flink program which cannot be serialized by the
Flink type serializer, Flink falls back to using the generic Kryo
serializer. You may register your own serializer or a serialization system like
Google Protobuf or Apache Thrift with Kryo. To do that, simply register the type
class and the serializer in the ExecutionConfig of your Flink program.
Note that your custom serializer has to extend Kryo’s Serializer class. In the
case of Google Protobuf or Apache Thrift, this has already been done for
For the above example to work, you need to include the necessary dependencies in
your Maven project file (pom.xml). In the dependency section, add the following
for Apache Thrift:
For Google Protobuf you need the following Maven dependency:
Please adjust the versions of both libraries as needed.
Issue with using Kryo’s JavaSerializer
If you register Kryo’s JavaSerializer for your custom type, you may
encounter ClassNotFoundExceptions even though your custom type class is
included in the submitted user code jar. This is due to a know issue with
Kryo’s JavaSerializer, which may incorrectly use the wrong classloader.
In this case, you should use org.apache.flink.api.java.typeutils.runtime.kryo.JavaSerializer
instead to resolve the issue. This is a reimplemented JavaSerializer in Flink
that makes sure the user code classloader is used.