How to register function in sqlContext UDF in scala?

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

I have

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

But he tells me that I need arguments or use _ after that, I tried to use _, but gave me an error. How to register it with an argument. I am new to scala, so I have no idea how to do this.

+4
source share
1 answer
 sqlContext.udf.register("getAge",getAge) 

it should be:

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

The underscore (must contain a space between the function and the underscore) turns the function into a partially applicable function that can be passed during registration.

Additional Information

When we call the function, we must pass all the necessary parameters. If we do not, the compiler will complain.

However, we can request it as a value with which we can go to the required parameters later. As we do this, use the underscore.

getAge means run getAge - for example, def getAge = 10 , giving us 10 . We do not need the result, we want this function. Moreover, with your definition, the compiler sees that getAge requires a parameter, and complains that it was not specified.

Here we want to pass getAge as a function value. We tell Scala, we don’t yet know the parameter, we want the function to be like a value, and we will provide it with the required parameter later. So we use getAge _ .

Assuming a signature for getAge :

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

getAge _ becomes an anonymous function:

 Long => Long = <function> 

which means that he needs a parameter of type Long , and the result of his call will give a value of type Long .

+13
source

Source: https://habr.com/ru/post/1011487/


All Articles