龙空技术网

Hive的UDF(用户自定义函数)开发

大数据与人工智能分享 172

前言:

眼前看官们对“hivepythonudf”大致比较着重,看官们都想要知道一些“hivepythonudf”的相关内容。那么小编同时在网摘上收集了一些关于“hivepythonudf””的相关内容,希望兄弟们能喜欢,看官们一起来学习一下吧!

*****根据日期判断属于哪个星座小demo*****

第一步:数据准备及创建hive表

1.准备数据

edward capriolo,edward@media6degrees.com,2-12-1981,209.191.139.200,M,10

bob,bob@test.net,10-10-2004,10.10.10.1,M,50

sara connor,sara@sky.net,4-5-1974,64.64.5.1,F,2

2.准备hive表

create table if not exists littlebigdata(

name string,

email string,

bday string,

ip string,

gender string,

anum int

)

row format delimited fields terminated by ',';

3.hive终端将准备数据导入hive表

load data local inpath 'littlebigdata.txt' into table littlebigdata;

第二步:代码开发

package cn.rtmap.bigdata.hive.testUDF.udf;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.hadoop.hive.ql.exec.UDF;import org.junit.Test;public class UDFZodiacSign extends UDF {    private SimpleDateFormat df;    public UDFZodiacSign() {        df = new SimpleDateFormat("MM-dd-yyyy");    }    public String evaluate(Date bday) {        return evaluate(bday.getMonth(), bday.getDay());    }    public String evaluate(String bday) {        Date date = null;        try {            date = df.parse(bday);        } catch (Exception ex) {            System.out.println("异常");            ex.printStackTrace();            return null;        }        return evaluate(date.getMonth() + 1, date.getDay());    }    public String evaluate(Integer month, Integer day) {        if (month == 1) {            if (day < 20) {                return "Capricorn";            } else {                return "Aquarius";            }        }        if (month == 2) {            if (day < 19) {                return "Capricorn";            } else {                return "Pisces";            }        }        if (month == 3) {            if (day < 20) {                return "Pisces";            } else {                return "Aries";            }        }        if (month == 4) {            if (day < 20) {                return "Aries";            } else {                return "Taurus";            }        }        if (month == 5) {            if (day < 20) {                return "Taurus";            } else {                return "Gemini";            }        }        if (month == 6) {            if (day < 21) {                return "Gemini";            } else {                return "Cancer";            }        }        if (month == 7) {            if (day < 22) {                return "Cancer";            } else {                return "Leo";            }        }        if (month == 8) {            if (day < 23) {                return "Leo";            } else {                return "Virgo";            }        }        if (month == 9) {            if (day < 22) {                return "Virgo";            } else {                return "Libra";            }        }        if (month == 10) {            if (day < 24) {                return "Libra";            } else {                return "Scorpio";            }        }        if (month == 11) {            if (day < 22) {                return "Scorpio";            } else {                return "Sagittarius";            }        }        if (month == 12) {            if (day < 22) {                return "Sagittarius";            } else {                return "Capricorn";            }        }        return null;    }    @Test    public void test() {        UDFZodiacSign aa = new UDFZodiacSign();        String str = aa.evaluate("01-10-2004");        System.out.println(str);    }}

第三步:导出jar包、加载jar包到hive中

1.hive终端将jar包导入到hive

add jar testUDF-0.0.1-SNAPSHOT.jar;

2.hive终端创建udf函数

create temporary function zodiac as "cn.rtmap.bigdata.hive.testUDF.udf.UDFZodiacSign";

3.使用udf函数查询hive中的数据

select name,bday,zodiac(bday) from littlebigdata;

效果:

标签: #hivepythonudf