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 2022/03/16 01:30:00 UTC

[GitHub] [beam] robertwb commented on a change in pull request #17101: PayloadBuilder for ExternalPythonTransform

robertwb commented on a change in pull request #17101:
URL: https://github.com/apache/beam/pull/17101#discussion_r827542787



##########
File path: runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PayloadBuilder.java
##########
@@ -0,0 +1,79 @@
+package org.apache.beam.runners.core.construction;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.schemas.JavaFieldSchema;
+import org.apache.beam.sdk.schemas.NoSuchSchemaException;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.SchemaRegistry;
+import org.apache.beam.sdk.values.Row;
+
+
+// TODO: Move to sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/
+//  when https://github.com/apache/beam/pull/17035 is in.
+// TODO: Add unit tests.
+public class PayloadBuilder {
+
+  private Schema schema;
+  private static final SchemaRegistry SCHEMA_REGISTRY = SchemaRegistry.createDefault();
+  private List<Object> args;
+  private Map<String, Object> kwargs;
+
+  private PayloadBuilder(Schema schema) {
+    this.schema = schema;
+    args = new ArrayList<>();
+    kwargs = new HashMap<>();
+  }
+
+  static PayloadBuilder fromSchema(Schema schema) {
+    return new PayloadBuilder(schema);
+  }
+
+  static PayloadBuilder fromType(Class<?> type) {
+    try {
+      return fromSchema(SCHEMA_REGISTRY.getSchema(type));
+    } catch (NoSuchSchemaException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  static PayloadBuilder fromJavaPojo(Object pojo) {

Review comment:
       Ideally, one could just do 
   
   ```
     row = PayloadBuilder.fromJavaPojo(pojo)
   ```
   
   rather than
   
   ```
     row = PayloadBuilder.fromJavaPojo(pojo).withArg(pojo.someArg).withArg(pojo.anotherArg).buildArgs()
   ```
   
   (and making sure that `someArg` and `anotherArg` were properly ordered). 
   
   I think perhaps we should structure this in terms of what a Pipeline author should write, e.g.
   
   ```
   PythonTransform.named("apache_beam.some.Transform").withArgs("foo", 3, ...)
   PythonTransform.named("apache_beam.some.Transform")
       .withKeywordArg("stringArg", "value")
       .withKeywordArg("intArg", 100)
       .withKeywordArgs(mapOfStringToObjectInferringTypes)
       .withKeywordArgs(rowOrMaybeEvenRegisteredPojo);
   ```




-- 
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: github-unsubscribe@beam.apache.org

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