简体   繁体   English

spark write: CSV 数据源不支持空数据类型

[英]spark write: CSV data source does not support null data type

I have an error in my code.我的代码中有错误。 The code is dumping some data into Redshift database.该代码正在将一些数据转储到 Redshift 数据库中。

After some investigation I found an easy way to reproduce it in spark console.经过一番调查,我找到了一种在 spark 控制台中重现它的简单方法。

This is working fine:这工作正常:

scala> Seq("France", "Germany").toDF.agg(avg(lit(null))).write.csv("1.csv")
scala>

But if I replace avg with max I got an error "CSV data source does not support null data type."但是,如果我将avg替换为max,则会出现错误“CSV 数据源不支持空数据类型”。

scala> Seq("France", "Germany").toDF.agg(max(lit(null))).write.csv("2.csv")
java.lang.UnsupportedOperationException: CSV data source does not support null data type.

What's wrong with max ? max 有什么问题?

The error is correct as AVG returns the DOUBLE datatype错误是正确的,因为 AVG 返回 DOUBLE 数据类型

Seq("France", "Germany").toDF.agg(avg(lit(null)).alias("col1")).printSchema

在此处输入图片说明

where as MAX returns the type as null其中 MAX 将类型返回为 null

Seq("France", "Germany").toDF.agg(max(lit(null)).alias("col1")).printSchema

在此处输入图片说明

so while you are writing the dataframe having MAX its throwing the error, if you want to save the dataframe with the max explicitely convert it into another type因此,当您编写具有 MAX 的数据帧时,它会抛出错误,如果您想使用 max 保存数据帧,请将其显式转换为另一种类型

Seq("France", "Germany").toDF.agg(max(lit(null)).alias("col1").cast(DoubleType)).write.csv("path")

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

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