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



##########
File path: 
sdks/java/extensions/python/src/main/java/org/apache/beam/sdk/extensions/python/ExternalPythonTransform.java
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.python;
+
+import java.util.Set;
+import java.util.UUID;
+import org.apache.beam.model.pipeline.v1.ExternalTransforms;
+import org.apache.beam.runners.core.construction.External;
+import org.apache.beam.sdk.coders.RowCoder;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.SchemaTranslation;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.values.PBegin;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionTuple;
+import org.apache.beam.sdk.values.PInput;
+import org.apache.beam.sdk.values.POutput;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.vendor.grpc.v1p43p2.com.google.common.collect.Iterables;
+import org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString;
+
+/** Wrapper for invoking external Python transforms. */
+public class ExternalPythonTransform<InputT extends PInput, OutputT extends 
POutput>
+    extends PTransform<InputT, OutputT> {
+  private final String fullyQualifiedName;
+  private final Row args;
+  private final Row kwargs;
+
+  public ExternalPythonTransform(String fullyQualifiedName, Row args, Row 
kwargs) {
+    this.fullyQualifiedName = fullyQualifiedName;
+    this.args = args;
+    this.kwargs = kwargs;
+  }
+
+  @Override
+  public OutputT expand(InputT input) {
+    int port;
+    try {
+      port = PythonService.findAvailablePort();
+      PythonService service =
+          new PythonService(
+              "apache_beam.runners.portability.expansion_service_main",
+              "--port",
+              "" + port,
+              "--fully_qualified_name_glob",
+              "*");
+      Schema payloadSchema =
+          Schema.of(
+              Schema.Field.of("constructor", Schema.FieldType.STRING),
+              Schema.Field.of("args", Schema.FieldType.row(args.getSchema())),
+              Schema.Field.of("kwargs", 
Schema.FieldType.row(kwargs.getSchema())));
+      payloadSchema.setUUID(UUID.randomUUID());

Review comment:
       Python has a schema cache (e.g. so it's not constantly re-creating types 
for a dynamic schema). Results in some very surprising behavior if this is not 
set. 




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to