-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CsvIO] Create CsvIOParse Class (#32028)
* [CsvIO] Create CsvIOParse Class Co-authored-by: Francis O'Hara <[email protected]> * rough draft * [CsvIO] Create CsvIOParse Class Co-authored-by: Francis O'Hara <[email protected]> * Deleted changes made to CsvIOStringToRecord Class Co-authored-by: Francis O'Hara <[email protected]> * [CsvIO] update tests for CsvIO for more coverage Co-authored-by: Francis O'Hara <[email protected]> * added more tests for CsvIOParse Co-authored-by: Francis O'Hara <[email protected]> * Added documentation for CsvIOParse Co-authored-by: Francis O'Hara <[email protected]> --------- Co-authored-by: Francis O'Hara <[email protected]>
- Loading branch information
1 parent
21009e6
commit 6aac47c
Showing
6 changed files
with
756 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIOParse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.List; | ||
import java.util.Map; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
import org.apache.beam.sdk.transforms.Flatten; | ||
import org.apache.beam.sdk.transforms.PTransform; | ||
import org.apache.beam.sdk.transforms.SerializableFunction; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.PCollectionList; | ||
import org.apache.beam.sdk.values.PCollectionTuple; | ||
import org.apache.beam.sdk.values.TupleTag; | ||
|
||
/** | ||
* {@link PTransform} for Parsing CSV Record Strings into {@link Schema}-mapped target types. {@link | ||
* CsvIOParse} is not instantiated directly but via {@link CsvIO#parse} or {@link CsvIO#parseRows}. | ||
*/ | ||
@AutoValue | ||
public abstract class CsvIOParse<T> extends PTransform<PCollection<String>, CsvIOParseResult<T>> { | ||
|
||
final TupleTag<T> outputTag = new TupleTag<T>() {}; | ||
final TupleTag<CsvIOParseError> errorTag = new TupleTag<CsvIOParseError>() {}; | ||
|
||
static <T> CsvIOParse.Builder<T> builder() { | ||
return new AutoValue_CsvIOParse.Builder<>(); | ||
} | ||
|
||
// TODO(https://github.com/apache/beam/issues/31875): Implement in future PR. | ||
public CsvIOParse<T> withCustomRecordParsing( | ||
Map<String, SerializableFunction<String, Object>> customProcessingMap) { | ||
return this; | ||
} | ||
|
||
/** Contains all configuration parameters for {@link CsvIOParse}. */ | ||
abstract CsvIOParseConfiguration.Builder<T> getConfigBuilder(); | ||
|
||
@AutoValue.Builder | ||
abstract static class Builder<T> { | ||
abstract Builder<T> setConfigBuilder(CsvIOParseConfiguration.Builder<T> configBuilder); | ||
|
||
abstract CsvIOParse<T> build(); | ||
} | ||
|
||
@Override | ||
public CsvIOParseResult<T> expand(PCollection<String> input) { | ||
CsvIOParseConfiguration<T> configuration = getConfigBuilder().build(); | ||
|
||
CsvIOStringToCsvRecord stringToCsvRecord = | ||
new CsvIOStringToCsvRecord(configuration.getCsvFormat()); | ||
CsvIOParseResult<List<String>> stringToCsvRecordResult = input.apply(stringToCsvRecord); | ||
PCollection<List<String>> stringToRecordOutput = stringToCsvRecordResult.getOutput(); | ||
PCollection<CsvIOParseError> stringToRecordErrors = stringToCsvRecordResult.getErrors(); | ||
|
||
CsvIORecordToObjects<T> recordToObjects = new CsvIORecordToObjects<T>(configuration); | ||
CsvIOParseResult<T> recordToObjectsResult = stringToRecordOutput.apply(recordToObjects); | ||
PCollection<T> output = recordToObjectsResult.getOutput(); | ||
PCollection<CsvIOParseError> recordToObjectsErrors = recordToObjectsResult.getErrors(); | ||
|
||
PCollectionList<CsvIOParseError> errorList = | ||
PCollectionList.of(stringToRecordErrors).and(recordToObjectsErrors); | ||
PCollection<CsvIOParseError> errors = errorList.apply(Flatten.pCollections()); | ||
|
||
PCollectionTuple result = PCollectionTuple.of(outputTag, output).and(errorTag, errors); | ||
return CsvIOParseResult.of(outputTag, configuration.getCoder(), errorTag, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.