自定义函数

add jar /root/Downloads/apache-hive-1.2.0-bin/lib/udf.jar;

1.引jar  写自己想用的方法

2.将jar  放入hive中的lib下

3.创建一个临时的方法 

create temporary function allsal as "com.bw.hive.function";

4.查询sql结果

package com.bw.hive;

import org.apache.hadoop.hive.ql.exec.UDF;
//用户自定义函数
public class function extends UDF{
    //在hive的udf 中每个执行的函数都叫做evaluate
    //salary*12+bonus
    public Double evaluate(Double a,Double b) {
        Double salary=0.0;
        Double comm=0.0;
        if(a!=null) {
            salary=a;
            
        }
        
        if(b!=null) {
            comm=b;
        }
        return 12*salary+comm;
        
    }
}

 

 

 

 ==========================================================================================================

==============================================================

 

create temporary function myconcat as "com.bw.hive.function";

//注意 要重新退出一下 再进一下hive

package com.bw.hive;

import org.apache.hadoop.hive.ql.exec.UDF;
//用户自定义函数
public class function extends UDF{
    //在hive的udf 中每个执行的函数都叫做evaluate
    //salary*12+bonus
    public Double evaluate(Double a,Double b) {
        Double salary=0.0;
        Double comm=0.0;
        if(a!=null) {
            salary=a;
            
        }
        
        if(b!=null) {
            comm=b;
        }
        return 12*salary+comm;
    }
    
    public String evaluate(String ...words) {
        if(words.length==1) {
            return words[0];
        }else {
            String result="";
            for(String s:words) {
                result+=" "+s;
            }
            return result.replaceFirst(" ","");
        }
    }
    //xxxx(1,2,3,4..........) 1 2 3 4 5 
    /*public String evaluate() {
        return null;
    }*/
    //aa(String[]args)   String[] strs={1,2,3,4,5} aa(strs)
            //bb(String ....args)  bb(1)  bb(1,2,3,4) bb(1,2,3,4)
    /*public static void aa(int[] arr) {}
    public static void bb(int ...args) {}
    public static void main(String[] args) {
        int[] abc= {1,2,3,4,5};
        aa(abc);
        bb(1,2,3,4,5);
    }*/
}

原文地址:https://www.cnblogs.com/JBLi/p/10862929.html