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/10/27 21:00:18 UTC

[GitHub] [beam] TheNeuralBit commented on a change in pull request #12780: [BEAM-5504] Add Avro support to Pubsub table provider

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



##########
File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubMessageToRow.java
##########
@@ -226,11 +260,11 @@ public void processElement(
                             field, timestamp, element.getAttributeMap(), payload))
                 .collect(toList());
         o.get(MAIN_TAG).output(Row.withSchema(messageSchema).addValues(values).build());
-      } catch (UnsupportedRowJsonException jsonException) {
+      } catch (UnsupportedRowJsonException | AvroRuntimeException exception) {

Review comment:
       I think it would be preferable to define our own `ParseException` that parsePayload throws. Then parsePayload can catch these exceptions specific to the format that is being used, and throw it with the format-specific exception as the cause. That way the calls to parsePayload just need to check for `ParseException`

##########
File path: sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/meta/provider/pubsub/PubsubAvroIT.java
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.pubsub;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasProperty;
+
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.utils.AvroUtils;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.hamcrest.Matcher;
+import org.joda.time.Instant;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Integration tests for querying Pubsub AVRO messages with SQL. */
+@RunWith(JUnit4.class)
+public class PubsubAvroIT extends PubsubTableProviderIT {
+  private static final Schema NAME_HEIGHT_KNOWS_JS_SCHEMA =
+      Schema.builder()
+          .addNullableField("name", Schema.FieldType.STRING)
+          .addNullableField("height", Schema.FieldType.INT32)
+          .addNullableField("knowsJavascript", Schema.FieldType.BOOLEAN)
+          .build();
+
+  private static final Schema NAME_HEIGHT_SCHEMA =
+      Schema.builder()
+          .addNullableField("name", Schema.FieldType.STRING)
+          .addNullableField("height", Schema.FieldType.INT32)
+          .build();
+
+  @Override
+  protected String getPayloadFormat() {
+    return "avro";
+  }
+
+  @Override
+  protected PCollection<String> applyRowsToStrings(PCollection<Row> rows) {
+    return rows.apply(
+        MapElements.into(TypeDescriptors.strings())
+            .via(
+                (Row row) ->
+                    new String(
+                        AvroUtils.getRowToAvroBytesFunction(row.getSchema()).apply(row), UTF_8)));
+  }
+
+  @Override
+  protected PubsubMessage messageIdName(Instant timestamp, int id, String name) {
+    Row row = row(PAYLOAD_SCHEMA, id, name);
+    return message(timestamp, AvroUtils.getRowToAvroBytesFunction(PAYLOAD_SCHEMA).apply(row));
+  }
+
+  @Override
+  protected Matcher<PubsubMessage> matcherNames(String name) {
+    Schema schema = Schema.builder().addStringField("name").build();
+    Row row = row(schema, name);
+    return hasProperty("payload", equalTo(AvroUtils.getRowToAvroBytesFunction(schema).apply(row)));
+  }
+
+  @Override
+  protected Matcher<PubsubMessage> matcherNameHeight(String name, int height) {
+    Row row = row(NAME_HEIGHT_SCHEMA, name, height);
+    return hasProperty(
+        "payload", equalTo(AvroUtils.getRowToAvroBytesFunction(NAME_HEIGHT_SCHEMA).apply(row)));
+  }
+
+  @Override
+  protected Matcher<PubsubMessage> matcherNameHeightKnowsJS(
+      String name, int height, boolean knowsJS) {
+    Row row = row(NAME_HEIGHT_KNOWS_JS_SCHEMA, name, height, knowsJS);
+    return hasProperty(
+        "payload",
+        equalTo(AvroUtils.getRowToAvroBytesFunction(NAME_HEIGHT_KNOWS_JS_SCHEMA).apply(row)));

Review comment:
       These functions should directly verify the Avro data rather than converting to a Row if possible

##########
File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PayloadFormat.java
##########
@@ -0,0 +1,23 @@
+/*
+ * 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;
+
+public enum PayloadFormat {
+  JSON,
+  AVRO
+}

Review comment:
       Can this be an inner class of `PubsubSchemaIOProvider`?




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