一·定义一个可以通过指定格式字符串变成map,然后通过第二个参数作为key,返回key对应的value的方法getValue 示例:
name=zhangsan&age=18&habits=eat
package programmer.homework.day07.work.complex.map; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDF; import java.util.HashMap; import java.util.Map; @Description (name = "getValue",value = "get the value of the key",extended = "Extended: get the value of the key") public class GetValueUDF extends UDF { //定义一个Map类型的属性,用于接收解析出来的键和值 Map<String,String> infos = new HashMap<>(); /** hive在执行函数的时候会调用下面的方法 */ public String evaluate(String info,String key){ String[] splits = info.split("&"); for(String s : splits){ String[] keyValues = s.split("="); infos.put(keyValues[0],keyValues[1]); } return infos.get(key); } /** * 写个main方法用于测试代码逻辑 */ public static void main(String[] args) { String s = "name=zhangsan&age=18&habits=eat"; GetValueUDF getValueUDF = new GetValueUDF(); System.out.println(getValueUDF.evaluate(s,"name")); } }执行以上自定义函数的步骤 ①在idea中用maven打成jar包 ②将jar包上传到linux的某个目录下,比如/usr/local/hive/jar/xxx.jar ③进入hive客户端,将jar包添加到class Path路径下,即执行命令 add jar /usr/local/hive/jar/xxx.jar; ④在当前session创建一个临时函数:
create temporary function getValue as '自定义类的包名.类名';⑤使用select 执行函数
select getValue('name=zhangsan&age=18&habits=eat','name'); -- 返回zhangsan二.定义一个函数可以通过字符串表现形式的出生日期返回年龄
package programmer.homework.day07.work.complex.birth2; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public class AgeUDF extends UDF { private IntWritable age = new IntWritable(); public static void main(String[] args) { AgeUDF ageUDF = new AgeUDF(); System.out.println(ageUDF.evaluate(new Text("2012-01-12"))); } public IntWritable evaluate(Text date){ java.util.Date dateTime = null; try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateTime = simpleDateFormat.parse(date.toString()); Calendar calendar = Calendar.getInstance(); //当前时间的年月日 int nowYear = calendar.get(Calendar.YEAR); int nowMonth = calendar.get(Calendar.MONTH); int nowDay = calendar.get(Calendar.DAY_OF_MONTH); //将Calendar对象的时间设置为指定的出生日期 calendar.setTime(dateTime); //出生日期的年月日 int birthYear = calendar.get(Calendar.YEAR); int birthMonth = calendar.get(Calendar.MONTH); int birthDay = calendar.get(Calendar.DAY_OF_MONTH); //年龄 int yearDiff = nowYear - birthYear; //根据具体的当前时间的月和日判断是否过了生日 if(nowMonth > birthMonth){ age.set(yearDiff + 1); return age; } if(nowMonth == birthMonth){ if(nowDay >= birthDay){ age.set(yearDiff + 1); return age; } } } catch (ParseException e) { e.printStackTrace(); } return age; } }在hive客户端执行以上自定义函数:
-- 添加jar包到class Path路径下 add jar /usr/local/hive/jar/getage.jar; -- 在当前session创建临时函数 getage create temporary function getage as 'programmer.homework.day07.work.complex.birth2.AgeUDF'; -- 使用该函数 select getage('2012-12-12'); //返回年龄 -- 方法的参数可以是hive表中的一个字段,birthday是出生日期的字段名,student是表名 select birthday,getage(birthday) as age from student; -- 可以看到左边一列是出生日期,右边一列是年龄hive中自定义函数的加载方式 三种方式第一步都是把打好的jar包上传到linux服务器上,jar包的名称如果不好记,可以改个名 ①方式一,纯命令 在hive客户端中依次输入以下命令:
add jar jar包绝对路径; create temporary function functionName as '定义的函数的类全限定名'; -- 查看临时函数是否创建成功,能看到自己定义的函数说明创建成功,注意创建的函数只在当前shell窗口有效 show functions;②方式二,启动参数加载
-- 进入linux中的hive目录下(此时不是在hive客户端中) cd /usr/local/hive; vi ./hive-init; -- 在hive-init文件中添加以下两个命令 add jar jar包的路径; create temporary function functionName as '定义方法的类的全限定名'; -- wq保存后退出,执行以下命令,在启动hive客户端的同时,初始化hive-init文件 hive -i ./hive-init;③方式三,配置文件加载,通过这种方式,只要启动hive,就会加载这个函数
-- 进入linux系统中的hive目录下(此时不是在hive客户端中) cd /usr/local/hive; -- 进入./bin/.hiverc文件,即使没有会创建,加入两句命令 add jar jar包的路径; create temporary function functionName as '定义方法的类的全限定名'; -- 启动hive hive