Hive之UDF

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.sid.hive.udf</groupId>
    <artifactId>hiveudf</artifactId>
    <version>1.0</version>

    <properties>
        <hadoop.version>2.9.0</hadoop.version>
        <hive.version>2.3.3</hive.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>${hive.version}</version>
        </dependency>
    </dependencies>

</project>
package com.zoujc.myhive;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * hive自定义函数
 * 1.继承 UDF 类
 * 2.重写evaluate方法
 */
public final class HiveUdfTest extends UDF {

   // 这里接收参数的类型必须是hadoop能支持的输入输出类型
   public Text evaluate(final Text str){
      if(str == null){
         return null;
      }
      return new Text(str.toString().toLowerCase());
   }
}

打包
在这里插入图片描述


把生成的jar包上传到hive所在的服务器

把jar包添加到hive中
hive
在这里插入图片描述
往服务器添加jar包

在这里插入图片描述

查看导入的jar包

在这里插入图片描述

创建临时函数,只在当前会话有效
在这里插入图片描述
使用结果
在这里插入图片描述
创建永久的自定义函数

将jar包放到hdfs上,不可以放到OS上。

创建的是在sid库下面的自定义函数
create function sid.test_udf_lower as com.zoujc.myhive.HiveUdfTest
using jar ‘hdfs:///root/opt/hiveudf-1.0.jar’;
use sid;
select student_id,test_udf_lower(name),age from student;

猜你喜欢

转载自blog.csdn.net/weixin_38799368/article/details/83031501