diff --git a/sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIOParseConfiguration.java b/sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIOParseConfiguration.java new file mode 100644 index 000000000000..22f06edc8322 --- /dev/null +++ b/sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIOParseConfiguration.java @@ -0,0 +1,69 @@ +/* + * 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.csv; + +import com.google.auto.value.AutoValue; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.commons.csv.CSVFormat; + +/** Stores parameters needed for CSV record parsing. */ +@AutoValue +abstract class CsvIOParseConfiguration { + + static Builder builder() { + return new AutoValue_CsvIOParseConfiguration.Builder(); + } + + /** + * The expected CSVFormat + * of the parsed CSV record. + */ + abstract CSVFormat getCsvFormat(); + + /** The expected {@link Schema} of the target type. */ + abstract Schema getSchema(); + + /** A map of the {@link Schema.Field#getName()} to the custom CSV processing lambda. */ + abstract Map> getCustomProcessingMap(); + + @AutoValue.Builder + abstract static class Builder { + abstract Builder setCsvFormat(CSVFormat csvFormat); + + abstract Builder setSchema(Schema schema); + + abstract Builder setCustomProcessingMap( + Map> customProcessingMap); + + abstract Optional>> getCustomProcessingMap(); + + abstract CsvIOParseConfiguration autoBuild(); + + final CsvIOParseConfiguration build() { + if (!getCustomProcessingMap().isPresent()) { + setCustomProcessingMap(new HashMap<>()); + } + return autoBuild(); + } + } +} diff --git a/sdks/java/io/csv/src/test/java/org/apache/beam/sdk/io/csv/CsvIOParseConfigurationTest.java b/sdks/java/io/csv/src/test/java/org/apache/beam/sdk/io/csv/CsvIOParseConfigurationTest.java new file mode 100644 index 000000000000..96474d2dfd81 --- /dev/null +++ b/sdks/java/io/csv/src/test/java/org/apache/beam/sdk/io/csv/CsvIOParseConfigurationTest.java @@ -0,0 +1,29 @@ +/* + * 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.csv; + +import org.apache.beam.sdk.util.SerializableUtils; +import org.junit.Test; + +public class CsvIOParseConfigurationTest { + @Test + public void isSerializable() { + SerializableUtils.ensureSerializable(CsvIOParseConfiguration.class); + SerializableUtils.ensureSerializable(CsvIOParseConfiguration.Builder.class); + } +}