首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hadoop基础教程-第12章 Hive:进阶(12.2 自定义函数)(草稿)

Hadoop基础教程-第12章 Hive:进阶(12.2 自定义函数)(草稿)

作者头像
程裕强
发布2018-01-02 16:37:03
6790
发布2018-01-02 16:37:03
举报

第12章 Hive:进阶

12.2 自定义函数

12.2.1 UDP

使用Eclipse编写UDP函数,可以通过maven下载需要的jar包,pom.xml文件如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hadron</groupId>
    <artifactId>hiveFun</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hiveFun</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>
    <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
</project>
package cn.hadron.hiveFun;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.DoubleWritable;

public class LevelUDF extends UDF{

     public Text evaluate(double sal){

         if(sal<1500){
             return new Text("穷人");
         }else if(sal<2000){
             return new Text("温饱");
         }else if(sal<3000){
             return new Text("低产");
         }else if(sal<6000){
             return new Text("中产");
         }else if(sal<8000){
             return new Text("高产");
         }else{
             return new Text("富人");
         }
     }
}

导出jar包

hive> add jar /root/hiveFun.jar;
Added [/root/hiveFun.jar] to class path
Added resources: [/root/hiveFun.jar]
hive> create temporary function level_sal as 'cn.hadron.hiveFun.LevelUDF';
OK
Time taken: 1.06 seconds
hive>
hive> select ename,level_sal(sal) from emp;
OK
CLARK   低产
KING    中产
MILLER  穷人
SMITH   穷人
JONES   低产
FORD    中产
ALLEN   温饱
WARD    穷人
MARTIN  穷人
BLAKE   低产
TURNER  温饱
JAMES   穷人
HADRON  高产
Time taken: 0.181 seconds, Fetched: 13 row(s)
hive> 

12.2.2 UDAF

UDAF是用户自定义聚合函数。 要实现UDAF,我们需要实现下面的类: org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator AbstractGenericUDAFResolver检查输入参数,并且指定使用哪个resolver。

GenericUDAFResolver类已经过时弃用了,现在是实现GenericUDAFResolver2接口

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第12章 Hive:进阶
  • 12.2 自定义函数
    • 12.2.1 UDP
      • 12.2.2 UDAF
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档