Spark UDF error - Schema for type Any is not supported

百般思念 提交于 2019-12-23 07:49:27

问题


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

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"}

I get this error

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

df.printSchema returns

|-- avg_acc_x: double (nullable = false) 

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


回答1:


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}



回答2:


This error basically occurs when the Analyzer cannot resolve the type of udf properly. That is, the types are mixed (Int and String).

Therefore,

if(avg_acc_x < 0) 0 else avg_acc_x (Integer Type)

or

if(avg_acc_x < 0) "0" else "avg_acc_x" (String Type)

should resolve this exception.



来源:https://stackoverflow.com/questions/37392900/spark-udf-error-schema-for-type-any-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!