hive 创建日期的udf函数(获取昨天今天明天)

创建udf函数的全流程

pom文件

<name>g6-hadoop</name>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hadoop.version>2.6.0-cdh6.1.0</hadoop.version>
        <hive.version>1.1.0-cdh6.1.0</hive.version>
    </properties>
    <!--添加CDH的仓库-->
    <repositories>
        <repository>
            <id>nexus-aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        </repository>
    </repositories>
    <dependencies>
        <!--添加Hadoop的依赖-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--添加hive依赖-->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

直接编写代码

package com.gd.udf;

import org.apache.hadoop.hive.ql.exec.UDF;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class GetDateUDF extends UDF {

    public String evaluate(int i) {
        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,i);//把日期往后增加一天.整数往后推,负数往前移动,0代表今天的时间
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }
    public static void main(String[] args) {
        System.out.println("今天时间:"+new GetDateUDF().evaluate(0));
        System.out.println("明天时间:"+new GetDateUDF().evaluate(1));
        System.out.println("昨天天时间:"+new GetDateUDF().evaluate(-1));
    }

}

之后进行打包
在这里插入图片描述
先clean之后再 package

然后在idea左侧的target目录下就会出现jar包

之后将jar包上传到 自己的服务器上
此处介绍一种临时的 一种永久的udf函数

首先临时UDF函数

将文件上传到
/root/shell/hiveUDF 文件名称是HiveUDF-1.0-SNAPSHOT_getdate.jar

之后进入hive

执行

add jar /root/shell/hiveUDF/HiveUDF-1.0-SNAPSHOT_getdate.jar;

CREATE TEMPORARY FUNCTION getdate as ‘com.gd.udf.GetDateUDF’;

然后 show functions; 就能看到定义的udf函数

select getdate(1);

删除临时udf 函数 DROP TEMPORARY FUNCTION IF EXISTS getdate;

创建永久UDF函数

1 在hive执行
add jar /root/shell/hiveUDF/HiveUDF-1.0-SNAPSHOT_getdate.jar;

2在hdfs上创建目录
hadoop fs -mkdir /lib

3将jar包上传到hdfs
hadoop fs -put /root/shell/hiveUDF/HiveUDF-1.0-SNAPSHOT_getdate.jar /lib/

4 在hive执行
CREATE FUNCTION getdate as ‘com.gd.udf.GetDateUDF’ using jar ‘hdfs://ip:8020/lib/HiveUDF-1.0-SNAPSHOT_getdate.jar’;

注意:端口可能不一样
Hadoop1.2.1中fs.default.name=hdfs://localhost:9000
Hadoop2.2.0中fs.default.name=hdfs://localhost:8020

以上UDF函数创建完成 就可以使用了

有一个小坑:
进入了hive session,然后注册了UDF进行执行,如果发现问题重新打包添加后,重新注册UDF似乎是不会生效的,必须要退出再进入再注册才生效。难道想调试修改UDF,必须要退出Hive session再进去?

这种通常都是两次添加的jar包全路径名称相同导致的。在一次hive session中不会重复加载相同全路径名称的jar包,所以给的直观感受是一直用的旧的。退出后再次进入会重新加载一次classpath。所以两种方法,一个是退出后再次进入,第二个就是修改jar包的名称不要和上次的一样就好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值