天天看点

Hadoop学习(11)—— Apache Hive DML语句与函数使用

作者:技术闲聊DD

1 Hive SQL DML语法之加载数据

1.1 Hive SQL-DML-Load加载数据

1.1.1 背景

  • 在Hive中建表成功之后,就会在HDFS上创建一个与之对应的文件夹,且文件夹名字就是表名;
  • 文件夹父路径是由参数hive.metastore.warehouse.dir控制,默认值是/user/hive/warehouse;
  • 不管路径在哪里,只有把数据文件移动到对应的表文件夹下面,Hive才能映射解析成功;
  • 最原始暴力的方式就是使用hadoop fs –put|-mv等方式直接将数据移动到表文件夹下;
  • Hive官方推荐使用Load命令将数据加载到表中。

1.1.2 Load语法功能

  • 所谓加载是指:将数据文件移动到与Hive表对应的位置,移动时是纯复制、移动操作。
  • 纯复制、移动指在数据load加载到表中时,Hive不会对表中的数据内容进行任何转换,任何操作。

1.1.3 Load语法规则

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename;

  • filepath表示待移动数据的路径。可以指向文件(在这种情况下,Hive将文件移动到表中),也可以指向目录(在这种情况下,Hive将把该目录中的所有文件移动到表中)。
  • filepath文件路径支持下面三种形式,要结合LOCAL关键字一起考虑:相对路径,例如:project/data1 绝对路径,例如:/user/hive/project/data1 具有schema的完整URI,例如:hdfs://namenode:9000/user/hive/project/data1

1.1.4 语法规则之LOCAL

  1. 指定LOCAL, 将在本地文件系统中查找文件路径。
  • 若指定相对路径,将相对于用户的当前工作目录进行解释;用户也可以为本地文件指定完整的URI-例如:file:///user/hive/project/data1。
  1. 没有指定LOCAL
  • 如果filepath指向的是一个完整的URI,会直接使用这个URI; 如果没有指定schema,Hive会使用在hadoop配置文件中参数fs.default.name指定的(不出意外,都是HDFS)
  1. LOCAL本地是哪里?

    如果对HiveServer2服务运行此命令,本地文件系统指的是Hiveserver2服务所在机器的本地Linux文件系统,不是Hive客户端所在的本地文件系统。

1.1.5 举例

建表

--step1:建表
--建表student_local 用于演示从本地加载数据
create table student_local(num int,name string,sex string,age int,dept string) row format delimited fields terminated by ',';
--建表student_HDFS 用于演示从HDFS加载数据
create external table student_HDFS(num int,name string,sex string,age int,dept string) row format delimited fields terminated by ',';           
Hadoop学习(11)—— Apache Hive DML语句与函数使用

加载数据

--建议使用beeline客户端 可以显示出加载过程日志信息
--step2:加载数据
-- 从本地加载数据 数据位于HS2(master)本地文件系统 本质是hadoop fs -put上传操作
LOAD DATA LOCAL INPATH '/export/data/students.txt' INTO TABLE student_local;
select * from student_local;
--从HDFS加载数据 数据位于HDFS文件系统根目录下 本质是hadoop fs -mv 移动操作
--先把数据上传到HDFS上 hadoop fs -put /export/data/students.txt /
LOAD DATA INPATH '/students.txt' INTO TABLE student_HDFS;           
Hadoop学习(11)—— Apache Hive DML语句与函数使用
Hadoop学习(11)—— Apache Hive DML语句与函数使用
Hadoop学习(11)—— Apache Hive DML语句与函数使用
Hadoop学习(11)—— Apache Hive DML语句与函数使用

1.2 Hive SQL-DML-Insert插入数据

1.2.1 Insert语法功能

  • Hive官方推荐加载数据的方式:清洗数据成为结构化文件,再使用Load语法加载数据到表中。这样的效率更高。
  • 也可以使用insert语法把数据插入到指定的表中,最常用的配合是把查询返回的结果插入到另一张表中。

1.2.2 insert+select

  • insert+select表示:将后面查询返回的结果作为内容插入到指定表中。需要保证查询结果列的数目和需要插入数据表格的列数目一致。如果查询出来的数据类型和插入表格对应的列数据类型不一致,将会进行转换,但是不能保证转换一定成功,转换失败的数据将会为NULL。

