Register a custom serializer for your Flink program

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.

  1. final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  2. // register the class of the serializer as serializer for a type
  3. env.getConfig().registerTypeWithKryoSerializer(MyCustomType.class, MyCustomSerializer.class);
  4. // register an instance as serializer for a type
  5. MySerializer mySerializer = new MySerializer();
  6. env.getConfig().registerTypeWithKryoSerializer(MyCustomType.class, mySerializer);

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 you:

  1. final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
  2. // register the Google Protobuf serializer with Kryo
  3. env.getConfig().registerTypeWithKryoSerializer(MyCustomType.class, ProtobufSerializer.class);
  4. // register the serializer included with Apache Thrift as the standard serializer
  5. // TBaseSerializer states it should be initialized as a default Kryo serializer
  6. env.getConfig().addDefaultKryoSerializer(MyCustomType.class, TBaseSerializer.class);

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:

  1. <dependency>
  2. <groupId>com.twitter</groupId>
  3. <artifactId>chill-thrift</artifactId>
  4. <version>0.7.6</version>
  5. <!-- exclusions for dependency conversion -->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>com.esotericsoftware.kryo</groupId>
  9. <artifactId>kryo</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <!-- libthrift is required by chill-thrift -->
  14. <dependency>
  15. <groupId>org.apache.thrift</groupId>
  16. <artifactId>libthrift</artifactId>
  17. <version>0.11.0</version>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>javax.servlet</groupId>
  21. <artifactId>servlet-api</artifactId>
  22. </exclusion>
  23. <exclusion>
  24. <groupId>org.apache.httpcomponents</groupId>
  25. <artifactId>httpclient</artifactId>
  26. </exclusion>
  27. </exclusions>
  28. </dependency>

For Google Protobuf you need the following Maven dependency:

  1. <dependency>
  2. <groupId>com.twitter</groupId>
  3. <artifactId>chill-protobuf</artifactId>
  4. <version>0.7.6</version>
  5. <!-- exclusions for dependency conversion -->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>com.esotericsoftware.kryo</groupId>
  9. <artifactId>kryo</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <!-- We need protobuf for chill-protobuf -->
  14. <dependency>
  15. <groupId>com.google.protobuf</groupId>
  16. <artifactId>protobuf-java</artifactId>
  17. <version>3.7.0</version>
  18. </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.

Please refer to FLINK-6025 for more details.