我的spark应用程序读取一个csv文件,使用sql将其转换为其他格式,然后将结果数据帧写入另一个csv文件中。
例如,我输入csv如下:
Id|FirstName|LastName|LocationId
1|John|Doe|123
2|Alex|Doe|234
Run Code Online (Sandbox Code Playgroud)
我的转换是:
Select Id,
FirstName,
LastName,
LocationId as PrimaryLocationId,
null as SecondaryLocationId
from Input
Run Code Online (Sandbox Code Playgroud)
(我不能回答为什么空被用作SecondaryLocationId,它是业务用例)现在火花想不通SecondaryLocationId的数据类型,并返回在架构空和引发错误CSV数据源不支持空数据在写入输出csv时键入。
以下是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)
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以默认为数据类型(例如字符串)?顺便说一句,我可以通过用空string('')替换null来使其工作,但这不是我想要的。