gpt4 book ai didi

java - Apache Spark : how to call UDF over dataset in Java?

转载 作者:行者123 更新时间:2023-11-30 06:49:43 25 4
gpt4 key购买 nike

以下 Scala 代码片段在 Java 中的准确翻译是什么?

import org.apache.spark.sql.functions.udf 

def upper(s:String) : String = {
s.toUpperCase
}
val toUpper = udf(upper _)
peopleDS.select(peopleDS(“name”), toUpper(peopledS(“name”))).show

请用 Java 填写以下缺失的语句:

import org.apache.spark.sql.api.java.UDF1;

UDF1 toUpper = new UDF1<String, String>() {
public String call(final String str) throws Exception {
return str.toUpperCase();
}
};

peopleDS.select(peopleDS.col("name"), /* how to run toUpper("name")) ? */.show();

注意

注册 UDF,然后使用 selectExpr 调用对我有用,但我需要类似于上面所示的东西。

工作示例:

sqlContext.udf().register(
"toUpper",
(String s) -> s.toUpperCase(),
DataTypes.StringType
);
peopleDF.selectExpr("toUpper(name)","name").show();

最佳答案

在 Java 中,不注册就调用 UDF 是不可能的。请检查以下讨论:


以下是您的 UDF:

private static UDF1 toUpper = new UDF1<String, String>() {
public String call(final String str) throws Exception {
return str.toUpperCase();
}
};

注册UDF,即可使用callUDF函数。

import static org.apache.spark.sql.functions.callUDF;
import static org.apache.spark.sql.functions.col;

sqlContext.udf().register("toUpper", toUpper, DataTypes.StringType);
peopleDF.select(col("name"),callUDF("toUpper", col("name"))).show();

关于java - Apache Spark : how to call UDF over dataset in Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com