大数据全系列 教程
1869个小节阅读:467.8k
目录
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
(datedimension &platformdimension )两个维度
修改pom.xml文件,添加依赖:
xxxxxxxxxx
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
新建包cn.itbaizhan.udf。
创建日期维度UDF自定义函数:
xxxxxxxxxx
package cn.itbaizhan.udf;
import cn.itbaizhan.common.DateEnum;
import cn.itbaizhan.tranformer.model.DateDimension;
import cn.itbaizhan.tranformer.service.IDimensionConverter;
import cn.itbaizhan.tranformer.service.impl.DimensionConverterImpl;
import cn.itbaizhan.util.TimeUtil;
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 相关的udf
* @author GTJin
*/
public class DateDimensionUDF 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 {
Object day = arguments[0].get();
if(day==null){
return 0L;
}
DateDimension dimension = DateDimension.buildDate(TimeUtil.parseString2Long(day.toString()), DateEnum.DAY);
try {
long id = this.converter.getDimensionIdByValue(dimension);
return id;
} catch (IOException e) {
throw new RuntimeException("获取dd_id异常" + day.toString());
}
}
@Override
public String getDisplayString(String[] children) {
return "";
}
}