1、下载需要的jar包,创建maven工程,配置相关的pom文件
2、编写java继承UDF类 方法名必须为evaluate()
import java.util.List;
import org.apache.hadoop.hive.ql.exec.UDF;
import com.ecs.accounts.bean.ChargePartyBean;
import com.ecs.accounts.util.TxtFileUtil;
public class CheckUDF extends UDF {
public String evaluate(String P_PAY_ORG_CODE, String P_PAY_CHANNEL_CODE, String fileurl) {
TxtFileUtil txtFile = new TxtFileUtil();
List<ChargePartyBean> listbean = txtFile.readTxtFile(fileurl);
for (ChargePartyBean bean : listbean) {
String V_UDP_LEVEL = null;
String V_MAP_CODE = null;
String V_FIRST_PARTY = null;
if (P_PAY_ORG_CODE == null || P_PAY_ORG_CODE.equals("")
&& (P_PAY_CHANNEL_CODE == null || P_PAY_CHANNEL_CODE.equals(""))) {
System.out.println("800");
return "800";
}
//System.out.println("org_code:" + bean.getUDP_CODE() + " udp_level:" + bean.getUDP_LEVEL());
if (P_PAY_ORG_CODE.equals(bean.getUDP_CODE())) {
V_UDP_LEVEL = bean.getUDP_LEVEL();
V_MAP_CODE = bean.getMAP_CODE();
if (V_UDP_LEVEL.equals("0")) {
System.out.println(bean.getMAP_CODE());
return V_MAP_CODE;
}
else if (V_UDP_LEVEL.equals("1")) {
V_FIRST_PARTY = V_MAP_CODE;
System.out.println("MAP_CODE:" + bean.getMAP_CODE());
for (ChargePartyBean listbe : listbean) {
if (listbe.getUDP_LEVEL().equals("2")
&& listbe.getUDP_CODE().toUpperCase().equals(P_PAY_CHANNEL_CODE.toUpperCase())) {
System.out.println(V_FIRST_PARTY + listbe.getMAP_CODE());
return V_FIRST_PARTY + listbe.getMAP_CODE();
}
}
}
}
}
System.out.println("999");
return "999";
}
3、打成jar包
4、将需要的fileurl.txt 文件和jar包上传到机子上。
5、进入hive命令
6、添加需要的依赖
ADD FILE FILEURL.TXT;
ADD JAR TEXT.JAR;
CREATE TEMPORARY FUNCTION TESTFUNCTION AS '执行的类路径';