Spark UDF 错误 - 不支持 Any 类型的架构 [英] Spark UDF error - Schema for type Any is not supported

查看:30
本文介绍了Spark UDF 错误 - 不支持 Any 类型的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 udf,它将用 0 替换列中的负值.

I’m trying to create a udf that will replace negative values in a column with 0.

我的数据框名为 df,包含一列名为 avg_x.这是我创建 udf 的代码

My dataframe is – called df, and contains one column called avg_x. This is my code for creating a udf

val noNegative = udf {(avg_acc_x: Double) => if(avg_acc_x < 0) 0 else "avg_acc_x"}

我收到此错误

java.lang.UnsupportedOperationException: Schema for type Any is not supported

df.printSchema 返回

df.printSchema returns

|-- avg_acc_x: double (nullable = false) 

所以我不明白为什么会出现这个错误?

so I don’t understand why this error is occurring?

推荐答案

这是因为 else 返回一个 String: "avg_acc_x".去掉引号:

It's because of the else returning a String: "avg_acc_x". Take away the quotes:

val noNegative = udf {(avg_acc_x: Double) => if(avg_acc_x < 0) 0 else avg_acc_x}

这篇关于Spark UDF 错误 - 不支持 Any 类型的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