imaffe commented on a change in pull request #17452: URL: https://github.com/apache/flink/pull/17452#discussion_r806705822
########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/writer/serializer/PulsarSerializationSchema.java ########## @@ -0,0 +1,161 @@ +/* + * 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.Message; +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.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, PulsarSinkContext)} 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 { + // Nothing to do by default. + } + + /** + * 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; + } + + /** + * Serializes given element into bytes. Property {@link TypedMessageBuilder#value(Object)}. + * + * @param element element to be serialized + * @param sinkContext context to provide extra information. + */ + byte[] serialize(IN element, PulsarSinkContext sinkContext); + + /** Setting message metadata which would rarely be used for normal users. */ + default void metadata(MetadataBuilder<IN> metadataBuilder, PulsarSinkContext sinkContext) { Review comment: Is it possible to pass `<IN> element` as an argument in the `metadata(...)` method? Maybe there will be use cases where users want to calculate metadata from the element. Adding element here might give users more "food" to cook XD. WDYT~ -- 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]
