-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9a7bf0
[SPARK-19018][SQL] Add support for custom encoding on csv writer
crafty-coder 8fd3e0e
Merge remote-tracking branch 'upstream/master'
crafty-coder 0d0addf
Fix checkstyle issue
crafty-coder 91f4750
Add test to check UTF-16 and UTF-32
crafty-coder 349e132
Add jira ticked (SPARK-19018:) as prefix on the related tests
crafty-coder fd857b0
Add styling improvements
crafty-coder b6311e3
Merge branch 'master' into master
crafty-coder 025958a
Fix import order on CSVSuite
crafty-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 => | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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