-
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 all 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 |
|---|---|---|
|
|
@@ -18,12 +18,14 @@ | |
| 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 | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.util.Properties | ||
|
|
||
| import org.apache.commons.lang3.time.FastDateFormat | ||
| import org.apache.hadoop.io.SequenceFile.CompressionType | ||
|
|
@@ -514,6 +516,41 @@ 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 => | ||
| withTempPath { path => | ||
| val csvDir = new File(path, "csv") | ||
| Seq(content).toDF().write | ||
| .option("encoding", encoding) | ||
| .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. nit: |
||
| val readback = Files.readAllBytes(csvFile.toPath) | ||
| val expected = (content + Properties.lineSeparator).getBytes(Charset.forName(encoding)) | ||
| assert(readback === expected) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-19018: error handling for unsupported charsets") { | ||
| val exception = intercept[SparkException] { | ||
| withTempPath { path => | ||
| val csvDir = new File(path, "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.
nit:
.write.repartition(1)to make sure we write only one file