-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 5 commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -625,6 +625,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> | ||||||
|
||||||
| * <li>`encoding` (by default it is not set): specifies encoding (charset) of saved json | |
| * files. If it is not set, the UTF-8 charset will be used. </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,12 @@ 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, | ||
|
||
| new Path(path), | ||
| charset) | ||
|
|
||
| private val gen = new UnivocityGenerator(dataSchema, writer, params) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| package org.apache.spark.sql.execution.datasources.csv | ||
|
|
||
| import java.io.File | ||
| import java.nio.charset.UnsupportedCharsetException | ||
| import java.nio.charset.{Charset, UnsupportedCharsetException} | ||
| import java.nio.file.Files | ||
| import java.sql.{Date, Timestamp} | ||
| import java.text.SimpleDateFormat | ||
| import java.util.Locale | ||
|
|
@@ -512,6 +513,43 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils with Te | |
| } | ||
| } | ||
|
|
||
| test("SPARK-19018: Save csv with custom charset") { | ||
|
|
||
| // scalastyle:off nonascii | ||
| val content = "µß áâä ÁÂÄ" | ||
| // scalastyle:on nonascii | ||
|
|
||
| Seq("iso-8859-1", "utf-8", "utf-16", "utf-32", "windows-1250").foreach { encoding => | ||
| withTempDir { dir => | ||
|
||
| val csvDir = new File(dir, "csv") | ||
|
|
||
| val originalDF = Seq(content).toDF("_c0").repartition(1) | ||
|
||
| originalDF.write | ||
| .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.getCanonicalPath) | ||
|
|
||
| csvDir.listFiles().filter(_.getName.endsWith("csv")).foreach({ csvFile => | ||
|
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.
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. What do you mean?
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: |
||
| val readback = Files.readAllBytes(csvFile.toPath) | ||
| val expected = (content + "\n").getBytes(Charset.forName(encoding)) | ||
|
||
| assert(readback === expected) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-19018: error handling for unsupported charsets") { | ||
| val exception = intercept[SparkException] { | ||
| withTempDir { dir => | ||
|
||
| val csvDir = new File(dir, "csv").getCanonicalPath | ||
| Seq("a,A,c,A,b,B").toDF().write | ||
| .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.
Likewise, let's match the doc to JSON's.