好程序员大数据学习路线分享UDF函数

好程序员大数据学习路线分享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)、也能够继承 generic UDF,须要重写 initliaze() 、 getDisplay() 、 evaluate()
4.UDF的使用
第一种:(当前session有效)
package edu.qianfeng.UDF;java

import org.apache.hadoop.hive.ql.exec.UDF;程序员

/**apache

  • 使用Java 自定义UDF
  • @author lyd

*
*1603 1603_class
*/
public class FirstUDF extends UDF{json

//重写evaluate()
public String evaluate(String str){
    //判断
    if(str == null){
        return null;
    }
    return str+"_class";
}

}
一、添加自定UDF的jar包
hive>add jar /home/h2h.jar;
二、建立临时函数
hive>create temporary function myfunc as "edu.qianfeng.UDF.FirstUDF";
三、测试是否添加好:
show functions;
select myfunc("1603");
四、肯定无用时能够删除:
drop temporary function myfunc;
第二种:(当前session有效)
package edu.qianfeng.UDF;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONException;
import org.json.JSONObject;session

public class KeyToValue extends UDF{框架

public String evaluate(String str,String key){
    if(str == null || key == null){
        return null;
    }
    //sex=1&hight=180&weight=130&sal=28000
    //{sex:}
    String str1 = str.replace("=", ":");
    String str2 = str1.replace("&", ",");
    String str3 = "{"+str2+"}";
    String value = "";
    try {
        JSONObject jo = new JSONObject(str3);
        value = jo.get(key).toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return value;
}
public static void main(String[] args) {
    System.out.println(new KeyToValue().evaluate("sex=1&hight=180&weight=130&sal=28000&facevalue=900", "facevalue"));
}

}
一、添加自定UDF的jar包(hive.aux.jars.path在该目录下的jar会在hive启动时自动加载)
<property>函数

<name>hive.aux.jars.path</name>
<value>$HIVE_HOME/auxlib</value>

</property>
cp /home/h2h.jar $HIVE_HOME/auxlib/
二、启动hive,建立临时函数
hive>create temporary function ktv as "edu.qianfeng.UDF.KeyToValue";
三、测试是否添加好:
show functions;
select myfunc("1603");
四、肯定无用时能够删除:
drop temporary function myfunc;
第三种:(当前session有效)
一、建立一个初始化文件:
vi ./init-hive
add jar /home/h2h.jar;
create temporary function ktv1 as "edu.qianfeng.UDF.KeyToValue";
二、启动使用命令:
hive -i ./init-hive
三、测试是否添加好:
show functions;
select myfunc("1603");
四、肯定无用时能够删除:
drop temporary function myfunc;
第四种:(作成永久性)
package edu.qianfeng.UDF;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.hadoop.hive.ql.exec.UDF;oop

/**学习

  • 根据出生年月获取周岁
  • @author lyd
  • 2000-04-19 17
  • 2000-04-20 16
  • 2000-04-21 16

*
*/
public class BirthdayToAge extends UDF{测试

public static void main(String[] args) {
    System.out.println(new BirthdayToAge().evaluate("2000-04-20"));
}
public String evaluate(String birthday){
    if(birthday == null || birthday.trim().isEmpty()){
        return null;
    }
    String age = "";
    try {
        //获取出生时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取出生的时间戳
        long bithdayts = sdf.parse(birthday).getTime();
        //获取今天的时间戳
        long nowts = new Date().getTime();
        //获取从出生到如今有多少秒
        long alldays = (nowts - bithdayts)/1000/60/60/24;
        if(alldays < 0){
            return null;
        }
        //判断闰年
        int birthdayyear = Integer.parseInt(birthday.split("-")[0]);
        Calendar ca = Calendar.getInstance();
        int nowyear = ca.get(Calendar.YEAR);
        //循环找
        int rnday = 0;
        for (int i = birthdayyear; i < nowyear; i++) {
            if((i%400 == 0) || (i%4 == 0 && i%100 != 0)){
                rnday ++ ;
            }
        }
        //将闰年的额天数减掉
        age = (alldays-rnday)/365+"";
    } catch (ParseException e) {
        return null;
    }
    return age;
}

}须要对源码编译。 1)将写好的Jave文件拷贝到~/install/hive-0.8.1/src/ql/src/java/org/apache/hadoop/hive/ql/UDF/ cd ~/install/hive-0.8.1/src/ql/src/java/org/apache/hadoop/hive/ql/UDF/ls -lhgt |head2)修改~/install/hive-0.8.1/src/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java,增长import和RegisterUDFimport com.meilishuo.hive.UDF.UDFIp2Long; //添加importregisterUDF("ip2long", UDFIp2Long.class, false); //添加register3)在~/install/hive-0.8.1/src下运行ant -Dhadoop.version=1.0.1 packagecd ~/install/hive-0.8.1/srcant -Dhadoop.version=1.0.1 package4)替换exec的jar包,新生成的包在/hive-0.8.1/src/build/ql目录下,替换连接 cp hive-exec-0.8.1.jar /hadoop/hive/lib/hive-exec-0.8.1.jar.0628rm hive-exec-0.8.1.jarln -s hive-exec-0.8.1.jar.0628 hive-exec-0.8.1.jar5)重启进行测试

相关文章
相关标签/搜索