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.apache.commons.lang.math.NumberUtils","isNumber","123")

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM