简体   繁体   English

无法使用 Spark Scala 使用标头写入 CSV

[英]Not able to write to CSV with header using Spark Scala

I've never had this issue before, but for some reason when I'm writing a dataframe to CSV in spark scala, the output CSV file is in completely wrong format.我以前从未遇到过这个问题,但是由于某种原因,当我在 spark scala 中将数据帧写入 CSV 时,输出的 CSV 文件格式完全错误。 1, it doesn't have any header row, and 2, there are random large blank gaps between the columns. 1,它没有任何标题行,2,列之间有随机的大空白。

But the funny part is when I do df.show in the IDE, it outputs absolutely fine with the header and in proper format.但有趣的是,当我在 IDE 中执行 df.show 时,它输出的标题和格式都非常好。

I'm using a very basic, generic write to csv method,我正在使用一种非常基本的通用写入 csv 方法,

df.write.csv("output.csv")

Why could this be happening?为什么会发生这种情况? Is it because of some joining and merging that I'm doing that is being distributed across clusters and not able to reformat properly before writing to CSV?是不是因为我正在做的一些加入和合并正在跨集群分布并且在写入 CSV 之前无法正确重新格式化?

You are missing some option :您缺少一些选项:

  • sep (default , ): sets a single character as a separator for each field and value. sep (默认, ):将单个字符设置为每个字段和值的分隔符。
  • quote (default " ): sets a single character used for escaping quoted values where the separator can be part of the value. If an empty string is set, it uses u0000 (null character). quote (default " ): 设置用于转义引用值的单个字符,其中分隔符可以是值的一部分。如果设置了空字符串,则使用 u0000 (空字符)。
  • escape (default \\ ): sets a single character used for escaping quotes inside an already quoted value. escape (默认\\ ):设置用于在已引用的值中转义引号的单个字符。
  • charToEscapeQuoteEscaping (default escape or \\0 ): sets a single character used for escaping the escape for the quote character. charToEscapeQuoteEscaping (默认escape\\0 ):设置用于转义引号字符的单个字符。 The default value is escape character when escape and quote characters are different, \\0 otherwise.当转义字符和引号字符不同时,默认值为转义字符,否则为 \\0。
  • escapeQuotes (default true ): a flag indicating whether values containing quotes should always be enclosed in quotes. escapeQuotes (默认为true ):一个标志,指示是否应始终将包含引号的值括在引号中。 Default is to escape all values containing a quote character.默认是转义包含引号字符的所有值。
  • quoteAll (default false ): a flag indicating whether all values should always be enclosed in quotes. quoteAll (默认false ):一个标志,指示是否所有值都应始终用引号括起来。 Default is to only escape values containing a quote character.默认是仅转义包含引号字符的值。
  • header (default false ): writes the names of columns as the first line. header (默认false ):将列名写入第一行。
  • nullValue (default empty string ): sets the string representation of a null value. nullValue (默认为empty string ):设置空值的字符串表示形式。
  • compression (default null ): compression codec to use when saving to file. compression (默认为null ):保存到文件时使用的压缩编解码器。 This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).这可以是已知的不区分大小写的缩写名称之一(none、bzip2、gzip、lz4、snappy 和 deflate)。
  • dateFormat (default yyyy-MM-dd) : sets the string that indicates a date format. dateFormat (default yyyy-MM-dd) :设置表示日期格式的字符串。 Custom date formats follow the formats at java.text.SimpleDateFormat.自定义日期格式遵循 java.text.SimpleDateFormat 中的格式。 This applies to date type.这适用于日期类型。
  • timestampFormat (default yyyy-MM-dd'T'HH:mm:ss.SSSXXX) : sets the string that indicates a timestamp format. timestampFormat (default yyyy-MM-dd'T'HH:mm:ss.SSSXXX) :设置表示时间戳格式的字符串。 Custom date formats follow the formats at java.text.SimpleDateFormat.自定义日期格式遵循 java.text.SimpleDateFormat 中的格式。 This applies to timestamp type.这适用于时间戳类型。
  • ignoreLeadingWhiteSpace (default true )`: a flag indicating whether or not leading whitespaces from values being written should be skipped. ignoreLeadingWhiteSpace (default true )`:一个标志,指示是否应跳过正在写入的值中的前导空格。
  • ignoreTrailingWhiteSpace (default true ): a flag indicating defines whether or not trailing whitespaces from values being written should be skipped. ignoreTrailingWhiteSpace (默认为true ):一个标志,用于定义是否应跳过正在写入的值的尾随空格。

In you case :在你的情况下:

df.write.option("header","true").csv("output.csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM