Scala udf UnsupportedOperationException

Scala udf UnsupportedOperationException

我有一个用 scala 编写的数据框 a2 :

val a3 = a2.select(printme.apply(col(“PlayerReference”)))

PlayerReference 列包含一个字符串。

调用 udf 函数:

val printme = udf({
      st: String =>
        val x = new JustPrint(st)
        x.printMe();
        
    })

这个udf函数调用了一个java class :

public class JustPrint {
    private String ss = null;
    
    public JustPrint(String ss) {
        this.ss = ss;
    }
    
    public void printMe() {
        System.out.println("Value : " + this.ss);
    }
}

但是我有这个关于 udf 的错误:

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

本练习的目的是验证调用链。 我应该怎么做才能解决这个问题?

您收到此错误的原因是您的 UDF 没有 return 任何东西,就 spark 而言,它被称为 Unit。

您应该做什么取决于您的实际需要,但是,假设您只想跟踪通过 UDF 的值,您应该更改 printMe 使其 returns 字符串,或者更改 UDF。 像这样:

public String printMe() {
    System.out.println("Value : " + this.ss);
    return this.ss;
}

或者像这样:

val printme = udf({
  st: String =>
    val x = new JustPrint(st)
    x.printMe();
    x
})