derrickaw commented on code in PR #34411:
URL: https://github.com/apache/beam/pull/34411#discussion_r2031414947


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/io/TFRecordReadSchemaTransformProvider.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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;
+
+import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
+
+import com.google.auto.service.AutoService;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Metrics;
+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.schemas.transforms.SchemaTransform;
+import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider;
+import org.apache.beam.sdk.schemas.transforms.providers.ErrorHandling;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.DoFn.ProcessElement;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionRowTuple;
+import org.apache.beam.sdk.values.PCollectionTuple;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TupleTag;
+import org.apache.beam.sdk.values.TupleTagList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@AutoService(SchemaTransformProvider.class)
+public class TFRecordReadSchemaTransformProvider
+    extends 
TypedSchemaTransformProvider<TFRecordReadSchemaTransformConfiguration> {
+  private static final String IDENTIFIER = 
"beam:schematransform:org.apache.beam:tfrecord_read:v1";
+  private static final String OUTPUT = "output";
+  private static final String ERROR = "errors";
+  public static final TupleTag<Row> OUTPUT_TAG = new TupleTag<Row>() {};
+  public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {};
+  private static final Logger LOG =
+      LoggerFactory.getLogger(TFRecordReadSchemaTransformProvider.class);
+
+  /** Returns the expected class of the configuration. */
+  @Override
+  protected Class<TFRecordReadSchemaTransformConfiguration> 
configurationClass() {
+    return TFRecordReadSchemaTransformConfiguration.class;
+  }
+
+  /** Returns the expected {@link SchemaTransform} of the configuration. */
+  @Override
+  protected SchemaTransform from(TFRecordReadSchemaTransformConfiguration 
configuration) {
+    return new TFRecordReadSchemaTransform(configuration);
+  }
+
+  /** Implementation of the {@link TypedSchemaTransformProvider} identifier 
method. */
+  @Override
+  public String identifier() {
+    return IDENTIFIER;
+  }
+
+  /**
+   * Implementation of the {@link TypedSchemaTransformProvider} 
inputCollectionNames method. Since
+   * no input is expected, this returns an empty list.
+   */
+  @Override
+  public List<String> inputCollectionNames() {
+    return Collections.emptyList();
+  }
+
+  /** Implementation of the {@link TypedSchemaTransformProvider} 
outputCollectionNames method. */
+  @Override
+  public List<String> outputCollectionNames() {
+    return Arrays.asList(OUTPUT, ERROR);
+  }
+
+  /**
+   * An implementation of {@link SchemaTransform} for TFRecord read jobs 
configured using {@link
+   * TFRecordReadSchemaTransformConfiguration}.
+   */
+  static class TFRecordReadSchemaTransform extends SchemaTransform {
+    private final TFRecordReadSchemaTransformConfiguration configuration;
+
+    TFRecordReadSchemaTransform(TFRecordReadSchemaTransformConfiguration 
configuration) {
+      this.configuration = configuration;
+    }
+
+    public Row getConfigurationRow() {
+      try {
+        // To stay consistent with our SchemaTransform configuration naming 
conventions,
+        // we sort lexicographically
+        return SchemaRegistry.createDefault()
+            .getToRowFunction(TFRecordReadSchemaTransformConfiguration.class)
+            .apply(configuration)
+            .sorted()
+            .toSnakeCase();
+      } catch (NoSuchSchemaException e) {
+        throw new RuntimeException(e);
+      }
+    }
+
+    @Override
+    public PCollectionRowTuple expand(PCollectionRowTuple input) {
+      // Validate configuration parameters
+      configuration.validate();
+
+      TFRecordIO.Read readTransform =
+          
TFRecordIO.read().withCompression(Compression.valueOf(configuration.getCompression()));
+
+      String filePattern = configuration.getFilePattern();
+      if (filePattern != null) {
+        readTransform = readTransform.from(filePattern);
+      }
+      if (!configuration.getValidate()) {
+        readTransform = readTransform.withoutValidation();
+      }
+
+      // Read TFRecord files into a PCollection of byte arrays.
+      PCollection<byte[]> tfRecordValues = 
input.getPipeline().apply(readTransform);
+
+      // Define the schema for the row
+      Schema schema = Schema.of(Schema.Field.of("record", 
Schema.FieldType.BYTES));

Review Comment:
   updated, thanks



-- 
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-unsubscr...@beam.apache.org

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

Reply via email to