derrickaw commented on code in PR #34411: URL: https://github.com/apache/beam/pull/34411#discussion_r2035410804
########## sdks/java/core/src/main/java/org/apache/beam/sdk/io/TFRecordWriteSchemaTransformProvider.java: ########## @@ -0,0 +1,268 @@ +/* + * 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 com.google.auto.service.AutoService; +import java.util.Arrays; +import java.util.List; +import org.apache.beam.sdk.coders.ByteArrayCoder; +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.Element; +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 TFRecordWriteSchemaTransformProvider + extends TypedSchemaTransformProvider<TFRecordWriteSchemaTransformConfiguration> { + private static final String IDENTIFIER = "beam:schematransform:org.apache.beam:tfrecord_write:v1"; + private static final String INPUT = "input"; + private static final String OUTPUT = "output"; + private static final String ERROR = "errors"; + public static final TupleTag<byte[]> OUTPUT_TAG = new TupleTag<byte[]>() {}; + public static final TupleTag<Row> ERROR_TAG = new TupleTag<Row>() {}; + private static final Logger LOG = + LoggerFactory.getLogger(TFRecordWriteSchemaTransformProvider.class); + + /** Returns the expected class of the configuration. */ + @Override + protected Class<TFRecordWriteSchemaTransformConfiguration> configurationClass() { + return TFRecordWriteSchemaTransformConfiguration.class; + } + + /** Returns the expected {@link SchemaTransform} of the configuration. */ + @Override + protected SchemaTransform from(TFRecordWriteSchemaTransformConfiguration configuration) { + return new TFRecordWriteSchemaTransform(configuration); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} identifier method. */ + @Override + public String identifier() { + return IDENTIFIER; + } + + /** Implementation of the {@link TypedSchemaTransformProvider} inputCollectionNames method. */ + @Override + public List<String> inputCollectionNames() { + return Arrays.asList(INPUT, ERROR); + } + + /** Implementation of the {@link TypedSchemaTransformProvider} outputCollectionNames method. */ + @Override + public List<String> outputCollectionNames() { + return Arrays.asList(OUTPUT, ERROR); + } + + /** + * An implementation of {@link SchemaTransform} for TFRecord Write jobs configured using {@link + * TFRecordWriteSchemaTransformConfiguration}. + */ + static class TFRecordWriteSchemaTransform extends SchemaTransform { + private final TFRecordWriteSchemaTransformConfiguration configuration; + + TFRecordWriteSchemaTransform(TFRecordWriteSchemaTransformConfiguration configuration) { + this.configuration = configuration; + } + + public Row getConfigurationRow() { + try { + // To stay consistent with our SchemaTransform configuration naming conventions, + // we sort lexicographically + return SchemaRegistry.createDefault() + .getToRowFunction(TFRecordWriteSchemaTransformConfiguration.class) + .apply(configuration) + .sorted() + .toSnakeCase(); + } catch (NoSuchSchemaException e) { + throw new RuntimeException(e); + } + } + + @Override + public PCollectionRowTuple expand(PCollectionRowTuple input) { + // Validate configuration parameters + configuration.validate(); + + // Create basic transform + TFRecordIO.Write writeTransform = + TFRecordIO.write().withCompression(Compression.valueOf(configuration.getCompression())); + + // Add more parameters if not null + String outputPrefix = configuration.getOutputPrefix(); + if (outputPrefix != null) { Review Comment: Made required, 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