大数据全系列 教程
1869个小节阅读:467.2k
目录
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建平台维度UDF自定义函数:
xxxxxxxxxx
package cn.itbaizhan.udf;
import cn.itbaizhan.tranformer.model.PlatformDimension;
import cn.itbaizhan.tranformer.service.IDimensionConverter;
import cn.itbaizhan.tranformer.service.impl.DimensionConverterImpl;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import java.io.IOException;
/**
* 操作平台dimension根据名称查询id的udf函数
* @author GTJin
*/
public class PlatformDimensionUDF extends GenericUDF {
private IDimensionConverter converter;
/**初始化方法
* @param arguments:传入到函数中的参数对应的类型的鉴别器对象
* @return 指定函数的返回值类型对象的鉴别器对象
* @throws UDFArgumentException
*/
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
converter = new DimensionConverterImpl();
//检验函数的参数个数是否正确:1个参数
if(arguments==null || arguments.length!=1){
throw new UDFArgumentException("参数的个数错误,不是1个参数!");
}
return PrimitiveObjectInspectorFactory.javaLongObjectInspector;
}
/**核心的计算方法
* @param arguments:传入到函数中的参数值
* @return 处理后的返回值
* @throws HiveException
*/
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
if(arguments[0].get()==null){
return 0L;
}
//获取参数
String platform = arguments[0].get().toString();
//构造平台维度对象
PlatformDimension dimension = new PlatformDimension(platform);
try {
long id = this.converter.getDimensionIdByValue(dimension);
return id;
} catch (IOException e) {
throw new RuntimeException("获取pd_id异常"+platform);
}
}
@Override
public String getDisplayString(String[] children) {
return "";
}
}