flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] fapaul commented on a change in pull request #16783: [FLINK-23640][connectors/kafka] Add KafkaRecordSerializationSchemaBuilder to cover common serialization scenarios
Date Tue, 17 Aug 2021 10:49:35 GMT

fapaul commented on a change in pull request #16783:
URL: https://github.com/apache/flink/pull/16783#discussion_r690254057



##########
File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/sink/KafkaRecordSerializationSchemaBuilder.java
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.streaming.connectors.kafka.sink;
+
+import org.apache.flink.api.common.serialization.SerializationSchema;
+import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkKafkaPartitioner;
+
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.Configurable;
+import org.apache.kafka.common.serialization.Serializer;
+
+import javax.annotation.Nullable;
+
+import java.util.Map;
+import java.util.OptionalInt;
+import java.util.function.Function;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * Builder to construct {@link KafkaRecordSerializationSchema}.
+ *
+ * <p>This class should give a first entrypoint when trying to serialize elements to
{@link
+ * ProducerRecord}. The following examples show some of the possibilities.
+ *
+ * <pre>Simple key-value serialization:
+ * {@code
+ * new KafkaRecordSerializationSchemaBuilder<String>("topic")
+ *     .withKeySerializationSchema(new SimpleStringSchema())
+ *     .withValueSerializationSchema(new SimpleStringSchema())
+ *     .build()
+ * }</pre>
+ *
+ * <pre>Using Kafka's serialization stack:
+ * {@code
+ * new KafkaRecordSerializationSchemaBuilder<String>("topic")
+ *     .withKeySerializer(StringSerializer.class)
+ *     .withValueSerializer(StringSerializer.class)
+ *     .build()
+ * }</pre>
+ *
+ * <pre>With custom partitioner:
+ * {@code
+ * new KafkaRecordSerializationSchemaBuilder<String>("topic")
+ *     .withPartitioner(MY_FLINK_PARTITIONER)
+ *     .withKeySerializationSchema(StringSerializer.class)
+ *     .build()
+ * }</pre>
+ *
+ * <p>The different serialization methods for key and value are mutually exclusive
thus i.e. it is
+ * not possible to use {@link #withKeySerializationSchema(SerializationSchema)} and {@link
+ * #withKeySerializer(Class)} on the same builder instance.
+ *
+ * <p>It is necessary to configure exactly one serialization method for the value.
+ *
+ * @param <IN> type of records to be serialized
+ * @see KafkaRecordSerializationSchema#builder(String)
+ */
+public class KafkaRecordSerializationSchemaBuilder<IN> {
+
+    @Nullable private final String topic;
+    @Nullable private final Function<IN, String> topicSelector;
+    @Nullable private FlinkKafkaPartitioner<IN> partitioner;
+    @Nullable private SerializationSchema<IN> keySerializationSchema;
+
+    private SerializationSchema<IN> valueSerializationSchema;
+
+    KafkaRecordSerializationSchemaBuilder(String topic) {
+        this.topic = checkNotNull(topic);
+        this.topicSelector = null;
+    }
+
+    KafkaRecordSerializationSchemaBuilder(Function<IN, String> topicSupplier) {
+        this.topicSelector = checkNotNull(topicSupplier);
+        this.topic = null;
+    }
+
+    /**
+     * Sets a custom partitioner determining the target partition of the target topic.
+     *
+     * @param partitioner
+     * @return {@link KafkaRecordSerializationSchemaBuilder}
+     */
+    public KafkaRecordSerializationSchemaBuilder<IN> withPartitioner(

Review comment:
       There is a `FlinkFixedPartitioner` in the Flink Kafka module. By default, Kafka decides
how to partition if the partitioner is null.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message