如何重命名现有的 Spark SQL 函数 [英] How to rename an existing Spark SQL function

查看:42
本文介绍了如何重命名现有的 Spark SQL 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spark 对用户提交的数据调用函数.

I am using Spark to call functions on the data which is submitted by the user.

如何将已存在的函数重命名为不同的名称,例如将 REGEXP_REPLACE 重命名为 REPLACE?

How can I rename an already existing function to a different name like like REGEXP_REPLACE to REPLACE?

我尝试了以下代码:

ss.udf.register("REPLACE", REGEXP_REPLACE)           // This doesn't work
ss.udf.register("sum_in_all", sumInAll)
ss.udf.register("mod", mod)
ss.udf.register("average_in_all", averageInAll)

推荐答案

用别名导入:

import org.apache.spark.sql.functions.{regexp_replace => replace }
df.show
+---+
| id|
+---+
|  0|
|  1|
|  2|
|  3|
|  4|
|  5|
|  6|
|  7|
|  8|
|  9|
+---+

df.withColumn("replaced", replace($"id", "(\\d)" , "$1+1") ).show

+---+--------+
| id|replaced|
+---+--------+
|  0|     0+1|
|  1|     1+1|
|  2|     2+1|
|  3|     3+1|
|  4|     4+1|
|  5|     5+1|
|  6|     6+1|
|  7|     7+1|
|  8|     8+1|
|  9|     9+1|
+---+--------+

要使用 Spark SQL 执行此操作,您必须使用不同的名称在 Hive 中重新注册该函数:

To do it with Spark SQL, you'll have to re-register the function in Hive with a different name :

sqlContext.sql(" create temporary function replace 
                 as 'org.apache.hadoop.hive.ql.udf.UDFRegExpReplace' ")

sqlContext.sql(""" select replace("a,b,c", "," ,".") """).show
+-----+
|  _c0|
+-----+
|a.b.c|
+-----+

这篇关于如何重命名现有的 Spark SQL 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