星火SQL:如何从数据帧操作使用JAVA调用UDF [英] Spark SQL: How to call UDF from DataFrame operation using JAVA
问题描述
我想知道如何用JAVA在星火SQL调用UDF函数特定领域语言(DSL)的功能。
I would like to know how to call UDF function from function of domain-specific language(DSL) in Spark SQL using JAVA.
我有UDF函数(只是举例):
I have UDF function (just for example):
UDF2 equals = new UDF2<String, String, Boolean>() {
@Override
public Boolean call(String first, String second) throws Exception {
return first.equals(second);
}
};
我就注册sqlContext
I've registered it to sqlContext
sqlContext.udf().register("equals", equals, DataTypes.BooleanType);
当我运行下面的查询,我的UDF叫,我也得到一个结果。
When I run following query, my UDF is called and I get a result.
sqlContext.sql("SELECT p0.value FROM values p0 WHERE equals(p0.value, 'someString')");
我会transfrom使用领域特定语言的功能星火SQL此查询,我不知道该怎么做。
I would transfrom this query using functions of domain specific language in Spark SQL, and I am not sure how to do it.
valuesDF.select("value").where(???);
我发现存在callUDF()函数,其中它的一个参数是功能2 fnctn但不UDF2。
如何使用UDF和功能从DSL?
I found that there exists callUDF() function where one of its parameters is Function2 fnctn but not UDF2. How can I use UDF and functions from DSL?
推荐答案
我发现与我半满意的解决方案。
它可以调用UDF作为柱条件如:
I found a solution with which I am half-satisfied. It is possible to call UDF as a Column Condition such as:
valuesDF.filter("equals(columnName, 'someString')").select("columnName");
但我仍然不知道它是否可以调用UDF直接。
But I still wonder if it is possible to call UDF directly.
编辑:
顺便说一下,有可能直接调用UDF例如:
Btw, it is possible to call udf directly e.g:
df.where(callUdf("equals", scala.collection.JavaConversions.asScalaBuffer(
Arrays.asList(col("columnName"), col("otherColumnName"))
).seq())).select("columnName");
组织。阿帕奇的进口。火花。SQL。功能是必需的。
import of org.apache.spark.sql.functions is required.
这篇关于星火SQL:如何从数据帧操作使用JAVA调用UDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!