You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/09/24 15:26:31 UTC

[GitHub] [beam] piotr-szuberski commented on a change in pull request #12838: [BEAM-10892] Add Proto support to Kafka Table Provider

piotr-szuberski commented on a change in pull request #12838:
URL: https://github.com/apache/beam/pull/12838#discussion_r494409925



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/kafka/BeamKafkaProtoTable.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.extensions.sql.meta.provider.kafka;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import org.apache.beam.sdk.coders.RowCoder;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+
+public class BeamKafkaProtoTable extends BeamKafkaTable {
+
+  public BeamKafkaProtoTable(Schema beamSchema, String bootstrapServers, List<String> topics) {
+    super(beamSchema, bootstrapServers, topics);
+  }
+
+  @Override
+  public PTransform<PCollection<KV<byte[], byte[]>>, PCollection<Row>> getPTransformForInput() {
+    return new ProtoRecorderDecoder(schema);
+  }
+
+  @Override
+  public PTransform<PCollection<Row>, PCollection<KV<byte[], byte[]>>> getPTransformForOutput() {
+    return new ProtoRecorderEncoder();
+  }
+
+  /** A PTransform to convert {@code KV<byte[], byte[]>} to {@link Row}. */
+  public static class ProtoRecorderDecoder
+      extends PTransform<PCollection<KV<byte[], byte[]>>, PCollection<Row>> {
+    private final Schema schema;
+
+    public ProtoRecorderDecoder(Schema schema) {
+      this.schema = schema;
+    }
+
+    @Override
+    public PCollection<Row> expand(PCollection<KV<byte[], byte[]>> input) {
+      return input
+          .apply(
+              "decodeProtoRecord",
+              ParDo.of(
+                  new DoFn<KV<byte[], byte[]>, Row>() {
+                    @ProcessElement
+                    public void processElement(ProcessContext c) {
+                      c.output(parseProtoPayloadToRow(c.element().getValue(), schema));
+                    }
+                  }))
+          .setRowSchema(schema);
+    }
+
+    static Row parseProtoPayloadToRow(byte[] payload, Schema payloadSchema) {
+      try {
+        InputStream inputStream = new ByteArrayInputStream(payload);
+        RowCoder rowCoder = RowCoder.of(payloadSchema);
+        return rowCoder.decode(inputStream);
+      } catch (IOException e) {
+        throw new IllegalArgumentException("Could not decode row from proto payload.", e);
+      }
+    }
+  }
+
+  /** A PTransform to convert {@link Row} to {@code KV<byte[], byte[]>}. */
+  public static class ProtoRecorderEncoder
+      extends PTransform<PCollection<Row>, PCollection<KV<byte[], byte[]>>> {
+
+    @Override
+    public PCollection<KV<byte[], byte[]>> expand(PCollection<Row> input) {
+      return input.apply(
+          "encodeProtoRecord",
+          ParDo.of(
+              new DoFn<Row, KV<byte[], byte[]>>() {
+                @ProcessElement
+                public void processElement(ProcessContext c) {
+                  Row in = c.element();
+                  c.output(KV.of(new byte[] {}, encodeRowToProtoBytes(in)));
+                }
+              }));
+    }
+
+    static byte[] encodeRowToProtoBytes(Row row) {
+      RowCoder rowCoder = RowCoder.of(row.getSchema());
+      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+      try {
+        rowCoder.encode(row, outputStream);

Review comment:
       Right, I've mistaken Row with RowProto. Thanks for the hints! I didn't know there is a universal coder for protos, I'll take a look.




----------------------------------------------------------------
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.

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