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")