0、自定义函数类型
(1)UDF:user define function,输入单行,输出单行,类似于 format_number(age,'000')
(2)UDTF:user define table-gen function,输入单行,输出多行,类似于 explode(array);
(3)UDAF:user define aggr function,输入多行,输出单行,类似于 sum(xxx)
1、自定义UDF函数
(1)继承UDF类
(2)编写evaluate方法,方法名必须是"evaluate",返回值类型不固定,即方法最后返回值的类型
(3)必须添加Description描述,包括name(函数名)、value(desc方法时出现的提示)和extended(方法的扩展信息)
(4)将方法打包并发送到"/soft/hive/lib"目录下,重启hive
(5)注册函数:永久注册或临时注册
create function myudf as 'com.oldboy.hive.udf.MyUDF' //永久
create temporary function myudf as 'com.oldboy.hive.udf.MyUDF' //临时
(6)eg:使用自定义函数统计每个商家每个标签的总数
(6-0)虚列(lateral view):将一列炸开后,使其他字段能够自动补全
(6-1)数据格式如下
77287793 {"reviewPics":null,"extInfoList":[{"title":"contentTags","values":["服务热情","音响效果好"],"desc":"","defineType":0},{"title":"tagIds","values":["22","173"],"desc":"","defineType":0}],"expenseList":null,"reviewIndexes":[2],"scoreList":null}
(6-2)自定义函数代码
public class ParseJSON extends UDF {
public List<String> evaluate(String json) {
List<