-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-25348][SQL] Data source for binary files #24354
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
Changes from 1 commit
42d1fc9
373af0f
a7aed42
55a6858
c3d4411
b711773
dda654a
aab4dcd
2b1780f
46a07e3
dd8e8c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.spark.sql.execution.datasources.binaryfile | ||
|
|
||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * `binaryfile` package implements Spark SQL data source API for loading binary file data | ||
| * as `DataFrame`. | ||
| * | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also document how to control the input partition size. cc: @cloud-fan |
||
| * The loaded `DataFrame` has two columns, the schema is: | ||
| * - status: `StructType` (the file status information) | ||
| * - content: `BinaryType` (binary data of the file content) | ||
| * | ||
| * The schema of "status" column described above is: | ||
| * - path: `StringType` (the file path) | ||
| * - modification_time: `TimestampType` (last modification time of the file) | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| * - length: `LongType` (the file length) | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| */ | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| class BinaryFileDataSource private() {} | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
HyukjinKwon marked this conversation as resolved.
Outdated
|
||
|
|
||
| object BinaryFileDataSource { | ||
|
|
||
| val fileStatusSchema = StructType( | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| StructField("path", StringType, true) :: | ||
| StructField("modification_time", TimestampType, true) :: | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| StructField("length", LongType, true) :: Nil) | ||
|
|
||
| val binaryFileSchema = StructType( | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| StructField("status", fileStatusSchema, true) :: | ||
| StructField("content", BinaryType, true) :: Nil) | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * 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.spark.sql.execution.datasources.binaryfile | ||
|
|
||
| import java.sql.Timestamp | ||
|
|
||
| import com.google.common.io.{ByteStreams, Closeables} | ||
| import org.apache.hadoop.conf.Configuration | ||
| import org.apache.hadoop.fs.{FileStatus, GlobFilter, Path} | ||
| import org.apache.hadoop.mapreduce.Job | ||
|
|
||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.encoders.RowEncoder | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, UnsafeRow} | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.GenerateUnsafeProjection | ||
| import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap | ||
| import org.apache.spark.sql.execution.datasources.{DataSource, FileFormat, OutputWriterFactory, PartitionedFile} | ||
| import org.apache.spark.sql.sources.{DataSourceRegister, Filter} | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.util.SerializableConfiguration | ||
|
|
||
|
|
||
| private[binaryfile] class BinaryFileFormat extends FileFormat with DataSourceRegister { | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
HyukjinKwon marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per https://issues.apache.org/jira/browse/SPARK-16964, I think we can remove |
||
|
|
||
| override def inferSchema( | ||
| sparkSession: SparkSession, | ||
| options: Map[String, String], | ||
| files: Seq[FileStatus]): Option[StructType] = Some(BinaryFileDataSource.binaryFileSchema) | ||
|
|
||
| override def prepareWrite( | ||
| sparkSession: SparkSession, | ||
| job: Job, | ||
| options: Map[String, String], | ||
| dataSchema: StructType): OutputWriterFactory = { | ||
| throw new UnsupportedOperationException("Write is not supported for binary file data source") | ||
| } | ||
|
|
||
| override def shortName(): String = "binaryFile" | ||
|
WeichenXu123 marked this conversation as resolved.
HyukjinKwon marked this conversation as resolved.
|
||
|
|
||
| override protected def buildReader( | ||
| sparkSession: SparkSession, | ||
| dataSchema: StructType, | ||
| partitionSchema: StructType, | ||
| requiredSchema: StructType, | ||
| filters: Seq[Filter], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we going to leverage the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can put it in later PR. |
||
| options: Map[String, String], | ||
| hadoopConf: Configuration): (PartitionedFile) => Iterator[InternalRow] = { | ||
|
|
||
| val broadcastedHadoopConf = | ||
| sparkSession.sparkContext.broadcast(new SerializableConfiguration(hadoopConf)) | ||
|
|
||
| val binaryFileSourceOptions = new BinaryFileSourceOptions(options) | ||
|
|
||
| val pathFilterRegex = binaryFileSourceOptions.pathFilterRegex | ||
| val globFilter = if (pathFilterRegex.isEmpty) { null } else { | ||
| new GlobFilter(pathFilterRegex) | ||
| } | ||
|
|
||
| (file: PartitionedFile) => { | ||
| val path = file.filePath | ||
| val fsPath = new Path(path) | ||
|
|
||
| if (globFilter == null || globFilter.accept(fsPath)) { | ||
| val fs = fsPath.getFileSystem(broadcastedHadoopConf.value.value) | ||
| val fileStatus = fs.getFileStatus(fsPath) | ||
| val length = fileStatus.getLen() | ||
| val modificationTime = new Timestamp(fileStatus.getModificationTime()) | ||
| val stream = fs.open(fsPath) | ||
|
WeichenXu123 marked this conversation as resolved.
|
||
| val content = try { | ||
|
HyukjinKwon marked this conversation as resolved.
|
||
| ByteStreams.toByteArray(stream) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remember correctly, the usual behavior in Spark is not to throw an exception but prefers null value. At this point, should we assign
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, we can control it with |
||
| } finally { | ||
| Closeables.close(stream, true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to above comment, should we not propagate IO exceptions? |
||
| } | ||
|
|
||
| val converter = RowEncoder(dataSchema) | ||
| val fullOutput = dataSchema.map { f => | ||
| AttributeReference(f.name, f.dataType, f.nullable, f.metadata)() | ||
| } | ||
| val requiredOutput = fullOutput.filter { a => | ||
| requiredSchema.fieldNames.contains(a.name) | ||
| } | ||
|
|
||
| val requiredColumns = GenerateUnsafeProjection.generate(requiredOutput, fullOutput) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does not help the performance. We still read the file content even if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is OK for now, maybe we can leave a TODO and implement the real column pruning in the future. |
||
|
|
||
| val row = Row(Row(path, modificationTime, length), content) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the schema is simple, we can create string type should be |
||
|
|
||
| Iterator(requiredColumns(converter.toRow(row))) | ||
| } else { | ||
| Iterator.empty | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[binaryfile] class BinaryFileSourceOptions( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
| @transient private val parameters: CaseInsensitiveMap[String]) extends Serializable { | ||
|
|
||
| def this(parameters: Map[String, String]) = this(CaseInsensitiveMap(parameters)) | ||
|
|
||
| /** | ||
| * only include files with path matching the regex pattern. | ||
| */ | ||
| val pathFilterRegex = parameters.getOrElse("pathFilterRegex", "").toString | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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.spark.sql.execution.datasources.binaryfile | ||
|
|
||
| import java.sql.Timestamp | ||
|
|
||
| import com.google.common.io.{ByteStreams, Closeables} | ||
| import org.apache.hadoop.fs.Path | ||
|
|
||
| import org.apache.spark.sql.{QueryTest, Row} | ||
| import org.apache.spark.sql.functions.{col, substring_index} | ||
| import org.apache.spark.sql.test.{SharedSQLContext, SQLTestUtils} | ||
| import org.apache.spark.sql.types.LongType | ||
|
|
||
| class BinaryFileSuite extends QueryTest with SharedSQLContext with SQLTestUtils { | ||
| import testImplicits._ | ||
|
|
||
| private lazy val filePath = testFile("test-data/text-partitioned") | ||
|
|
||
| private lazy val fsFilePath = new Path(filePath) | ||
|
|
||
| private lazy val fs = fsFilePath.getFileSystem(sparkContext.hadoopConfiguration) | ||
|
|
||
| test("binary file test") { | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val resultDF = spark.read.format("binaryFile") | ||
| .load(filePath) | ||
| .select( | ||
| substring_index(col("status.path"), "/", -1).as("path"), | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| col("status.modification_time"), | ||
| col("status.length"), | ||
| col("content"), | ||
| col("year") | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| val expectedRowSet = new collection.mutable.HashSet[Row]() | ||
|
|
||
| for (partitionDirStatus <- fs.listStatus(fsFilePath)) { | ||
| val dirPath = partitionDirStatus.getPath | ||
|
|
||
| for (fileStatus <- fs.listStatus(dirPath)) { | ||
| val fname = fileStatus.getPath.getName | ||
| val flen = fileStatus.getLen | ||
| val modificationTime = new Timestamp(fileStatus.getModificationTime) | ||
|
|
||
| val fcontent = { | ||
| val stream = fs.open(fileStatus.getPath) | ||
| val content = try { | ||
| ByteStreams.toByteArray(stream) | ||
| } finally { | ||
| Closeables.close(stream, true) | ||
| } | ||
| content | ||
| } | ||
|
|
||
| val partitionName = dirPath.getName.split("=")(1) | ||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| val year = partitionName.toInt | ||
| val row = Row(fname, modificationTime, flen, fcontent, year) | ||
| expectedRowSet.add(row) | ||
| } | ||
| } | ||
|
|
||
| val result = resultDF.collect() | ||
| assert(Set(result: _*) === expectedRowSet.toSet) | ||
|
HyukjinKwon marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
|
WeichenXu123 marked this conversation as resolved.
Outdated
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.