hive的用户自定udf的讲解

版权声明:版权声明中 https://blog.csdn.net/lds_include/article/details/88783945

udf用户自定义函数

1.为什么需要UDF

  1. 因为内部函数没法满足需求。
  2. hive它本身就是一个灵活框架,允许用自定义模块功能,如可以自定义UDF、serde、输入输出等。

2.UDF是什么

  • UDF:user difine function,用户自定义函数,一对一。常用
  • UDAF:user define aggregate function,用户自定义聚合函数,多对一。
  • UDTF:user define table_generate function,用户自定义表生成函数,一对多。

3.怎么编写UDF函数

  1. 该类需要继承UDF,重写evaluate(),允许该方法重载。
  2. 也可以继承 genericUDF,需要重写 initliaze() 、 getDisplay() 、 evaluate()

4.UDF的使用

第一种:实现字符串的拼接(当前session有效)

  • 编写udf函数并且打好jar包

    import org.apache.hadoop.hive.ql.exec.UDF;
    
    /**
     * @description
     * @author: LuoDeSong [email protected]
     * @create: 2018-10-09 11:50:22
     **/
    public class MyConcatUdf extends UDF{
        public String evaluate(String word){
            if(word == null) return null;
            return word + "_firstUDF";
        }
    }
    
  • 添加自定UDF的jar包到hive中去

    hive>add jar /tempdataforhdfs_lds/hadoop-1.0-SNAPSHOT.jar;
    
  • 在hive中就刚才的包创建自己的临时函数

    hive>create temporary function myfunc as "org.bigdata.hive.udf.MyConcatUdf";
    
  • 测试是否可以使用:

    show functions;
    select myfunc("luodesong");
    
  • 确定无用时可以删除:

    drop temporary function myfunc;
    

第二种:(当前session有效)

  • 编写udf函数,传入一个字符串,再传入一个key,然后输出这个key在这个字符窜中对应的value值。

    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    /**
     * @description 通过传入的key值找value
     * @author: LuoDeSong [email protected]
     * @create: 2019-03-24 11:25:08
     **/
    /*
        string为sex=1&hight=180&weight=130&sal=28000&facevalue=900
        要将它转化为json字符串
     */
    public class FindValueByKey extends UDF {
        public String evaluate(String string, String key) {
            if (string == null || key == null) return null;
            string = string.replaceAll("&", ",");
            string = string.replaceAll("=",":");
            string = "{" + string + "}";
            try {
                JSONObject jsonObject = new JSONObject(string);
                String string1 = jsonObject.get(key).toString();
                return string1;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    
  • 添加自定UDF的jar包(hive.aux.jars.path在该目录下的jar会在hive启动时自动加载)

    <property>
        <name>hive.aux.jars.path</name>
        <value>$HIVE_HOME/auxlib</value>
    </property>
    
    cp /home/hadoop-1.0-SNAPSHOT.jar $HIVE_HOME/auxlib/
    
  • 启动hive,创建临时函数

    hive>create temporary function ktv as "org.bigdata.hive.udf.FindValueByKey";
    
  • 测试是否添加好:

    show functions;
    select ktv("sex=1&hight=180&weight=130&sal=28000&facevalue=900","sex");
    
  • 确定无用时可以删除:

    drop temporary function ktv;
    

第三种:(当前session有效),类似于一个永久的

  • 在hive的安装目录的bin下创建一个文件:.hiverc

    vi ./hive/bin/.hiverc
    add jar /tempdataforhdfs_lds/hadoop-1.0-SNAPSHOT.jar;
    create temporary function ktv1 as "org.bigdata.hive.udf.FindValueByKey";
    
  • 重新启动hive

  • 测试是否添加好:

    show functions;
    select myfunc("1603");
    
  • 确定无用时可以删除:

    drop temporary function ktv1;
    

猜你喜欢

转载自blog.csdn.net/lds_include/article/details/88783945