Apache Hive DML语句与函数使用
文章目录
- Apache Hive DML语句与函数使用
- 一、Hive SQL DML语法之**加载数据**
- (1). 掌握Hive SQL Load加载数据语句
- 什么是本地?
- (2).掌握Hive SQL Insert插入数据语句
- 二、Hive SQL DML语法之查询数据
- (1)掌握Hive SQL Select语法介绍
- (2)select_expr、ALL DISTINCT结束返回与去重
- (3)WHERE 过滤
- (4) 聚合操作
- (5) GROUP BY分组
- GROUP BY语法限制:
- (6)HAVING 分组后过滤
- (7) ORDER BY 排序
- (8)LIMIT限制
- (9)select执行顺序
- (10)HAVING和WHERE区别
- 三、Hive SQL Join关联查询
- (1).掌握Hive SQL Join查询语句
- inner join 内连接
- left join 左连接
- 四、Hive SQL 中的函数使用
- (1).掌握Hive SQL 常用函数的使用
- 常用内置函数
Apache Hive DML语句与函数使用
一、Hive SQL DML语法之加载数据
(1). 掌握Hive SQL Load加载数据语句
Load英文单词的含义为:加载、装载
所谓加载是指:将数据文件移动到Hive表对应的位置,移动是是纯复制(从本地加载)、移动操作(从HDFS)
纯复制、移动是指在数据load加载到表中时,Hive不会对表中数据内容进行任何转换、任何操作
什么是本地?
- 如果对hiveServer2服务运行此命令
即本地系统就是hive服务器所在的位置,源文件所在的位置,远程连接到的是node1的位置。
第一种方法:
--step1:建表
--建表student_local 用于演示从本地加载数据
create table student_local(num int,name string,sex string,age int,dept string) row format delimited fields terminated by ',';0: jdbc:hive2://master:10000> load data local inpath /root/hivedata/student.txt into table tyh.student_local;
---从结果可以看出是从本地文件
--Loading data to table tyh.student_local from file:/root/hivedata/students.txt0: jdbc:hive2://master:10000>select * from tyh.student_local;--建表student_HDFS 用于演示从HDFS加载数据
create table student_HDFS(num int,name string,sex string,age int,dept string) row format delimited fields terminated by ',';0: jdbc:hive2://master:10000>load data inpath /student.txt into table tyh.student_hdfs;---从结果可以看出是从hdfs文件
-- Loading data to table tyh.student_hdfs from hdfs://master:8020/students.txt0: jdbc:hive2://master:10000>select * from tyh.student_hdfs;
第二种方法:
[root@master ~]# mkdir hivedata
[root@master ~]# cd hivedata
[root@master ~]# vim 1.txt
[root@s3 ~]# /export/server/apache-hive-3.1.2-bin/bin/beeline0: jdbc:hive2://master:10000> create table t_1(id int,name string,year int)row format delimited fields terminated by ",";
[root@master hivedata]# hadoop fs -put 1.txt /user/hive/warehouse/t_1
0: jdbc:hive2://master:10000> select * from t_1;
+---------+-----------+-----------+
| t_1.id | t_1.name | t_1.year |
+---------+-----------+-----------+
| 1 | ttt | 11 |
| 2 | zhangsan | 12 |
| 3 | lisi | 13 |
+---------+-----------+-----------+
(2).掌握Hive SQL Insert插入数据语句
insert +select:将后面查询返回的结果作为内容插入到指定表中
- 需要保证查询结果列的数目和需要插入数据表格的列数目一致。
- 如果查询出来的数据类型和插入表格对应的列数据类型不一致,将会进行转换,但是不能保证转换一定成功,转换失败的数据将会为NULL
***
--step1:创建一张源表student
create table student(num int,name string,sex string,age int,dept string)
row format delimited fields terminated by ',';--step2:加载数据
load data local inpath '/root/hivedata/students.txt' into table student;select * from student;--step3:创建一张目标表 只有两个字段
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;
二、Hive SQL DML语法之查询数据
(1)掌握Hive SQL Select语法介绍
--创建表t_usa_covid19
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 '/root/hivedata/us-covid19-counties.dat' into table t_usa_covid19;---查询表内容
select * from t_usa_covid19;
(2)select_expr、ALL DISTINCT结束返回与去重
--1、select_expr
--查询所有字段或者指定字段
select * from t_usa_covid19;//技巧:选中表格按住crtl+Q 就会出现表的字段信息select county, cases, deaths from t_usa_covid19;
--查询常数返回 此时返回的结果和表中字段无关
select 1 from t_usa_covid19;
--查询当前数据库
select current_database(); --省去from关键字**2、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;//整体字段综合进行比较
(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 ;
(4) 聚合操作
- SQL中拥有很多可用于计数和计算的内建函数,其使用的语法是:SELECT functon(列) FROM表
- 聚合操作函数,如:COUTNT 、SUM、Max、Min、Avg等函数
- 聚合函数的最大特点是不管原始数据有多少行记录,经过聚合操着只返回一条数据,这一条数据就是聚合的结果
4、聚合操作
--统计美国总共有多少个县county
select county as tyh from t_usa_covid19;
--学会使用as 给查询返回的结果起个别名
select count(county) as county_cnts from t_usa_covid19;
--去重distinct
select count(distinct county) as county_cnts 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;
(5) GROUP BY分组
GPOUP BY语句用于聚合操作,根据一个或多个列队结果集进行分组
GROUP BY语法限制:
- 出现在GROUP BY中select_expr的字段:要么是GROUP BY分组字段;要么是在被聚合函数应用的字段
- 原因:避免出现一个字段多个值的歧义
- 分组字段 出现select_expr中,一定没有歧义,因为就是基于该字段分组的,同一组中必相同
- 被聚合函数应用的字段,也没有歧义,因为聚合函数的本质就是多进一出,最终返回一个结果
--被聚合函数应用
select *
from t_usa_covid19;--根据state州进行分组 统计每个州有多少个县county
select count(county) from t_usa_covid19 where count_date = "2021-01-28" group by state;--想看一下统计的结果是属于哪一个州的
select state,count(county) as county_nums from t_usa_covid19 where count_date = "2021-01-28" group by state;
--再想看一下每个县的死亡病例数
select state,count(county),sum(deaths) from t_usa_covid19 where count_date = "2021-01-28" group by state;
(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;
(7) ORDER BY 排序
ORDER BY语句根据指定的列对结果集进行排序
ORDER BY默认按照升序(ASC)对记录进行排序。如果您希望按照降序对记录进行排序,可以使用DESC关键字
--7、order by--根据确诊病例数升序排序 查询返回结果select * from t_usa_covid19 ;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;
(8)LIMIT限制
- LIMIT用于限制SELECT语句返回的行数
- LIMIT接受一个或两个数字参数,这两个参数都必须是非负整数常量
- 第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。当给出单个参数时,它代表最大行数,并且偏移量为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开始的
(9)select执行顺序
- 在查询过程中执行顺序:from>where>group(含聚合)>having>order>select
- 聚合语句(sum,min,max,avg,count)要比having子句优先执行
- where子句在查询过程中执行优先级别优先于聚合语句(sum,min,max,avg,count)
--执行顺序
select state,sum(deaths) as cnts from t_usa_covid19
where count_date = "2021-01-28"
group by state
having cnts> 10000
limit 2;
(10)HAVING和WHERE区别
having是在分组后对数据进行过滤
where实在分组前对数据进行过滤
having后面可以使用聚合函数
where后面不可以使用聚合函数
三、Hive SQL Join关联查询
(1).掌握Hive SQL Join查询语句
join语法的出现是用于根据两个或多个表中列之间的关系,从这些表中共同组合查询数据
在Hive中,使用最多,最重要的两种join分别是:
inner join(内连接)、left join(左连接)
--准备环境
--table1: 员工表
CREATE TABLE employee(id int,name string,deg string,salary int,dept string) row format delimited
fields terminated by ',';--table2:员工家庭住址信息表
CREATE TABLE employee_address (id int,hno string,street string,city string
) row format delimited
fields terminated by ',';--table3:员工联系方式信息表
CREATE TABLE employee_connection (id int,phno string,email string
) row format delimited
fields terminated by ',';--加载数据到表中
load data local inpath '/root/hivedata/employee.txt' into table employee;
load data local inpath '/root/hivedata/employee_address.txt' into table employee_address;
load data local inpath '/root/hivedata/employee_connection.txt' into table employee_connection;select *
from employee;select *
from employee_address;select *
from employee_connection;
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//给employee取别名为e
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;
left join 左连接
- left 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;
四、Hive SQL 中的函数使用
(1).掌握Hive SQL 常用函数的使用
概述
- Hive内建了不少函数,用于满足用户不同使用需求,提高SQL编写效率:
1.使用show functions查看当下可用的所有函数;
2.通过describe function extended funcname来查看函数的使用方式。
show functions;
describe function extended count;
分类标准
- Hive的函数分为两大类:内置函数( Built-in Functions )、用户定义函数UDF ( User-Defined Functions )
1.内置函数可分为 :数值类型函数、日期类型函数、字符串类型函数、集合函数、条件函数等;
2.用户定义函数根据输入输出的行数可分为3类: UDF、UDAF、 UDTF。
用户定义函数UDF分类标准
- 根据函数输入输出的行数 :
UDF ( User-Defined-Function )普通函数,一进一出
UDAF ( User-Defined Aggregation Function )聚合函数,多进一出
UDTF ( User-Defined Table-Generat ing Functions )表生成函数,一进多出
常用内置函数
------------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);--结果为:从第二个开始起的共二个 结果为ng
--分割字符串函数: split(str, regex)
--split针对字符串数据进行切割 返回是数组array 可以通过数组的下标取内部的元素 注意下标从0开始的
select split('apache hive', ' ');
select split('apache hive', ' ')[0];
select split('apache hive', ' ')[1];----------- 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);----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);-----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;
Apache Hive DML语句与函数使用相关推荐
- Hive DML 语句操作报错
按照MySQL 方式创建的表, hive> desc tt; OK id int name string age int Time taken: 0.123 seconds, Fetched: ...
- hive 补0_Hive常见语句及函数
原标题:Hive常见语句及函数 一.数据库 1 创建库语句 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_commen ...
- 4.1 数据仓库基础与Apache Hive入门
数据仓库基础与Apache Hive入门 一.数据仓库基本概念 1.数据仓库概念 2. 案例:数据仓库为何而来 (1)业务数据的存储问题 (2)分析型决策的制定 3.数据仓库主要特征 面向主题性(Su ...
- apache hive 使用概述
2019独角兽企业重金招聘Python工程师标准>>> 1 产品概述 1.1 产品功能 Apache Hive数据仓库软件提供对存储在分布式中的大型数据集的查询和管 ...
- Hive数据连接与函数(2)
1 数据连接 内连接:保留左表和右表连接成功的数据信息,连接未成功则不保留该数据 select * from hive_day03.orders as o join users u on o.user ...
- Apache Hive实战基础
本节目标 了解Hive的作用和优势 了解Hive的基本架构 了解Hive的数据类型 了解Hive的数据库和表操作 理解Hive的数据分区 理解Hive的数据分桶 4.1 数据仓库 4.1.1 为什么要 ...
- Apache Hive入门:模拟实现Hive功能、Hive架构、 组件
一.Apache Hive概述 什么是Hive Apache Hive是一款建立在Hadoop之上的开源数据仓库系统,可以将存储在Hadoop文件中的结构化.半结构化数据文件映射为一张数据库表, 基 ...
- beeline执行sql语句_由“Beeline连接HiveServer2后如何使用指定的队列(Yarn)运行Hive SQL语句”引发的一系列思考...
背景 我们使用的HiveServer2的版本为0.13.1-cdh5.3.2,目前的任务使用Hive SQL构建,分为两种类型:手动任务(临时分析需求).调度任务(常规分析需求),两者均通过我们的We ...
- Apache Ignite与Apache Hive的个人理解与总结
首先,贴一下官网链接辟邪:官网链接 下面,就看我强行总结吧,如果理解有误,请大佬及时指正,感激不尽! Apache Ignite是啥玩意,在上一篇已经讲过了,可能还比较易于理解:理解Ignite传送门 ...
最新文章
- EXP的flashback_scn和flashback_time
- 【转载】数字签名是什么?
- OpenGL:画三角形程序详解笔记
- C++学习之路 | PTA乙级—— 1011 A+B 和 C (15分)(精简)
- 高端大气上档次的官网介绍导航页源码
- 动态规划——最大子矩阵(hdu1081)
- 魅族官宣好消息:魅族16s Pro暮光森林终于要来了
- python安装教程-最新Python安装图文教程[很详细]
- html5 audio 资源,HTML5 Audio(音频)
- 软考高项3门45分总结及分享
- 新东方 计算机英语,常用计算机英语词汇:DRAM
- 机器学习-马尔可夫随机场(MRF)
- 树莓派添加开机自启动
- 减少资源占用 Vista操作系统瘦身大法
- 计算机硕士研究生毕设选题方向推荐 - 题目推荐
- Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!
- uniapp使用百度地图
- php里用钢笔画曲线,ps钢笔工具怎么抠图
- 分频电路设计(笔记)
- python2和pytho3切换_电脑上同时安装Python2和Pytho
热门文章
- QQradio 2.0 官方软件下载...【里面超多的无线音乐电台、英语电台】
- 清华大学 老师 一席话 [转]
- 【移动硬盘】Ubuntu20.04 移动硬盘不能访问
- ORACLE 创建视图时,出现权限不足 ORA-01031:权限不足
- 【报告分享】WEIQ红人营销平台双11红人营销报告-WEIQ(附下载)
- 小狗电器发布新品卧吸S9 AWE演绎碾压级最强吸力
- Android开发中混淆后Gson序列化、反序列化的那些坑,androidstudio配置
- 3Dgame_homework8
- 外嫁女能否享受村民拆迁补偿待遇
- Matlab激光雷达相机联合标定经验分享