hive函数(内置函数,自定义函数)&&java自定义函数

一、hive内置函数

1、查看系统自带的函数

hive> show functions;

2、显示自带的函数用法

hive> desc function upper;

3、详细显示自带的函数用法

hive> desc function extended upper;

二、hive自定义函数

当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数。

(一)根据用户自定义函数类别分为以下三种:

1、UDF(User-Defined-Function)

一进一出

2、UDAF(User-Defined Aggregation Function)

聚集函数,多进一出

类似于:count/max/min

3、UDTF(User-Defined Table-Generating Functions)

一进多出

如lateral view explore()

(二)编程步骤:

继承org.apache.hadoop.hive.ql.UDF

需要实现evaluate函数;evaluate函数支持重载;

1、导入pom相关依赖和插件

<repositories>
    <repository>
        <id>cloudera</id>
 <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.0-cdh5.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.1.0-cdh5.14.0</version>
    </dependency>
</dependencies>
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>2.2</version>
         <executions>
             <execution>
                 <phase>package</phase>
                 <goals>
                     <goal>shade</goal>
                 </goals>
                 <configuration>
                     <filters>
                         <filter>
                             <artifact>*:*</artifact>
                             <excludes>
                                 <exclude>META-INF/*.SF</exclude>
                                 <exclude>META-INF/*.DSA</exclude>
                                 <exclude>META-INF/*/RSA</exclude>
                             </excludes>
                         </filter>
                     </filters>
                 </configuration>
             </execution>
         </executions>
     </plugin>
</plugins>
</build>

2、开发java类继承UDF,并重载evaluate 方法

public class ItcastUDF extends UDF {
    public Text evaluate(final Text s) {
        if (null == s) {
            return null;
        }
        //返回大写字母
        return new Text(s.toString().toUpperCase());

    }
}

3、将我们的项目打包,并上传到虚拟机/tmp目录下

 

《hive函数(内置函数,自定义函数)&&java自定义函数》

4、添加我们的jar包

进入hive的shell窗口添加我们的jar包

//add jar jar包所在的绝对路径

add jar /tmp/original-task_20191124_udf-1.0-SNAPSHOT.jar;

 

5、设置函数与我们的自定义函数关联

<--创建临时函数-->

create temporary function tolowercase as 'com.czxy.udf.ItcastUDF';    //包名.类名

<--删除临时函数-->

drop temporary function tolowercase




<--创建永久函数-->

create function tolowercase1 as 'com.czxy.udf.ItcastUDF';

<--删除永久函数-->

drop   function tolowercase1;

6、使用自定义函数

select tolowercase('abc');

(三)注意事项

(1)UDF必须要有返回类型,可以返回null,但是返回类型不能为void;

(2UDF中常用Text/LongWritable类型,不推荐使用java类型;

三、java方式自定义函数

1、书写函数逻辑

package com.czxy.demo01;

public class custom_function {

    public String gettr(String data){
        return data+"哈哈哈哈哈";
    }

}

2、打包上传到虚拟机/tmp目录下

《hive函数(内置函数,自定义函数)&&java自定义函数》

3、添加我们的jar包并使用

add jar jar包在虚拟机上的绝对路径
select reflect(‘包名.类名’,’函数名’,’传递到函数的参数’);

hive (default)> add jar /opt/original-task_20191124_udf-1.0-SNAPSHOT.jar;
Added [/opt/original-task_20191124_udf-1.0-SNAPSHOT.jar] to class path
Added resources: [/opt/original-task_20191124_udf-1.0-SNAPSHOT.jar]
hive (default)> select reflect('com.czxy.demo01.custom_function','getStr','chundidecanshu   ');
OK
_c0
chundidecanshu   aaaaa
Time taken: 12.427 seconds, Fetched: 1 row(s)
hive (default)> 

《hive函数(内置函数,自定义函数)&&java自定义函数》

 

    原文作者:依旧ฅ=ฅ
    原文地址: https://blog.csdn.net/qq_44065303/article/details/103372532
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