You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hive.apache.org by Yu PENG <lo...@gmail.com> on 2013/02/23 02:09:45 UTC

Function definition in hive

Hi,

I am a newbie for Hive. I was assigned a job to do a lot of queries with
similar structure. I just want to ask if there is a way to define functions
like other programming languages so that I can call the function with
different parameters. This will be really helpful. Does anyone know that?

Thanks,
Yu Peng

Re: Function definition in hive

Posted by Alexander Pivovarov <ap...@gmail.com>.
https://cwiki.apache.org/Hive/hiveplugins.html

Creating Custom UDFs

First, you need to create a new class that extends UDF, with one or more
methods named evaluate.

package com.example.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.io.Text;
public final class Lower extends UDF {
  public Text evaluate(final Text s) {
    if (s == null) { return null; }
    return new Text(s.toString().toLowerCase());
  }
}

(Note that there's already a built-in function for this, it's just an easy
example).

After compiling your code to a jar, you need to add this to the hive
classpath. See the section below on deploying jars.

Once hive is started up with your jars in the classpath, the final step is
to register your function:

create temporary function my_lower as 'com.example.hive.udf.Lower';

Now you can start using it:

hive> select my_lower(title), sum(freq) from titles group by my_lower(title);

...

Ended Job = job_200906231019_0006
OK
cmo	13.0
vp	7.0

For a more involved example, see this
page<https://cwiki.apache.org/Hive/genericudafcasestudy.html>
.
Deploying jars for User Defined Functions and User Defined SerDes

In order to start using your UDF, you first need to add the code to the
classpath:


hive> add jar my_jar.jar;
Added my_jar.jar to class path

By default, it will look in the current directory. You can also specify a
full path:

hive> add jar /tmp/my_jar.jar;
Added /tmp/my_jar.jar to class path

Your jar will then be on the classpath for all jobs initiated from that
session. To see which jars have been added to the classpath you can use:

hive> list jars;
my_jar.jar



On Fri, Feb 22, 2013 at 5:09 PM, Yu PENG <lo...@gmail.com> wrote:

> Hi,
>
> I am a newbie for Hive. I was assigned a job to do a lot of queries with
> similar structure. I just want to ask if there is a way to define functions
> like other programming languages so that I can call the function with
> different parameters. This will be really helpful. Does anyone know that?
>
> Thanks,
> Yu Peng
>
>
>