[
https://issues.apache.org/jira/browse/BEAM-6063?focusedWorklogId=166974&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-166974
]
ASF GitHub Bot logged work on BEAM-6063:
----------------------------------------
Author: ASF GitHub Bot
Created on: 16/Nov/18 17:46
Start Date: 16/Nov/18 17:46
Worklog Time Spent: 10m
Work Description: aromanenko-dev commented on a change in pull request
#7052: [BEAM-6063] KafkaIO: add writing support with ProducerRecord
URL: https://github.com/apache/beam/pull/7052#discussion_r234292484
##########
File path:
sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ProducerRecordCoder.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.beam.sdk.io.kafka;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.IterableCoder;
+import org.apache.beam.sdk.coders.KvCoder;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.coders.StructuredCoder;
+import org.apache.beam.sdk.coders.VarIntCoder;
+import org.apache.beam.sdk.coders.VarLongCoder;
+import org.apache.beam.sdk.values.KV;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.header.Header;
+import org.apache.kafka.common.header.Headers;
+
+/** {@link Coder} for {@link ProducerRecord}. */
+public class ProducerRecordCoder<K, V> extends
StructuredCoder<ProducerRecord<K, V>> {
+ private static final StringUtf8Coder stringCoder = StringUtf8Coder.of();
+ private static final VarLongCoder longCoder = VarLongCoder.of();
+ private static final VarIntCoder intCoder = VarIntCoder.of();
+ private static final IterableCoder headerCoder =
+ IterableCoder.of(KvCoder.of(stringCoder, ByteArrayCoder.of()));
+
+ private final KvCoder<K, V> kvCoder;
+
+ public static <K, V> ProducerRecordCoder<K, V> of(Coder<K> keyCoder,
Coder<V> valueCoder) {
+ return new ProducerRecordCoder<>(keyCoder, valueCoder);
+ }
+
+ public ProducerRecordCoder(Coder<K> keyCoder, Coder<V> valueCoder) {
+ this.kvCoder = KvCoder.of(keyCoder, valueCoder);
+ }
+
+ @Override
+ public void encode(ProducerRecord<K, V> value, OutputStream outStream)
throws IOException {
+ stringCoder.encode(value.topic(), outStream);
+ intCoder.encode(value.partition() != null ? value.partition() :
Integer.MAX_VALUE, outStream);
Review comment:
Yes, I think "-1" should work for partition (but not for timestamp)
Is there some better way how to deal with null values in Coder? Does
`NullableCoder` can help here?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 166974)
Time Spent: 1h 50m (was: 1h 40m)
> KafkaIO: add writing support with ProducerRecord
> ------------------------------------------------
>
> Key: BEAM-6063
> URL: https://issues.apache.org/jira/browse/BEAM-6063
> Project: Beam
> Issue Type: Improvement
> Components: io-java-kafka
> Reporter: Alexey Romanenko
> Assignee: Alexey Romanenko
> Priority: Major
> Time Spent: 1h 50m
> Remaining Estimate: 0h
>
> Currently, the default input collection for {{KafkaIO.Write}} is
> {{PCollection<KV<K,V>>}}. To support writing of Kafka headers or different
> output Kafka topics, we need to change type of input collection to
> {{PCollection<ProducerRecord<K,V>>}}. Also, it will make sense to use
> {{ProducerRecord<K,V>}} instead of {{KV<K,V>}} internally in {{KafkaIO}} to
> keep all meta information.
> In the same time, we need to keep compatibility for old interface based on
> {{KV<K,V>}} but make it deprecated and totally move to
> {{ProducerRecord<K,V>}} later.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)