用java代码编写,继承UDF类即可
然后打包,导入hive中,再创建个函数名就能使用
hive: add jar '/路径/xxx.jar';
hive: create function 函数名 as '主类名';
以下用计算地球间两点的距离的为例
import org.apache.hadoop.hive.ql.exec.UDF;public class CalculateLength extends UDF {/*** 地球半径*/private static final double EARTH_RADIUS = 6378137;/*** 计算两个经纬度之间的距离** @param longi1 经度1* @param lati1 纬度1* @param longi2 经度2* @param lati2 纬度2* @return 距离*/public double evaluate(double longi1, double lati1, double longi2, double lati2) {double lat21 = lati1 * Math.PI / 180.0;double lat22 = lati2 * Math.PI / 180.0;double a = lat21 - lat22;double b = (longi1 - longi2) * Math.PI / 180.0;double sa2 = Math.sin(a / 2.0);double sb2 = Math.sin(b / 2.0);double d = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat21) * Math.cos(lat22) * sb2 * sb2));return Math.abs(d);}
}