Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) {
* indicates a timestamp format. Custom date formats follow the formats at
* `java.text.SimpleDateFormat`. This applies to timestamp type.</li>
* </ul>
* <li>`writeEncoding`(default `utf-8`) save dataFrame 2 csv by giving encoding</li>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also should add the same documentation in readwriter.py.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,I will write my unit test and modify this pull request

*
* @since 2.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
val linesReader = new HadoopFileLinesReader(file, conf)
Option(TaskContext.get()).foreach(_.addTaskCompletionListener(_ => linesReader.close()))
linesReader.map { line =>
new String(line.getBytes, 0, line.getLength, csvOptions.charset)
new String(line.getBytes, 0, line.getLength, csvOptions.readCharSet)
}
}

Expand Down Expand Up @@ -195,7 +195,7 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
sparkSession: SparkSession,
options: CSVOptions,
inputPaths: Seq[String]): Dataset[String] = {
if (Charset.forName(options.charset) == StandardCharsets.UTF_8) {
if (Charset.forName(options.readCharSet) == StandardCharsets.UTF_8) {
sparkSession.baseRelationToDataFrame(
DataSource.apply(
sparkSession,
Expand All @@ -204,7 +204,7 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
).resolveRelation(checkFilesExist = false))
.select("value").as[String](Encoders.STRING)
} else {
val charset = options.charset
val charset = options.readCharSet
val rdd = sparkSession.sparkContext
.hadoopFile[LongWritable, Text, TextInputFormat](inputPaths.mkString(","))
.mapPartitions(_.map(pair => new String(pair._2.getBytes, 0, pair._2.getLength, charset)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private[csv] class CSVOptions(@transient private val parameters: CaseInsensitive
val delimiter = CSVTypeCast.toChar(
parameters.getOrElse("sep", parameters.getOrElse("delimiter", ",")))
private val parseMode = parameters.getOrElse("mode", "PERMISSIVE")
val charset = parameters.getOrElse("encoding",
val readCharSet = parameters.getOrElse("encoding",
parameters.getOrElse("charset", StandardCharsets.UTF_8.name()))
val writeCharSet = parameters.getOrElse("writeEncoding",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not necessarily introduce additional option. We could just use charset variable because other options such as nullValue are already applied to both reading and writing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon I think so

parameters.getOrElse("charset", StandardCharsets.UTF_8.name()))

val quote = getChar("quote", '\"')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.spark.sql.execution.datasources.csv

import java.io.{CharArrayWriter, OutputStream, StringReader}
import java.nio.charset.StandardCharsets
import java.io.OutputStream
import java.nio.charset.Charset

import com.univocity.parsers.csv._

Expand Down Expand Up @@ -71,6 +71,7 @@ private[csv] class LineCsvWriter(
output: OutputStream) extends Logging {
private val writerSettings = new CsvWriterSettings
private val format = writerSettings.getFormat
private val writerCharset = Charset.forName(params.writeCharSet)

format.setDelimiter(params.delimiter)
format.setQuote(params.quote)
Expand All @@ -84,7 +85,7 @@ private[csv] class LineCsvWriter(
writerSettings.setHeaders(headers: _*)
writerSettings.setQuoteEscapingEnabled(params.escapeQuotes)

private val writer = new CsvWriter(output, StandardCharsets.UTF_8, writerSettings)
private val writer = new CsvWriter(output, writerCharset, writerSettings)

def writeRow(row: Seq[String], includeHeader: Boolean): Unit = {
if (includeHeader) {
Expand Down