用Java Lambda编写的Spark UDF引发了ClassCastException

时间:2018-01-02 01:15:47

标签: java apache-spark apache-spark-sql user-defined-functions classcastexception

以下是例外:

java.lang.ClassCastException: cannot assign instance of java.lang.invoke.SerializedLambda to ... of type org.apache.spark.sql.api.java.UDF2 in instance of ...

如果我没有通过Lambda表达式实现UDF,那没关系。像:

private UDF2 funUdf = new UDF2<String, String, String>() {
    @Override
    public String call(String a, String b) throws Exception {
        return fun(a, b);
    }
};
dataset.sparkSession().udf().register("Fun", funUdf, DataTypes.StringType);
functions.callUDF("Fun", functions.col("a"), functions.col("b"));

我在本地投放,因此这个答案将not帮助:https://stackoverflow.com/a/28367602/4164722

为什么呢?我该如何解决?

1 个答案:

答案 0 :(得分:0)

UDF1 myUDF = new UDF1<String, String>() {
            public String call(final String str) throws Exception {
                return str+"A";
            }
        };

sparkSession.udf().register("Fun", myUDF, DataTypes.StringType);

Dataset<Row> rst = sparkSession.read().format("text").load("myFile");

rst.withColumn("nameA",functions.callUDF("Fun",functions.col("name")))