INSERT INTO TABLE tablename select_statement1 FROM from_statement;

1.2.3 insert+select举例

--step1:创建一张源表student
drop table if exists student;
create table student(num int,name string,sex string,age int,dept string)
row format delimited
fields terminated by ',';
--加载数据
load data local inpath '/export/data/students.txt' into table student
select * from student;
--step2:创建一张目标表 只有两个字段
create table student_from_insert(sno int,sname string);
--使用insert+select插入数据到新表中
insert into table student_from_insert select num,name from student;
select * from student_from_insert;           

2 Hive SQL DML语法之查询数据

2.1 Hive SQL select语法介绍

SELECT [ALL | DISTINCT] select_expr, select_expr, ...

FROM table_reference

[WHERE where_condition]

[GROUP BY col_list]

[ORDER BY col_list] [LIMIT [offset,] rows];

注意:表名和列名不区分大小写。

准备数据

drop table if exists t_usa_covid19;
CREATE TABLE t_usa_covid19(
count_date string,
county string,
state string,
fips int,
cases int,
deaths int)
row format delimited fields terminated by ",";
--将源数据load加载到t_usa_covid19表对应的路径下
load data local inpath '/export/data/us-covid19-counties.dat' into table t_usa_covid19;
select * from t_usa_covid19;           

2.2 ALL DISTINCT 结果返回与去重

用于指定查询返回结果中重复的行如何处理。

  • 如果没有给出这些选项,则默认值为ALL(返回所有匹配的行)。
  • DISTINCT指定从结果集中删除重复的行。
--返回所有匹配的行
select state from t_usa_covid19;
--相当于
select all state from t_usa_covid19;
--返回所有匹配的行 去除重复的结果
select distinct state from t_usa_covid19;
--多个字段distinct 整体去重
select distinct county,state from t_usa_covid19;           

2.3 WHERE 过滤

--3、WHERE CAUSE
select * from t_usa_covid19 where 1 > 2; -- 1 > 2 返回false
select * from t_usa_covid19 where 1 = 1; -- 1 = 1 返回true
--找出来自于California州的疫情数据
select * from t_usa_covid19 where state = "California";
--where条件中使用函数 找出州名字母长度超过10位的有哪些
select * from t_usa_covid19 where length(state) >10 ;
--注意:where条件中不能使用聚合函数
-- --报错 SemanticException:Not yet supported place for UDAF ‘sum'
--聚合函数要使用它的前提是结果集已经确定。
--而where子句还处于“确定”结果集的过程中,因而不能使用聚合函数。
select state,sum(deaths) from t_usa_covid19 where sum(deaths) >100 group by state;
--可以使用Having实现
select state,sum(deaths) from t_usa_covid19 group by state having sum(deaths) > 100;           

2.4 聚合操作

聚合函数的最大特点是不管原始数据有多少行记录,经过聚合操作只返回一条数据,这一条数据就是聚合的结果。

Hadoop学习(11)—— Apache Hive DML语句与函数使用
--4、聚合操作
--统计美国总共有多少个县county
select count(county) from t_usa_covid19;
--统计美国加州有多少个县
select count(county) from t_usa_covid19 where state = "California";
--统计德州总死亡病例数
select sum(deaths) from t_usa_covid19 where state = "Texas";
--统计出美国最高确诊病例数是哪个县
select max(cases) from t_usa_covid19;           

2.5 GROUP BY 分组

GROUP BY语句用于结合聚合函数,根据一个或多个列对结果集进行分组;

--5、GROUP BY
--根据state州进行分组 统计每个州有多少个县county
select count(county) from t_usa_covid19 where count_date = "2021-01-28" group by state;
--想看一下统计的结果是属于哪一个州的
select state,count(county) from t_usa_covid19 where count_date = "2021-01-28" group by state;
--再想看一下每个县的死亡病例数,我们猜想很简单呀 把deaths字段加上返回 真实情况如何呢?
select state,count(county),deaths from t_usa_covid19 where count_date = "2021-01-28" group by state;
--很尴尬 sql报错了org.apache.hadoop.hive.ql.parse.SemanticException:Line 1:27 Expression not in GROUP BY key 'deaths'
--为什么会报错??group by的语法限制
--结论:出现在GROUP BY中select_expr的字段:要么是GROUP BY分组的字段;要么是被聚合函数应用的字段。
--deaths不是分组字段 报错
--state是分组字段 可以直接出现在select_expr中
--被聚合函数应用
select state,count(county),sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state;           

