-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-19018][SQL] Add support for custom encoding on csv writer #20949
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
b9a7bf0
8fd3e0e
0d0addf
91f4750
349e132
fd857b0
b6311e3
025958a
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 |
|---|---|---|
|
|
@@ -826,7 +826,7 @@ def text(self, path, compression=None, lineSep=None): | |
| def csv(self, path, mode=None, compression=None, sep=None, quote=None, escape=None, | ||
| header=None, nullValue=None, escapeQuotes=None, quoteAll=None, dateFormat=None, | ||
| timestampFormat=None, ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, | ||
| charToEscapeQuoteEscaping=None): | ||
| charToEscapeQuoteEscaping=None, encoding=None): | ||
| """Saves the content of the :class:`DataFrame` in CSV format at the specified path. | ||
|
|
||
| :param path: the path in any Hadoop supported file system | ||
|
|
@@ -876,6 +876,8 @@ def csv(self, path, mode=None, compression=None, sep=None, quote=None, escape=No | |
| the quote character. If None is set, the default value is | ||
| escape character when escape and quote characters are | ||
| different, ``\0`` otherwise.. | ||
| :param encoding: sets encoding used for encoding the file. If None is set, it | ||
| uses the default value, ``UTF-8``. | ||
|
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. Likewise, let's match the doc to JSON's. |
||
|
|
||
| >>> df.write.csv(os.path.join(tempfile.mkdtemp(), 'data')) | ||
| """ | ||
|
|
@@ -885,7 +887,8 @@ def csv(self, path, mode=None, compression=None, sep=None, quote=None, escape=No | |
| dateFormat=dateFormat, timestampFormat=timestampFormat, | ||
| ignoreLeadingWhiteSpace=ignoreLeadingWhiteSpace, | ||
| ignoreTrailingWhiteSpace=ignoreTrailingWhiteSpace, | ||
| charToEscapeQuoteEscaping=charToEscapeQuoteEscaping) | ||
| charToEscapeQuoteEscaping=charToEscapeQuoteEscaping, | ||
| encoding=encoding) | ||
| self._jwrite.csv(path) | ||
|
|
||
| @since(1.5) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -623,6 +623,7 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) { | |||||
| * enclosed in quotes. Default is to only escape values containing a quote character.</li> | ||||||
| * <li>`header` (default `false`): writes the names of columns as the first line.</li> | ||||||
| * <li>`nullValue` (default empty string): sets the string representation of a null value.</li> | ||||||
| * <li>`encoding` (default `UTF-8`): encoding to use when saving to file.</li> | ||||||
|
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. I think we should match the doc with JSON's spark/sql/core/src/main/scala/org/apache/spark/sql/DataFrameWriter.scala Lines 525 to 526 in 6ea582e
|
||||||
| * <li>`compression` (default `null`): compression codec to use when saving to file. This can be | ||||||
| * one of the known case-insensitive shorten names (`none`, `bzip2`, `gzip`, `lz4`, | ||||||
| * `snappy` and `deflate`). </li> | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
|
|
||
| package org.apache.spark.sql.execution.datasources.csv | ||
|
|
||
| import java.nio.charset.Charset | ||
|
|
||
| import org.apache.hadoop.conf.Configuration | ||
| import org.apache.hadoop.fs.{FileStatus, Path} | ||
| import org.apache.hadoop.mapreduce._ | ||
|
|
@@ -146,7 +148,13 @@ private[csv] class CsvOutputWriter( | |
| context: TaskAttemptContext, | ||
| params: CSVOptions) extends OutputWriter with Logging { | ||
|
|
||
| private val writer = CodecStreams.createOutputStreamWriter(context, new Path(path)) | ||
| private val charset = Charset.forName(params.charset) | ||
|
|
||
| private val writer = CodecStreams.createOutputStreamWriter( | ||
| context, | ||
|
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. tiny nit: private val writer = CodecStreams.createOutputStreamWriter(context, new Path(path), charset) |
||
| new Path(path), | ||
| charset | ||
| ) | ||
|
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. Move the |
||
|
|
||
| private val gen = new UnivocityGenerator(dataSchema, writer, params) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -513,6 +513,43 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| test("Save csv with custom charset") { | ||||||||||||||||||||||||||||||||||||
|
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. Could you prepend |
||||||||||||||||||||||||||||||||||||
| Seq("iso-8859-1", "utf-8", "windows-1250").foreach { encoding => | ||||||||||||||||||||||||||||||||||||
|
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. Could you check the spark/sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala Lines 2322 to 2338 in c7e2742
|
||||||||||||||||||||||||||||||||||||
| withTempDir { dir => | ||||||||||||||||||||||||||||||||||||
|
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.
|
||||||||||||||||||||||||||||||||||||
| val csvDir = new File(dir, "csv").getCanonicalPath | ||||||||||||||||||||||||||||||||||||
| // scalastyle:off | ||||||||||||||||||||||||||||||||||||
|
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. Let's ignore the specific rule for this, e.g.: |
||||||||||||||||||||||||||||||||||||
| val originalDF = Seq("µß áâä ÁÂÄ").toDF("_c0") | ||||||||||||||||||||||||||||||||||||
| // scalastyle:on | ||||||||||||||||||||||||||||||||||||
| originalDF.write | ||||||||||||||||||||||||||||||||||||
| .option("header", "false") | ||||||||||||||||||||||||||||||||||||
|
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. The header flag is disabled by default. Just in case, are there any specific reasons fro testing without CSV header?
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. My bad, there is no reason. It's fixed on the next commit. |
||||||||||||||||||||||||||||||||||||
| .option("encoding", encoding) | ||||||||||||||||||||||||||||||||||||
| .csv(csvDir) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| val df = spark | ||||||||||||||||||||||||||||||||||||
| .read | ||||||||||||||||||||||||||||||||||||
| .option("header", "false") | ||||||||||||||||||||||||||||||||||||
| .option("encoding", encoding) | ||||||||||||||||||||||||||||||||||||
|
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. I think our CSV read encoding option is incomplete for now .. there are many discussions about this now. I am going to fix the read path soon. Let me revisit this after fixing it.
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. Now it's fine. I think we decided to support encoding in CSV/JSON datasources. Ignore the comment above. We can proceed separately. |
||||||||||||||||||||||||||||||||||||
| .csv(csvDir) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| checkAnswer(df, originalDF) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| test("bad encoding name on writer") { | ||||||||||||||||||||||||||||||||||||
| val exception = intercept[SparkException] { | ||||||||||||||||||||||||||||||||||||
| withTempDir { dir => | ||||||||||||||||||||||||||||||||||||
|
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.
|
||||||||||||||||||||||||||||||||||||
| val csvDir = new File(dir, "csv").getCanonicalPath | ||||||||||||||||||||||||||||||||||||
| Seq("a,A,c,A,b,B").toDF().write | ||||||||||||||||||||||||||||||||||||
| .option("header", "false") | ||||||||||||||||||||||||||||||||||||
| .option("encoding", "1-9588-osi") | ||||||||||||||||||||||||||||||||||||
| .csv(csvDir) | ||||||||||||||||||||||||||||||||||||
|
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. nit: you could use directly |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| assert(exception.getCause.getMessage.contains("1-9588-osi")) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| test("commented lines in CSV data") { | ||||||||||||||||||||||||||||||||||||
| Seq("false", "true").foreach { multiLine => | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
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.
Could you reformulate this
encoding used for encoding