서드파티 시리얼라이저

서드파티 시리얼라이저 (3rd Party Serializers)

Flink 프로그램에서 Flink 타입 시리얼라이저가 직렬화할 수 없는 커스텀 타입을 사용하면, Flink는 일반(generic) Kryo 시리얼라이저로 폴백(fallback)해요. 자신만의 시리얼라이저 또는 Google Protobuf나 Apache Thrift 같은 직렬화 시스템을 Kryo에 등록할 수 있어요. 그러려면 pipeline.serialization-config 설정 옵션을 통해 타입 클래스와 시리얼라이저를 등록하기만 하면 돼요:

출처: 문서

본문

pipeline.serialization-config:
  - org.example.MyCustomType: {type: kryo, kryo-type: registered, class: org.example.MyCustomSerializer}

프로그래밍 방식으로도 다음과 같이 설정할 수 있어요:

Configuration config = new Configuration();

// register the class of the serializer as serializer for a type
config.set(PipelineOptions.SERIALIZATION_CONFIG,
    List.of("org.example.MyCustomType: {type: kryo, kryo-type: registered, class: org.example.MyCustomSerializer}"));

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(config);

커스텀 시리얼라이저는 Kryo의 Serializer 클래스를 상속해야 한다는 점에 주의하세요. Google Protobuf나 Apache Thrift의 경우에는 이미 그렇게 되어 있어요.

pipeline.serialization-config:
# register the Google Protobuf serializer with Kryo
  - org.example.MyCustomProtobufType: {type: kryo, kryo-type: registered, class: com.twitter.chill.protobuf.ProtobufSerializer}
# register the serializer included with Apache Thrift as the standard serializer
# TBaseSerializer states it should be initialized as a default Kryo serializer
  - org.example.MyCustomThriftType: {type: kryo, kryo-type: default, class: com.twitter.chill.thrift.TBaseSerializer}

위 예시가 동작하려면 Maven 프로젝트 파일(pom.xml)에 필요한 의존성을 포함해야 해요. dependency 섹션에 Apache Thrift에 대해 다음을 추가해요:

<dependency>
    <groupId>com.twitter</groupId>
    <artifactId>chill-thrift</artifactId>
    <version>0.7.6</version>
    <exclusions>
        <exclusion>
            <groupId>com.esotericsoftware.kryo</groupId>
            <artifactId>kryo</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.11.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Google Protobuf의 경우 다음 Maven 의존성이 필요해요:

<dependency>
    <groupId>com.twitter</groupId>
    <artifactId>chill-protobuf</artifactId>
    <version>0.7.6</version>
    <exclusions>
        <exclusion>
            <groupId>com.esotericsoftware.kryo</groupId>
            <artifactId>kryo</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.7.0</version>
</dependency>

두 라이브러리의 버전은 필요에 따라 조정해 주세요.

Kryo의 JavaSerializer 사용 시 발생하는 문제 (Issue with using Kryo's JavaSerializer)

커스텀 타입에 대해 Kryo의 JavaSerializer를 등록하면, 커스텀 타입 클래스가 제출된 사용자 코드 jar에 포함되어 있음에도 ClassNotFoundException이 발생할 수 있어요. 이것은 Kryo의 JavaSerializer가 잘못된 클래스로더를 사용할 수 있는 알려진 문제 때문이에요.

이 경우 org.apache.flink.api.java.typeutils.runtime.kryo.JavaSerializer를 대신 사용해 문제를 해결해야 해요. 이것은 Flink에서 재구현된 JavaSerializer로, 사용자 코드 클래스로더가 사용되도록 보장해요.

자세한 내용은 FLINK-6025를 참고하세요.

더 알아보기 (Learn more)