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 2021/03/05 16:58:08 UTC

[GitHub] [beam] TheNeuralBit commented on a change in pull request #13980: [BEAM-11659] Add new schema types to Pub/Sub SQL

TheNeuralBit commented on a change in pull request #13980:
URL: https://github.com/apache/beam/pull/13980#discussion_r588463206



##########
File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/NestedRowToMessage.java
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.gcp.pubsub;
+
+import static org.apache.beam.sdk.io.gcp.pubsub.PubsubMessageToRow.ATTRIBUTES_FIELD;
+import static org.apache.beam.sdk.io.gcp.pubsub.PubsubMessageToRow.PAYLOAD_FIELD;
+import static org.apache.beam.sdk.io.gcp.pubsub.PubsubSchemaIOProvider.ATTRIBUTE_ARRAY_FIELD_TYPE;
+import static org.apache.beam.sdk.io.gcp.pubsub.PubsubSchemaIOProvider.ATTRIBUTE_MAP_FIELD_TYPE;
+import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Collection;
+import javax.annotation.Nonnull;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.schemas.Schema.TypeName;
+import org.apache.beam.sdk.schemas.io.payloads.PayloadSerializer;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+class NestedRowToMessage extends SimpleFunction<Row, PubsubMessage> {
+  private static final long serialVersionUID = 65176815766314684L;
+
+  private final PayloadSerializer serializer;
+
+  NestedRowToMessage(PayloadSerializer serializer) {
+    this.serializer = serializer;
+  }
+
+  @Override
+  public PubsubMessage apply(Row row) {
+    Schema schema = row.getSchema();
+    ImmutableMap.Builder<String, String> attributes = ImmutableMap.builder();
+    if (schema.getField(ATTRIBUTES_FIELD).getType().equals(ATTRIBUTE_MAP_FIELD_TYPE)) {
+      attributes.putAll(checkArgumentNotNull(row.getMap(ATTRIBUTES_FIELD)));
+    } else {
+      checkArgument(schema.getField(ATTRIBUTES_FIELD).getType().equals(ATTRIBUTE_ARRAY_FIELD_TYPE));
+      Collection<Row> attributeEntries = checkArgumentNotNull(row.getArray(ATTRIBUTES_FIELD));
+      for (Row entry : attributeEntries) {
+        attributes.put(
+            checkArgumentNotNull(entry.getString("key")),
+            checkArgumentNotNull(entry.getString("value")));
+      }
+    }
+    @Nonnull byte[] payload;
+    if (schema.getField(PAYLOAD_FIELD).getType().equals(FieldType.BYTES)) {
+      payload = checkArgumentNotNull(row.getBytes(PAYLOAD_FIELD));
+    } else {
+      checkArgument(schema.getField(PAYLOAD_FIELD).getType().getTypeName().equals(TypeName.ROW));
+      payload = serializer.serialize(checkArgumentNotNull(row.getRow(PAYLOAD_FIELD)));
+    }
+    return new PubsubMessage(payload, attributes.build());
+  }

Review comment:
       nit: This will re-check the schema for every element, it would be better to select between these different code paths at construction time. You could do that by making this a `PTransform<Row, PubsubMessage>`, and selecting between the paths in `expand` by observing the schema on the input PCollection. Each option would just apply a different ParDo.

##########
File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubMessage.java
##########
@@ -31,51 +31,68 @@
   "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
 })
 public class PubsubMessage {
+  @AutoValue
+  abstract static class Impl {
+    @SuppressWarnings("mutable")
+    abstract byte[] getPayload();
 
-  private byte[] message;
-  private @Nullable Map<String, String> attributes;
-  private @Nullable String messageId;
+    abstract @Nullable Map<String, String> getAttributeMap();
+
+    abstract @Nullable String getMessageId();
+
+    static Impl create(
+        byte[] payload, @Nullable Map<String, String> attributes, @Nullable String messageId) {
+      return new AutoValue_PubsubMessage_Impl(payload, attributes, messageId);
+    }
+  }
+
+  private Impl impl;

Review comment:
       I'm curious what this change is for. Is it so you can leverage the AutoValue generated equals and hashCode?




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