我的spark应用程序读取csv文件,使用sql将其转换为不同的格式,并将结果数据帧写入不同的csv文件 .
例如,我输入csv如下:
Id|FirstName|LastName|LocationId
1|John|Doe|123
2|Alex|Doe|234
我的转变是:
Select Id,
FirstName,
LastName,
LocationId as PrimaryLocationId,
null as SecondaryLocationId
from Input
(我可以't answer why the null is being used as SecondaryLocationId, it is business use case) Now spark can'弄清楚SecondaryLocationId的数据类型并在架构中返回null并在写入输出csv时抛出错误 CSV data source does not support null data type .
下面是我正在使用的printSchema()和写入选项 .
root
|-- Id: string (nullable = true)
|-- FirstName: string (nullable = true)
|-- LastName: string (nullable = true)
|-- PrimaryLocationId: string (nullable = false)
|-- SecondaryLocationId: null (nullable = true)
dataFrame.repartition(1).write
.mode(SaveMode.Overwrite)
.option("header", "true")
.option("delimiter", "|")
.option("nullValue", "")
.option("inferSchema", "true")
.csv(outputPath)
有没有办法默认为数据类型(如字符串)?顺便说一句,我可以通过用空字符串('')替换null来实现这一点,但这不是我想要做的 .
1 回答
use lit(null): import org.apache.spark.sql.functions.{lit, udf}
例:
或使用这样的UDF:
重新发布zero323代码 .
现在让我们讨论你的第二个问题
Question :
“这只有在我知道哪些列将被视为空数据类型时 . 当正在读取大量文件并应用各种转换时,我不知道或者有什么方法我可能知道哪些字段被空处理?“
Ans :
在这种情况下,您可以使用选项
Databricks Scala style guide不同意应始终禁止在Scala代码中使用null,并说:“对于性能敏感的代码,首选null而不是Option,以避免虚方法调用和装箱 . ”
示例:
https://medium.com/@mrpowers/dealing-with-null-in-spark-cfdbb12f231e
https://github.com/vaquarkhan/scala-style-guide