You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spark/Scala嵌套值更新时出现数据类型不匹配错误

DataFrame数组结构体替换Null为空串后写入Parquet报错问题

输入DataFrame

+---------------------------------------------------------------------+ 
|infos                                                                |
+---------------------------------------------------------------------+ 
|[{100, 1, foo}, {103, 1, bar}, {99, 0, null}]                        | 
|[{101, 1, null}, {102, 1, null}]                                     | 
|[]                                                                   |
+---------------------------------------------------------------------+

Schema定义

root
 |-- Infos: array (nullable = false)
 |    |-- element: struct (containsNull = true)
 |    |    |-- id: string (nullable = true)
 |    |    |-- val: string (nullable = true)
 |    |    |-- com: string (nullable = true)

问题描述

需要将DataFrame中所有null值替换为空字符串,自行编写了递归更新的Scala代码。执行outputDf.show(false)时结果正常,但写入Parquet文件时触发数据类型不匹配错误:

Caused by: java.lang.RuntimeException: java.lang.String is not a valid external type for schema of array<struct < id:string, val:string, com:string> >

不清楚报错的具体原因,想请教是否有更合适的方式重写nullToEmptyString函数,以正确处理空struct类型?


内容的提问来源于stack exchange,提问作者ilmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:24:11