hive udf&udaf说明
主要步骤有:
1.
添加
jar
到
hive session <BIP
中如果在hive-site.xml中定义了hive.aux.jars.path 属性的值,则可以省略这个步骤>
2. create temporary function
函数名
as ‘
引用的类名
’
3.
便可以在
select
中使用函数
BIP 现在开发的UDF(BIPPower)和UDAF(BIPCount,BIPSum).
假定已执行
create temporary function bippower as ‘'com.pplive.bip.hive.BIPSum';
create temporary function bipcount as 'com.pplive.bip.hive.BIPCount';
create temporary function bippower as 'com.pplive.bip.hive.BIPPower';
BIPPower:
针对
hive
自有的
power
函数做的更改
,
对于含有
where
条件的求幂。
例如:
Select power(a.col1,2) from a where a.col2 > 5,
等价于
Select bippower(a.col1, 2 ,a.col2, ”>”, 5) from a
。
<
参数依次是被求幂的表字段
,
指数
,
条件表字段
,
条件操作符
,
条件的值序列
>;
BIPCount:
针对
hive
自有的
count
函数做的更改
, ,
对于含有
where
条件的计数统计。
例如:
Select count(a.col1) from a where a.col2 > 5,
等价于
Select bipcount(a.col1, a.col2, ”>”, 5) from a
。
<
参数依次是计数统计的表字段
,
条件表字段
,
条件操作符
,
条件的值序列
>;
BIPSum:
针对
hive
自有的
sum
函数做的更改
, ,
对于含有
where
条件的求和统计。
例如:
Select sum(a.col1) from a where a.col2 > 5,
等价于
Select bipsum(a.col1, a.col2, ”>”, 5) from a
。
<
参数依次是求和统计的表字段
,
条件表字段
,
条件操作符
,
条件的值序列
>;
上面各个函数的例子仅仅使用了操作符号
“>”,
还支持
><,><=,>=<,>=<=,=,!=
以及
in,not in
等常见
hql
操作符。如
select bipsum(a.col1,a.col2,”>=<=”,5,10) from a
等于是
select sum(a.col1) from a where a.col2>=5 and a.col2<=10.<
注意
:
操作符号请将大于,或者大于等于放在前面,后面的值序列从小到大
>;
select bipsum(a.col1,a.col2,”=”,2,3,4,6,10) from a
相当于
select sum(a.col1) from a where a.col1 in (2,3,4,6,10);
select bipsum(a.col1,a.col2,”!=”,2,3,4,6,10) from a
相当于
select sum(a.col1) from a where a.col1 not in (2,3,4,6,10);
也都支持
distinct
操作
补充:目前bip中无需用户创建 temporary function ,且函数名都改成ifpower, ifcount, ifsum,还有新增udf urldecode
前面三个函数除了上述功能外,新增可以使用布尔表达式,如select ifcount(a.col1,a.col2>34) from a 等价于select(a.col1) from a where a.col2>34
select urldecode(a.col1) from a 默认使用utf-8解码,当然可以使用select urldecode(a.col1,charset) from a