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