大数据必知必会系列——萌新提问怎么定义HiveUDF函数?能否给个示例

引言

大家好,我是ChinaManor,直译过来就是中国码农的意思,俺希望自己能成为国家复兴道路的铺路人,大数据领域的耕耘者,一个平凡而不平庸的人。
学习大数据差不多一年了,笔者最近在整理大数据学习的笔记资料,这个系列是整理的一些大数据必知必会的知识。

在这里插入图片描述

下面为大家分享Hive中UDF如何自定义

UDF开发实例
简单UDF示例

第一步:创建maven java 工程,导入jar包

<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>


第二步:开发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());

    }
}

第三步:将我们的项目打包,并上传到hive的lib目录下

第四步:添加我们的jar包
重命名我们的jar包名称

cd /export/servers/hive-1.1.0-cdh5.14.0/lib
mv original-day_06_hive_udf-1.0-SNAPSHOT.jar udf.jar

hive的客户端添加我们的jar包

add jar /export/servers/hive-1.1.0-cdh5.14.0/lib/udf.jar;

第五步:设置函数与我们的自定义函数关联
创建临时函数

create temporary function tolowercase as 'cn.itcast.udf.ItcastUDF';

删除临时函数
drop temporary function tolowercase

创建永久函数

create function tolowercase1 as 'cn.itcast.udf.ItcastUDF';

删除永久函数

drop   function tolowercase1;

第六步:使用自定义函数

select tolowercase('abc');

总结

以上便是项目选型上的一些参考回答,愿你读过之后有自己的收获,如果有收获不妨关注一下~我们下期再见👋
上面文章对你有帮助吗?如果有的话,不妨一键三连~~

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIMaynor

觉得有用,要个免费的三连可有?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值