1)查看系统自带的函数
hive> show functions;
2)显示自带的函数的用法
hive> desc function upper;
3)详细显示自带的函数的用法
hive> desc function extended upper;
1)Hive 自带了一些函数,比如:max/min 等,但是数量有限,自己可以通过自定义 UDF来方便的扩展。 2)当 Hive 提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UDF:user-defined function)。 3)根据用户自定义函数类别分为以下三种:
(1)UDF(User-Defined-Function)一进一出
(2)UDAF(User-Defined Aggregation Function)聚集函数,多进一出 类似于:count/max/min
(3)UDTF(User-Defined Table-Generating Functions) 一进多出 如 lateral view explore()
4)官方文档地址 https://cwiki.apache.org/confluence/display/Hive/HivePlugins 5)编程步骤
(1)继承 org.apache.hadoop.hive.ql.UDF
(2)需要实现 evaluate 函数;evaluate 函数支持重载;
(3)在 hive 的命令行窗口创建函数
a)添加 jar
add jar linux_jar_path
b)创建 function
create [temporary] function [dbname.]function_name AS
class_name;
(4)在 hive 的命令行窗口删除函数
Drop [temporary] function [if exists]
[dbname.]function_name;
6)注意事项 (1)UDF 必须要有返回类型,可以返回 null,但是返回类型不能为 void;
1)创建一个 Maven 工程 Hive 2)导入依赖
<dependencies>
<!--https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
3)创建一个类
public class Lower extends UDF {
public String evaluate (String s) {
if (s == null) {
return null;
}
return s.toLowerCase();
}
}
4)打成 jar 包上传到服务器/opt/module/datas/udf.jar 5)将 jar 包添加到 hive 的 classpath
hive (default)> add jar /opt/module/datas/udf.jar;
6)创建临时函数与开发好的 java class 关联
hive (default)> create temporary function mylower as
"com.atguigu.hive.Lower";
hive (default)> select ename, mylower(ename) lowername from emp;
1)需求说明 自定义一个 UDTF 实现将一个任意分割符的字符串切割成独立的单词,例如:
Line:"hello,world,hadoop,hive" Myudtf(line, ",")
hello
world
hadoop
hive
2)代码实现
public class MyUDTF extends GenericUDTF {
private ArrayList<String> outList = new ArrayList<>(); @Override
public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
//1.定义输出数据的列名和类型
List<String> fieldNames = new ArrayList<>(); List<ObjectInspector> fieldOIs = new ArrayList<>();
//2.添加输出数据的列名和类型fieldNames.add("lineToWord");
fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInsp ector);
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldName s, fieldOIs);
}
@Override
public void process(Object[] args) throws HiveException {
//1.获取原始数据
String arg = args[0].toString();
//2.获取数据传入的第二个参数,此处为分隔符String splitKey = args[1].toString();
//3.将原始数据按照传入的分隔符进行切分String[] fields = arg.split(splitKey);
//4.遍历切分后的结果,并写出
for (String field : fields) {
//集合为复用的,首先清空集合outList.clear();
//将每一个单词添加至集合
outList.add(field);
//将集合内容写出forward(outList);
outList.add(field);
//将集合内容写出forward(outList);
}
}
@Override
public void close() throws HiveException {
}
}
3)打成 jar 包上传到服务器/opt/module/data/udtf.jar 4)将 jar 包添加到hive 的 classpath 下
hive (default)> add jar /opt/module/data/udtf.jar;
5)创建临时函数与开发好的 java class 关联
hive (default)> create temporary function myudtf as "com.atguigu.hive.MyUDTF";
6)即可在hql 中使用自定义的函数
hive (default)> select myudtf(line, ",") word from words;