-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-18658][SQL] Write text records directly to a FileOutputStream #16089
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 all commits
56667bd
373d8e0
d349d71
c178dac
7742ee4
5707218
27c102d
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 |
|---|---|---|
|
|
@@ -194,4 +194,8 @@ private[sql] class JacksonGenerator( | |
| writeFields(row, schema, rootFieldWriters) | ||
| } | ||
| } | ||
|
|
||
| def writeLineEnding(): Unit = { | ||
| gen.writeRaw('\n') | ||
|
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.
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. 7-bit ASCII is a subset of UTF-8,
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. Presumably just a convoluted way to create a byte array containing the byte
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. That is my assumption. I'm also assuming that writing a single byte is slightly more efficient than writing an array of a single byte. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import java.io.{OutputStream, OutputStreamWriter} | ||
| import java.nio.charset.{Charset, StandardCharsets} | ||
|
|
||
| import org.apache.hadoop.fs.Path | ||
| import org.apache.hadoop.io.compress._ | ||
| import org.apache.hadoop.mapreduce.JobContext | ||
| import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat | ||
| import org.apache.hadoop.util.ReflectionUtils | ||
|
|
||
| object CodecStreams { | ||
| private def getCompressionCodec( | ||
| context: JobContext, | ||
| file: Option[Path] = None): Option[CompressionCodec] = { | ||
| if (FileOutputFormat.getCompressOutput(context)) { | ||
| val compressorClass = FileOutputFormat.getOutputCompressorClass( | ||
| context, | ||
| classOf[GzipCodec]) | ||
|
|
||
| Some(ReflectionUtils.newInstance(compressorClass, context.getConfiguration)) | ||
| } else { | ||
| file.flatMap { path => | ||
| val compressionCodecs = new CompressionCodecFactory(context.getConfiguration) | ||
| Option(compressionCodecs.getCodec(path)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a new file and open it for writing. | ||
| * If compression is enabled in the [[JobContext]] the stream will write compressed data to disk. | ||
| * An exception will be thrown if the file already exists. | ||
|
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. will "probably" be thrown; object stores have issues there
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. Is this a problem with Hadoop in general? The FileSystem docs also specify this behavior: |
||
| */ | ||
| def createOutputStream(context: JobContext, file: Path): OutputStream = { | ||
| val fs = file.getFileSystem(context.getConfiguration) | ||
| val outputStream: OutputStream = fs.create(file, false) | ||
|
|
||
| getCompressionCodec(context, Some(file)) | ||
| .map(codec => codec.createOutputStream(outputStream)) | ||
| .getOrElse(outputStream) | ||
| } | ||
|
|
||
| def createOutputStreamWriter( | ||
| context: JobContext, | ||
| file: Path, | ||
| charset: Charset = StandardCharsets.UTF_8): OutputStreamWriter = { | ||
| new OutputStreamWriter(createOutputStream(context, file), charset) | ||
| } | ||
|
|
||
| /** Returns the compression codec extension to be used in a file name, e.g. ".gzip"). */ | ||
| def getCompressionExtension(context: JobContext): String = { | ||
| getCompressionCodec(context) | ||
| .map(_.getDefaultExtension) | ||
| .getOrElse("") | ||
| } | ||
| } | ||
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.
always good to have tests for the corner case codepaths here, as they are invariably the official home of off-by-one errors
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.
Agreed.
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.
I've added a few tests for this method.