【发布时间】:2021-11-29 02:40:33
【问题描述】:
我有这样的数据框:
ID Country Revenue
1 US 1000
2 IND 2000
3 DE 4000
我正在尝试转换为 JSON 格式并写入我的本地路径。
输出:
[
{
"ID": 1,
"Country": "US",
"Revenue": 1000
},
{
"ID": 2,
"Country": "IND",
"Revenue": 2000
},......
]
代码:
import spark.implicits._
val DF = spark.sql("select ID,Country,Revenue from table")
DF.show()
case class ID(ID:int)
case class country(country:String)
case class Revenue(Revenue:Int)
case class details(ID:ID,country:country,Revenue:Revenue)
val JsonDF= DF.map(r=>{val details_1=details(r.getString(0),r.getString(1),r.getString(2))})
JsonDF.repartition(1).write.option("multiLine","true").json("C:/Desktop/output/revenue.json")
但我收到以下错误:
找不到单元类型的编码器。需要一个隐式 Encoder[Unit] 来将 Unit 实例存储在 Dataset 中。导入 spark.implicits 支持原始类型(Int、String 等)和产品类型(案例类)。_
我的错误在哪里?
【问题讨论】:
-
你得到什么错误?
-
@GaëlJ 此错误无法找到 Unit 类型的编码器。需要一个隐式 Encoder[Unit] 来将 Unit 实例存储在 Dataset 中。通过导入 spark.implicits._ 支持原始类型(Int、String 等)和产品类型(案例类)
标签: scala apache-spark