chitralverma commented on a change in pull request #555: [WIP] [GRIFFIN-297] Allow support for additional file based data sources URL: https://github.com/apache/griffin/pull/555#discussion_r347936994
########## File path: measure/src/main/scala/org/apache/griffin/measure/datasource/connector/batch/FileBasedDataConnector.scala ########## @@ -0,0 +1,207 @@ +/* +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.griffin.measure.datasource.connector.batch + +import scala.util.{Success, Try} + +import org.apache.spark.sql.{DataFrame, DataFrameReader, SparkSession} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.StructType + +import org.apache.griffin.measure.Loggable +import org.apache.griffin.measure.configuration.dqdefinition.DataConnectorParam +import org.apache.griffin.measure.context.TimeRange +import org.apache.griffin.measure.datasource.TimestampStorage +import org.apache.griffin.measure.utils.HdfsUtil +import org.apache.griffin.measure.utils.ParamUtil._ + +/** + * A batch data connector for file based sources which allows support various + * file based data sources like Parquet, CSV, TSV, ORC etc. + * Local files can also be read by prepending `file://` namespace. + * + * Currently supported formats like Parquet, ORC, AVRO, Text and Delimited types like CSV, TSV etc. + * + * Supported Configurations: + * - format : [[String]] specifying the type of file source (parquet, orc, etc.). + * - paths : [[Seq]] specifying the paths to be read + * - options : [[Map]] of format specific options + * - skipOnError : [[Boolean]] specifying where to continue execution if one or more paths are invalid. + * - schema : [[Seq]] of {colName, colType and isNullable} given as key value pairs. If provided, this can + * help skip the schema inference step for some underlying data sources. + * + * Some defaults assumed by this connector (if not set) are as follows: + * - `delimiter` is \t for TSV format, + * - `schema` is None, + * - `header` is false, + * - `format` is parquet + */ +case class FileBasedDataConnector(@transient sparkSession: SparkSession, + dcParam: DataConnectorParam, + timestampStorage: TimestampStorage) + extends BatchDataConnector { + + import FileBasedDataConnector._ + + val config: Map[String, Any] = dcParam.getConfig + var options: Map[String, String] = config.getParamStringMap(Options, Map.empty) + + var format: String = config.getString(Format, DefaultFormat).toLowerCase + val paths: Seq[String] = config.getStringArr(Paths, Nil) + val schemaSeq: Seq[Map[String, String]] = config.getAnyRef[Seq[Map[String, String]]](Schema, Nil) + val skipErrorPaths: Boolean = config.getBoolean(SkipErrorPaths, defValue = false) + + val currentSchema: Option[StructType] = Try(getUserDefinedSchema) match { + case Success(structType) if structType.fields.nonEmpty => Some(structType) + case _ => None + } + + assert(SupportedFormats.contains(format), + s"Invalid format '$format' specified. Must be one of ${SupportedFormats.mkString("['", "', '", "']")}") + + if (format == "csv") validateCSVOptions() + if (format == "tsv") { + format = "csv" + options = options + (Delimiter -> "\t") Review comment: Initial thought : Since TSV is a special case of CSV with delimiter as '\t', the delimter has been forcefully coded as tab ('\t'). If the delimter is anything else its no longer a TSV file and delimter can be provided with format as CSV. I'll make the change anyways. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
