如何在Scala中将函数注册到sqlContext UDF? [英] How do I register a function to sqlContext UDF in scala?

查看:138
本文介绍了如何在Scala中将函数注册到sqlContext UDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为getAge(timestamp:Long)的方法,我想将此方法注册为sql函数.

I have a method called getAge(timestamp:Long) and I want to register this as a sql function.

我有

sqlContext.udf.register("getAge",getAge) 

但是它告诉我我需要参数或之后使用_,我尝试使用_但给了我错误.我如何用一个参数注册它.我是Scala的新手,所以我不知道该怎么做.

But its telling me I need arguments or use _ afterwards, I tried using _ but gives me error. How do I register it with an argument. I am new to scala so I have no idea how to do this.

推荐答案

sqlContext.udf.register("getAge",getAge) 

应该是:

sqlContext.udf.register("getAge",getAge _)

下划线(在函数和下划线之间必须有空格)将功能转换为可以在注册中传递的部分应用的功能.

The underscore (must have a space in between function and underscore) turns the function into a partially applied function that can be passed in the registration.

当我们调用一个函数时,我们必须传入所有必需的参数.如果我们不这样做,编译器会抱怨.

When we invoke a function, we have to pass in all the required parameters. If we don't, the compiler will complain.

但是,我们可以要求它提供一个函数值,以便以后可以传入所需的参数.我们的方法是使用下划线.

We can however ask it for the function as a value, with which we can pass in the required parameters at a later time. How we do this is to use the underscore.

getAge表示运行getAge-例如,def getAge = 10给我们10.我们不想要结果,我们想要函数.而且,根据您的定义,编译器会看到getAge需要一个参数,并抱怨没有给出一个参数.

getAge means to run getAge - for example, def getAge = 10 giving us 10. We don't want the result, we want the function. Moreover, with your definition, the compiler sees that getAge requires a parameter, and complains that one wasn't given.

我们在这里要做的是将getAge作为函数值传递.我们告诉Scala,我们尚不知道参数,我们希望函数作为值,并在以后提供所需的参数.因此,我们使用getAge _.

What we want to do here is to pass getAge as a function value. We tell Scala, we don't know the parameter yet, we want the function as a value and we'll supply it with the required parameter at a later time. So, we use getAge _.

假定getAge的签名为:

getAge(l: Long): Long = <function>

getAge _成为匿名函数:

Long => Long = <function>

这意味着它需要一个Long类型的参数,调用它的结果将产生一个Long类型的值.

which means it needs a parameter of type Long and the result of invoking it will yield a value of type Long.

这篇关于如何在Scala中将函数注册到sqlContext UDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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