admin 管理员组文章数量: 1087649
hive函数之~reflect函数
reflect函数可以支持在sql中调用java中的自带函数,秒杀一切udf函数。
使用java.lang.Math当中的Max求两列中最大值
创建hive表
create table test_udf(col1 int,col2 int) row format delimited fields terminated by ',';
准备数据并加载数据
cd /export/servers/hivedatas vim test_udf
1,2
4,3
6,4
7,5
5,6
加载数据
hive (hive_explode)> load data local inpath '/export/servers/hivedatas/test_udf' overwrite into table test_udf;
使用java.lang.Math当中的Max求两列当中的最大值
hive (hive_explode)> select reflect("java.lang.Math","max",col1,col2) from test_udf;
不同记录执行不同的java内置函数
创建hive表
hive (hive_explode)> create table test_udf2(class_name string,method_name string,col1 int , col2 int) row format delimited fields terminated by ',';
准备数据
cd /export/servers/hivedatas vim test_udf2
java.lang.Math,min,1,2
java.lang.Math,max,2,3
加载数据
hive (hive_explode)> load data local inpath '/export/servers/hivedatas/test_udf2' overwrite into table test_udf2;
执行查询
hive (hive_explode)> select reflect(class_name,method_name,col1,col2) from test_udf2;
判断是否为数字
使用apache commons中的函数,commons下的jar已经包含在hadoop的classpath中,所以可以直接使用。
使用方式如下:
select reflect("org.apachemons.lang.math.NumberUtils","isNumber","123")
本文标签: hive函数之reflect函数
版权声明:本文标题:hive函数之~reflect函数 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1700323149a396638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论