常用函数
nvl(sal,0) 如果字段sal的值为null就返回0
concat(str1,str2,str3) 将三个字符串拼接起来
concat_ws("-",str1,str2,str3) 将三个字符串用"-“符号拼接起来
concat_ws(”-",arr1,arr2,arr3) 将数组内的元素用"-“符号拼接起来
split(“abc bcd def”,” “) 将字符串用空格切割开,并放入数组中
cast(“123”,int) 将字符串转换成int类型的数字
date_sub(“2020-09-04”,2) 从2020-09-04的基础上减两天
date_add(“2020-09-04”,2) 从2020-09-04的基础上加两天
substart(str,5) 取str字符串的前5个字符
if(gender,‘男’,1,0) 如果gender为"男"就返回1,如过不为"男”,就返回0
其他函数查看方式
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() split()
4)官方文档地址
https://cwiki.apache.org/confluence/display/Hive/HivePlugins
自定义UDF函数
1.创建一个Maven工程Hive
2.导入依赖
org.apache.hive
hive-exec
1.2.1
3.创建一个类
package com.doit.hive;
import org.apache.hadoop.hive.ql.exec.UDF;
public class Lower extends UDF {
public String evaluate (final String s) {
if (s == null) {
return null;
}
return s.toLowerCase();
}
}
4.打成demo.jar包上传到 linux: /doit/
5.将jar包添加到hive的classpath
hive (default)> add jar /doit/demo.jar;
6.创建临时函数与开发好的java class关联
hive (default)> create temporary function mf as "com.doit.hive.Lower ";
7.即可在hql中使用自定义的函数strip
hive (default)> select ename, mf (ename) lowername from emp;
永久函数的使用:
1. 把自定义函数的jar上传到hdfs中.
hdfs dfs -put lower.jar 'hdfs://path/to/hive_func';
2. 创建永久函数
hive> create function xxoo_lower as 'com._51doit.func.MyFunction' using
jar 'hdfs://path/to/hive_func/lower.jar'
3. 验证
hive> select xxoo_lower("Hello World");
hive> show functions;
永久函数的删除也容易:
hive> drop function xxoo_lower;
JDBC
jdbc是jdk开发的操作数据库的一套标准API(接口方法)
基本的开发步骤:
1 创建项目 导入jar包
2 注册驱动
3 获取连接
4 获取执行SQL语句的对象
5 sql 发送执行sql
6 获取结果数据
7 释放资源
创建maven项目添加依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc
数据库的驱动 连接的是何种数据库
-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
JAVAAPI
public class App {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
// 注册驱动
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
// 获取连接
Connection con = DriverManager.getConnection("jdbc:hive2://doit01:10000/demo", "root", "");
// 获取指定SQL的对象
Statement stmt = con.createStatement();
String sql = "select * from tb_case";
// 执行sql
ResultSet res = stmt.executeQuery(sql);
// 处理结果
if (res.next()) {
System.out.println(res.getString(1));
}
}
}