Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b9a7bf08fd3e0e0d0addf91f4750349e132fd857b0b6311e3025958aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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