Return-Path: X-Original-To: apmail-hive-user-archive@www.apache.org Delivered-To: apmail-hive-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1F89FDDE5 for ; Sat, 23 Feb 2013 01:20:45 +0000 (UTC) Received: (qmail 80519 invoked by uid 500); 23 Feb 2013 01:20:43 -0000 Delivered-To: apmail-hive-user-archive@hive.apache.org Received: (qmail 80465 invoked by uid 500); 23 Feb 2013 01:20:43 -0000 Mailing-List: contact user-help@hive.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@hive.apache.org Delivered-To: mailing list user@hive.apache.org Received: (qmail 80455 invoked by uid 99); 23 Feb 2013 01:20:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 23 Feb 2013 01:20:43 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of apivovarov@gmail.com designates 209.85.210.171 as permitted sender) Received: from [209.85.210.171] (HELO mail-ia0-f171.google.com) (209.85.210.171) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 23 Feb 2013 01:20:35 +0000 Received: by mail-ia0-f171.google.com with SMTP id z13so1075143iaz.30 for ; Fri, 22 Feb 2013 17:20:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=3q0HxSwRG4GR+yd4qxXoYaOSr+V3oaylqCs6roXdXqg=; b=zDnuCo2IsGWuEV8Q61yZmo3gBQKAkUaLv20XftuxToWv1lzkS1u1hGWkV4FpbzS3Dn SlX1CKeV9qsRq8zwNqjZVQnqqLc/VD3lDUXpoOk7ZXlIpCMd+lc+YyapTLbT9w9GmRUj +ICsUTHha9M9oYbEDdj77nnjrpCTWBJSASmIsFhUfjY/X9RkgGH+q3HnOfDeHht5+ITH A1p7kxkpQKJAsHrHOAE0Zm1ldtR4kmDe2gUw7CYsacoCWQRPfEoF2KwLGxxTP2eSnzTY xaMABn+HLA5Dl8C2kpmI//12aHM1KiXqNoTjDVYlQ1dru5vV+fhwggWkjcd+4QrhqayD JMUw== X-Received: by 10.50.178.73 with SMTP id cw9mr203133igc.22.1361582415420; Fri, 22 Feb 2013 17:20:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.42.178.68 with HTTP; Fri, 22 Feb 2013 17:19:55 -0800 (PST) In-Reply-To: References: From: Alexander Pivovarov Date: Fri, 22 Feb 2013 17:19:55 -0800 Message-ID: Subject: Re: Function definition in hive To: user@hive.apache.org Content-Type: multipart/alternative; boundary=e89a8f647b4134268f04d65a1cf9 X-Virus-Checked: Checked by ClamAV on apache.org --e89a8f647b4134268f04d65a1cf9 Content-Type: text/plain; charset=ISO-8859-1 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 . 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 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 > > > --e89a8f647b4134268f04d65a1cf9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
https://cwiki.apache.org/Hive/hiveplugins.html

=

Creati= ng 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 =3D=3D null) { return null; }
    return new Text(s.toStr=
ing().toLowerCase());
  }
}

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

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

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

cre=
ate 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 =3D job_200906231019_0006
OK
cmo	13.0
vp	7.0

For a more involved example, see=A0this page.

= 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 cla= sspath:


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

By default, it will look in the current directo= ry. 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:

hiv=
e> list jars;
my_jar.jar


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

I am a newbie for Hiv= e. I was assigned a job to do a lot of queries with similar structure. I ju= st 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


=

--e89a8f647b4134268f04d65a1cf9--