HQL (hive sql)之函数总结
1. 背景
- 作为操作hive客户端的编程语言,hql有自己的数据类型,自然也有编译器,也有函数。并且这个函数有内置,也允许自定义来扩展功能
- 注意,不管是普通hql语言还是函数,最终都是转换为mapreduce程序来运行。(很少部分例外,是一些sql语句可以优化为不使用mapreduce,直接读取文件获取结果即可)
2. hive定义函数
2.1. 查看系统自带函数
show functions;

2.2. 显示自带函数用法
desc function upper;

2.3. 详细显示自带函数用法
desc function extended upper;

2.4. 官方文档
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

2.5 函数分类
2.5.1 运算符优先级

2.5.2 关系运算,就是得出的结果是true或者false(特殊几个特定场景返回NULL)

2.5.3 数值运算

注意,在HQL中,并没有异常这种概念,包括数组越界,null空指针等,只会以返回null值或者其他方式来兼容这些异常,并不是像java程序一样直接终止程序
2.5.4 逻辑运算符

2.5.5 字符串操作符

2.5.6 复杂类型构造方法

2.5.7 复杂类型操作符

2.5.8 数学运算


2.5.9 集合函数

2.5.10 类型转换

2.5.11 日期函数


2.5.12 条件判断

2.5.13 字符串处理函数



2.5.14 遮罩函数(给值加一层面具,类似把身份证中间几位变成***,带上面具一样,敏感信息就不会直接显示出来)

2.5.15 非查询相关函数

2.5.16 聚合函数


2.5.17 打散函数(将数据打散为一个中间数据表集合)

3. 自定义函数
概述
- 当hive自带函数无法满足要求时,可以自行编写java代码,打包后上传,然后临时或者永久关联到hive中,这样就是在hive中使用自定义的函数。
- 自定义函数也是分为多类
- 一进一出,UDF(user defined function)
- 多进一出(UDAF user defined aggregation function)
- 一进多出(UDTF user defined table generating functions)
- 官方文档地址
https://cwiki.apache.org/confluence/display/Hive/HivePlugins

- 代码步骤
- 继承org.apache.hadoop.hive.ql.UDF
- 需要实现evaluate函数;evaluate函数支持重载;
- 在hive的命令行窗口创建函数
3.1. 添加jar
add jar linux_jar_path
3.2. 创建function,
create [temporary] function [dbname.]function_name AS class_name;- 在hive的命令行窗口删除函数
Drop [temporary] function [if exists] [dbname.]function_name;
UDF必须要有返回类型,可以返回null,但是返回类型不能为void;
实际操作
-
创建maven项目
-
导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hive_udf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<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>
<!-- 假设这是需要引入的第三方jar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!-- 假设这是需要引入的第三方jar包 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<!-- 将第三方jar包打进maven构建包中的插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 创建一个类(由于一个类中实现一个evaluate,所以如果需要实现多个自定义函数,需要创建多个类,分别实现对应逻辑)
import org.apache.hadoop.hive.ql.exec.UDF;
/**
* @author hulc
* @slogan: just do it
* @date 2020/9/6 21:23
*/
public class UserDefineFunction1 extends UDF {
// 随便实现一个方法,需要确定返回值和参数
public String evaluate(final String arg) {
if(arg == null) {
return null;
}
return arg.toLowerCase();
}
}
/**
* @author hulc
* @slogan: just do it
* @date 2020/9/6 21:23
*/
public class UserDefineFunction2 {
public int evaluate(final String arg) {
if(arg == null) {
return Integer.MIN_VALUE;
}
return Integer.parseInt(arg);
}
}
- 打包成jar包,上传到服务器linux节点上

先clean,再package

/root/jars/udf_demo.jar - 将jar包添加到hive的classpath,这样启动时就会一起加载
# 这个需要在hive 客户端中操作
add jar /root/jars/udf_demo.jar;

- 创建临时函数与开发好的java class 关联
# 这个需要在hive 客户端中操作
# 注意,这里需要给类的全类名,因为这里只是简单演示,没有创建多层包路径,所以没有com.xxx.xxx之类的包前缀路径
create temporary function mf1 as "UserDefineFunction1";
create temporary function mf2 as "UserDefineFunction2";
- 在hive sql中使用自定义函数
show functions;
select mf1("AHAHA");
select mf2("300");


永久自定义函数
- 把自定义函数的jar上传到hdfs中.
hdfs dfs -put lower.jar ‘hdfs:///path/to/hive_func’;- 创建永久函数
hive> create function xxoo_lower as ‘com._51doit.func.MyFunction’ using
jar ‘hdfs:///path/to/hive_func/lower.jar’- 验证
hive> select xxoo_lower(“Hello World”);
hive> show functions;
永久函数的删除也容易:
hive> drop function xxoo_lower;

本文总结了Hive SQL的内置函数,包括运算符优先级、关系运算、数值运算、字符串和复杂类型操作等,并介绍了自定义函数的创建、使用和删除方法,帮助理解HQL如何扩展功能。
1408

被折叠的 条评论
为什么被折叠?



