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>3.0.0-cdh6.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>2.1.1-cdh6.2.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 HIVE_UDF extends UDF {
public Text evaluate(final Text s) {
    if (null == s) {
        return null;
    }
    //返回大写字母
    return new Text(s.toString().toUpperCase());

}

}

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

第四步:添加我们的jar包

hive的客户端添加我们的jar包
add jar $HIVE_HOME/lib/udf.jar

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

创建临时函数

create temporary function touppercase as 'cn.udf.HIVE_UDF';

删除临时函数

drop temporary function touppercase;

创建永久函数

create function touppercase as 'cn.udf.HIVE_UDF';

删除永久函数

drop  function touppercase1;

第六步:使用自定义函数

select touppercase('abc');
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

数据湖填坑

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值