出现在GROUP BY中select_expr的字段:要么是GROUP BY分组的字段;要么是被聚合函数应用的字段。

2.6 HAVING 分组后过滤

  • 在SQL中增加HAVING子句原因是,WHERE关键字无法与聚合函数一起使用。
  • HAVING子句可以让我们筛选分组后的各组数据,并且可以在Having中使用聚合函数,因为此时where,group by已经执行结束,结果集已经确定。
--6、having
--统计2021-01-28死亡病例数大于10000的州
select state,sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" and sum(deaths) >10000 group by state;
--where语句中不能使用聚合函数 语法报错
--先where分组前过滤,再进行group by分组, 分组后每个分组结果集确定 再使用having过滤
select state,sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state having sum(deaths) > 10000;
--这样写更好 即在group by的时候聚合函数已经作用得出结果 having直接引用结果过滤 不需要再单独计算一次了
select state,sum(deaths) as cnts from t_usa_covid19 where count_date = "2021-01-28" group by state having cnts> 10000;           

HAVING与WHERE区别

  • having是在分组后对数据进行过滤
  • where是在分组前对数据进行过滤
  • having后面可以使用聚合函数
  • where后面不可以使用聚合函数

2.7 ORDER BY 排序

  • ORDER BY 语句用于根据指定的列对结果集进行排序。
  • ORDER BY 语句默认按照升序(ASC)对记录进行排序。如果您希望按照降序对记录进行排序,可以使用DESC关键字。
--7、order by
--根据确诊病例数升序排序 查询返回结果
select * from t_usa_covid19 order by cases;
--不写排序规则 默认就是asc升序
select * from t_usa_covid19 order by cases asc;
--根据死亡病例数倒序排序 查询返回加州每个县的结果
select * from t_usa_covid19 where state = "California" order by cases desc;           

2.8 LIMIT 返回条数限制

  • LIMIT用于限制SELECT语句返回的行数。
  • LIMIT接受一个或两个数字参数,这两个参数都必须是非负整数常量。
  • 第一个参数指定要返回的第一行的偏移量(从 Hive 2.0.0开始),第二个参数指定要返回的最大行数。当给出单个参数时,它代表最大行数,并且偏移量默认为0。
--8、limit
--没有限制返回2021.1.28 加州的所有记录
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California";
--返回结果集的前5条
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California" limit 5;
--返回结果集从第1行开始 共3行
select * from t_usa_covid19 where count_date = "2021-01-28" and state ="California" limit 2,3;
--注意 第一个参数偏移量是从0开始的           

3 Hive SQL Join关联查询

3.1 背景

join语法的出现是用于根据两个或多个表中的列之间的关系,从这些表中共同组合查询数据。

3.2 语法规则

在Hive中,使用最多,最重要的两种join分别是:inner join(内连接)、left join(左连接)。

3.3 inner join 内连接

  • 内连接是最常见的一种连接,它也被称为普通连接,其中inner可以省略:inner join == join ;
  • 只有进行连接的两个表中都存在与连接条件相匹配的数据才会被留下来。
--1、inner join
select e.id,e.name,e_a.city,e_a.street
from employee e inner join employee_address e_a
on e.id =e_a.id;
--等价于 inner join=join
select e.id,e.name,e_a.city,e_a.street
from employee e join employee_address e_a
on e.id =e_a.id;
--等价于 隐式连接表示法
select e.id,e.name,e_a.city,e_a.street
from employee e , employee_address e_a
where e.id =e_a.id;           

3.3 left join 左连接

  • left join中文叫做是左外连接(Left Outer Join)或者左连接,其中outer可以省略,left outer join是早期的写法。
  • left join的核心就在于left左。左指的是join关键字左边的表,简称左表。
  • 通俗解释:join时以左表的全部数据为准,右边与之关联;左表数据全部返回,右表关联上的显示返回,关联不上的显示null返回。
--2、left join
select e.id,e.name,e_conn.phno,e_conn.email
from employee e left join employee_connection e_conn
on e.id =e_conn.id;
--等价于 left outer join
select e.id,e.name,e_conn.phno,e_conn.email
from employee e left outer join employee_connection e_conn
on e.id =e_conn.id;           

