syhily commented on a change in pull request #17452: URL: https://github.com/apache/flink/pull/17452#discussion_r805870452
########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/writer/serializer/PulsarSerializationSchema.java ########## @@ -0,0 +1,184 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.pulsar.sink.writer.serializer; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.apache.flink.api.common.serialization.SerializationSchema.InitializationContext; +import org.apache.flink.connector.pulsar.common.schema.PulsarSchema; +import org.apache.flink.connector.pulsar.sink.config.SinkConfiguration; +import org.apache.flink.connector.pulsar.sink.writer.context.PulsarSinkContext; + +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.TypedMessageBuilder; +import org.apache.pulsar.common.schema.KeyValue; + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * The serialization schema for how to serialize record into Pulsar. + * + * @param <IN> The message type send to Pulsar. + */ +@PublicEvolving +public interface PulsarSerializationSchema<IN> extends Serializable { + + /** + * Initialization method for the schema. It is called before the actual working methods {@link + * #serialize(Object)} and thus suitable for one time setup work. + * + * <p>The provided {@link InitializationContext} can be used to access additional features such + * as e.g. registering user metrics. + * + * @param initializationContext Contextual information that can be used during initialization. + * @param sinkContext runtime information i.e. partitions, subtaskId + * @param sinkConfiguration All the configure options for Pulsar sink. You can add custom + * options. + */ + default void open( + InitializationContext initializationContext, + PulsarSinkContext sinkContext, + SinkConfiguration sinkConfiguration) + throws Exception {} + + /** + * Serializes given element into bytes. Property {@link TypedMessageBuilder#value(Object)}. + * + * @param element element to be serialized + */ + byte[] serialize(IN element); + + /** + * Property {@link TypedMessageBuilder#orderingKey(byte[])}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default byte[] orderingKey(IN element, PulsarSinkContext sinkContext) { + return new byte[0]; + } + + /** + * Property {@link TypedMessageBuilder#key(String)}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default String key(IN element, PulsarSinkContext sinkContext) { + return null; + } + + /** + * Property {@link TypedMessageBuilder#properties(Map)}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default Map<String, String> properties(IN element, PulsarSinkContext sinkContext) { + return Collections.emptyMap(); + } + + /** + * Property {@link TypedMessageBuilder#eventTime(long)}. This value could be nullable if the + * element don't have a timestamp. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default Long eventTime(IN element, PulsarSinkContext sinkContext) { + return sinkContext.timestamp(); + } + + /** + * Property {@link TypedMessageBuilder#replicationClusters(List)}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default List<String> replicationClusters(IN element, PulsarSinkContext sinkContext) { + return Collections.emptyList(); + } + + /** + * Property {@link TypedMessageBuilder#disableReplication()}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + default boolean disableReplication(IN element, PulsarSinkContext sinkContext) { + return false; + } + + /** @return The related Pulsar Schema for this serializer. */ + default Schema<IN> schema() { + throw new UnsupportedOperationException( + "Implement this method if you need Pulsar schema evolution."); + } + + /** + * Create a PulsarSerializationSchema by using the flink's {@link SerializationSchema}. It would + * consume the pulsar message as byte array and decode the message by using flink's logic. + */ + static <T> PulsarSerializationSchema<T> flinkSchema( Review comment: We just use this naming for keeping it consistent with the naming in `PulsarDeserializationSchema`. Users who are familiar with the Pulsar source can easily find what they need. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
