hive自定义UDF函数,用来判断时间是当月的第几周

常规操作,先在pom.xml中添加依赖


        
        
            org.apache.hive
            hive-exec
            2.1.1
        
        
        
            org.apache.hadoop
            hadoop-common
            2.7.5
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.0
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

如果碰到缺少pentaho-aggdesigner-algorithm.jar这个jar包,去https://mvnrepository.com/artifact/org.pentaho/pentaho-aggdesigner-algorithm/5.1.5-jhyde
这个网址下载一个,放到org\pentaho\pentaho-aggdesigner-algorithm\5.1.5-jhyde这个目录下面。
不说了,上代码

package com.ping;

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

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

/**
 * @author zheng
 * @date 2019/12/24 21:33
 */
public class Week extends UDF {

    public int evaluate(String time){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //第几周
        int week = calendar.get(Calendar.WEEK_OF_MONTH);
        return week;
    }
}

打成jar导入到你的hive安装目录的lib目录下面
add jar /export/servers/apache-hive-2.1.1-bin/lib/.jar;
创建对应的函数create temporary function week as ‘com.pingan.Week’;
hive自定义UDF函数,用来判断时间是当月的第几周_第1张图片
ok完成了

hue中运行的效果hive自定义UDF函数,用来判断时间是当月的第几周_第2张图片

你可能感兴趣的:(hive自定义UDF函数,用来判断时间是当月的第几周)