4 Hive SQL中的函数使用

4.1 Hive 函数概述及分类标准

4.1.1 概述

  • Hive内建了不少函数,用于满足用户不同使用需求,提高SQL编写效率:使用show functions查看当下可用的所有函数;通过describe function extended funcname来查看函数的使用方式。

4.1.2 分类标准

  • Hive的函数分为两大类:内置函数(Built-in Functions)、用户定义函数UDF(User-Defined Functions):内置函数可分为:数值类型函数、日期类型函数、字符串类型函数、集合函数、条件函数等;用户定义函数根据输入输出的行数可分为3类:UDF、UDAF、UDTF。

4.1.3 用户定义函数UDF分类标准

根据函数输入输出的行数:

  • UDF(User-Defined-Function)普通函数,一进一出
  • UDAF(User-Defined Aggregation Function)聚合函数,多进一出
  • UDTF(User-Defined Table-Generating Functions)表生成函数,一进多出

UDF分类标准扩大化

UDF分类标准本来针对的是用户自己编写开发实现的函数。UDF分类标准可以扩大到Hive的所有函数中:包括内置函数和用户自定义函数。 因为不管是什么类型的函数,一定满足于输入输出的要求,那么从输入几行和输出几行上来划分没有任何问题。千万不要被UD(User-Defined)这两个字母所迷惑,照成视野的狭隘。

比如Hive官方文档中,针对聚合函数的标准就是内置的UDAF类型。

Hadoop学习(11)—— Apache Hive DML语句与函数使用

4.2 Hive 常用的内置函数

内置函数(build-in)指的是Hive开发实现好,直接可以使用的函数,也叫做内建函数。

4.2.1 String Functions 字符串函数

  • 字符串长度函数:length
  • 字符串反转函数:reverse
  • 字符串连接函数:concat
  • 带分隔符字符串连接函数:concat_ws
  • 字符串截取函数:substr,substring
------------String Functions 字符串函数------------
select length("itcast");
select reverse("itcast");
select concat("angela","baby");
--带分隔符字符串连接函数:concat_ws(separator, [string | array(string)]+)
select concat_ws('.', 'www', array('itcast', 'cn'));
--字符串截取函数:substr(str, pos[, len]) 或者 substring(str, pos[, len])
select substr("angelababy",-2); --pos是从1开始的索引,如果为负数则倒着数
select substr("angelababy",2,2);
--分割字符串函数: split(str, regex)
select split('apache hive', ' ')           

4.2.2 Date Functions 日期函数

----------- Date Functions 日期函数 -----------------
--获取当前日期: current_date
select current_date();
--获取当前UNIX时间戳函数: unix_timestamp
select unix_timestamp();
--日期转UNIX时间戳函数: unix_timestamp
select unix_timestamp("2011-12-07 13:01:03");
--指定格式日期转UNIX时间戳函数: unix_timestamp
select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
--UNIX时间戳转日期函数: from_unixtime
select from_unixtime(1618238391);
select from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');
--日期比较函数: datediff 日期格式要求'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'
select datediff('2012-12-08','2012-05-09');
--日期增加函数: date_add
select date_add('2012-02-28',10);
--日期减少函数: date_sub
select date_sub('2012-01-1',10);           

4.2.3 Mathematical Functions 数学函数

----Mathematical Functions 数学函数-------------
--取整函数: round 返回double类型的整数值部分 (遵循四舍五入)
select round(3.1415926);
--指定精度取整函数: round(double a, int d) 返回指定精度d的double类型
select round(3.1415926,4);
--取随机数函数: rand 每次执行都不一样 返回一个0到1范围内的随机数
select rand();
--指定种子取随机数函数: rand(int seed) 得到一个稳定的随机数序列
select rand(3);           

4.2.4 Conditional Functions 条件函数

主要用于条件判断、逻辑判断转换这样的场合。

-----Conditional Functions 条件函数------------------
--使用之前课程创建好的student表数据
select * from student limit 3;
--if条件判断: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
select if(1=2,100,200);
select if(sex ='男','M','W') from student limit 3;
--空值转换函数: nvl(T value, T default_value)
select nvl("allen","itcast");
select nvl(null,"itcast");
--条件转换函数: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
select case sex when '男' then 'male' else 'female' end from student limit 3;