-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-76] Add CSV Source support for Hudi Delta Streamer #1165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nsivabalan
merged 1 commit into
apache:master
from
yihua:HUDI-76-deltastreamer-csv-source
Mar 19, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
126 changes: 126 additions & 0 deletions
126
hudi-utilities/src/main/java/org/apache/hudi/utilities/sources/CsvDFSSource.java
This file contains hidden or 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,126 @@ | ||
| /* | ||
| * 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.hudi.utilities.sources; | ||
|
|
||
| import org.apache.hudi.common.util.Option; | ||
| import org.apache.hudi.common.util.TypedProperties; | ||
| import org.apache.hudi.common.util.collection.Pair; | ||
| import org.apache.hudi.utilities.schema.SchemaProvider; | ||
| import org.apache.hudi.utilities.sources.helpers.DFSPathSelector; | ||
|
|
||
| import org.apache.spark.api.java.JavaSparkContext; | ||
| import org.apache.spark.sql.DataFrameReader; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.SparkSession; | ||
| import org.apache.spark.sql.avro.SchemaConverters; | ||
| import org.apache.spark.sql.types.StructType; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Reads data from CSV files on DFS as the data source. | ||
| * | ||
| * Internally, we use Spark to read CSV files thus any limitation of Spark CSV also applies here | ||
| * (e.g., limited support for nested schema). | ||
| * | ||
| * You can set the CSV-specific configs in the format of hoodie.deltastreamer.csv.* | ||
| * that are Spark compatible to deal with CSV files in Hudi. The supported options are: | ||
| * | ||
| * "sep", "encoding", "quote", "escape", "charToEscapeQuoteEscaping", "comment", | ||
| * "header", "enforceSchema", "inferSchema", "samplingRatio", "ignoreLeadingWhiteSpace", | ||
| * "ignoreTrailingWhiteSpace", "nullValue", "emptyValue", "nanValue", "positiveInf", | ||
| * "negativeInf", "dateFormat", "timestampFormat", "maxColumns", "maxCharsPerColumn", | ||
| * "mode", "columnNameOfCorruptRecord", "multiLine" | ||
| * | ||
| * Detailed information of these CSV options can be found at: | ||
| * https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/DataFrameReader.html#csv-scala.collection.Seq- | ||
| * | ||
| * If the source Avro schema is provided through the {@link org.apache.hudi.utilities.schema.FilebasedSchemaProvider} | ||
| * using "hoodie.deltastreamer.schemaprovider.source.schema.file" config, the schema is | ||
| * passed to the CSV reader without inferring the schema from the CSV file. | ||
| */ | ||
| public class CsvDFSSource extends RowSource { | ||
| // CsvSource config prefix | ||
| public static final String CSV_SRC_CONFIG_PREFIX = "hoodie.deltastreamer.csv."; | ||
| // CSV-specific configurations to pass in from Hudi to Spark | ||
| public static final List<String> CSV_CONFIG_KEYS = Arrays.asList( | ||
| "sep", "encoding", "quote", "escape", "charToEscapeQuoteEscaping", "comment", | ||
| "header", "enforceSchema", "inferSchema", "samplingRatio", "ignoreLeadingWhiteSpace", | ||
| "ignoreTrailingWhiteSpace", "nullValue", "emptyValue", "nanValue", "positiveInf", | ||
| "negativeInf", "dateFormat", "timestampFormat", "maxColumns", "maxCharsPerColumn", | ||
| "mode", "columnNameOfCorruptRecord", "multiLine" | ||
| ); | ||
|
|
||
| private final DFSPathSelector pathSelector; | ||
| private final StructType sourceSchema; | ||
|
|
||
| public CsvDFSSource(TypedProperties props, | ||
| JavaSparkContext sparkContext, | ||
| SparkSession sparkSession, | ||
| SchemaProvider schemaProvider) { | ||
| super(props, sparkContext, sparkSession, schemaProvider); | ||
| this.pathSelector = new DFSPathSelector(props, sparkContext.hadoopConfiguration()); | ||
| if (schemaProvider != null) { | ||
| sourceSchema = (StructType) SchemaConverters.toSqlType(schemaProvider.getSourceSchema()) | ||
| .dataType(); | ||
| } else { | ||
| sourceSchema = null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected Pair<Option<Dataset<Row>>, String> fetchNextBatch(Option<String> lastCkptStr, | ||
| long sourceLimit) { | ||
| Pair<Option<String>, String> selPathsWithMaxModificationTime = | ||
| pathSelector.getNextFilePathsAndMaxModificationTime(lastCkptStr, sourceLimit); | ||
| return Pair.of(fromFiles( | ||
| selPathsWithMaxModificationTime.getLeft()), selPathsWithMaxModificationTime.getRight()); | ||
| } | ||
|
|
||
| /** | ||
| * Reads the CSV files and parsed the lines into {@link Dataset} of {@link Row}. | ||
| * | ||
| * @param pathStr The list of file paths, separated by ','. | ||
| * @return {@link Dataset} of {@link Row} containing the records. | ||
| */ | ||
| private Option<Dataset<Row>> fromFiles(Option<String> pathStr) { | ||
| if (pathStr.isPresent()) { | ||
| DataFrameReader dataFrameReader = sparkSession.read().format("csv"); | ||
| CSV_CONFIG_KEYS.forEach(optionKey -> { | ||
| String configPropName = CSV_SRC_CONFIG_PREFIX + optionKey; | ||
| String value = props.getString(configPropName, null); | ||
| // Pass down the Hudi CSV configs to Spark DataFrameReader | ||
| if (value != null) { | ||
| dataFrameReader.option(optionKey, value); | ||
| } | ||
| }); | ||
| if (sourceSchema != null) { | ||
| // Source schema is specified, pass it to the reader | ||
| dataFrameReader.schema(sourceSchema); | ||
| } | ||
| dataFrameReader.option("inferSchema", Boolean.toString(sourceSchema == null)); | ||
|
|
||
| return Option.of(dataFrameReader.load(pathStr.get().split(","))); | ||
| } else { | ||
| return Option.empty(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason for introducing this flattening?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for CSV format, the nested schema is not well supported. So to test CSV source, we need to generate the test CSV data with a flattened schema.