栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 云计算 > 云平台

Hive 3.x|第六七天|DML数据操作

云平台 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hive 3.x|第六七天|DML数据操作

目录
  • 数据导入
    • 语法
    • 实操
    • 通过查询语句向表中插入数据
    • 查询语句中创建表并加载数据(As Select)
    • 创建表时通过 Location 指定加载数据路径
    • Import 数据到指定 Hive 表中
  • 数据导出
    • Insert 导出
    • Hadoop 命令导出到本地
    • Hive Shell 命令导出
    • Export 导出到 HDFS 上
    • 清除表中数据(Truncate)
  • 基本查询
    • 全表和特定列查询
    • Like 和 RLike
    • 满外连接
    • 多表连接
    • 全局排序
    • 每个 Reduce 内部排序(Sort By)
    • 分区(Distribute By)
    • Cluster By
    • 四个排序总结

数据导入 语法
load data [local] inpath '数据的 path' [overwrite] into table 
student [partition (partcol1=val1,…)];

(1)load data:表示加载数据
(2)local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表
(7)partition:表示上传到指定分区

实操
  1. 创建一张表:studetne 设定由t分割每行
create table student(id string, name string) row format delimited fields terminated by 't';
  1. 加载本地文件到 hive
load data local inpath '/opt/module/hive/datas/student.txt' into table default.student;

其中student.txt的内容为

1001	buyongyong
1002	xiaosu
1003	ban

  1. 或者上传文件到 HDFS
hive (default)> dfs -put /opt/module/hive/data/student.txt /user/hive/warehouse;

再加载HDFS上的数据

load data inpath '/user/hive/warehouse/student.txt' into table default.student;
  1. 加载数据覆盖表中已有的数据
load data inpath '/user/hive/warehouse/student.txt' overwrite into table default.student;
通过查询语句向表中插入数据
  1. 创建一张表
create table student_par(id int, name string) row format delimited fields terminated by 't';
  1. 基本插入数据
insert into table student_par values(1,'wangwu'),(2,'zhaoliu');
  1. 基本模式插入(根据单张表查询结果)
    从student里查出来,往student_par里放
insert overwrite table student_par select id, name from student where month='201709';
  1. 多表(多分区)插入模式(根据多张表查询结果)
 from student
 insert overwrite table student partition(month='201707')
 select id, name where month='201709'
 insert overwrite table student partition(month='201706')
 select id, name where month='201709';
查询语句中创建表并加载数据(As Select)

根据查询结果创建表(查询的结果会添加到新创建的表中)

create table if not exists student3 as select id, name from student;
创建表时通过 Location 指定加载数据路径
  1. 上传数据到 hdfs 上
hive (default)> dfs -mkdir /student;
hive (default)> dfs -put /opt/module/datas/student.txt /student;
  1. 创建表,并指定在 hdfs 上的位置
hive (default)> create external table if not exists student5(id int, name string) row format delimited fields terminated by 't' location '/student;
  1. 查询数据
hive (default)> select * from student5;
Import 数据到指定 Hive 表中

注意:先用 export 导出后,再将数据导入。

hive (default)> import table student2 from '/user/hive/warehouse/export/student';
数据导出 Insert 导出
  1. 将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/hive/data/export/student' select * from student;
  1. 将查询的结果格式化导出到本地
insert overwrite local directory  '/opt/module/hive/data/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' select * from student;
  1. 将查询的结果导出到 HDFS 上(没有 local)
hive (default)> insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY 't'  select * from student;
Hadoop 命令导出到本地
hive (default)> dfs -get /user/hive/warehouse/student/student.txt /opt/module/data/export/student3.txt;
Hive Shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

[yjr@hadoop102 hive]$ bin/hive -e 'select * from default.student;' > /opt/module/hive/data/export/student4.txt;
Export 导出到 HDFS 上

export 和 import 主要用于两个 Hadoop 平台集群之间 Hive 表迁移。

export table default.student to '/user/hive/warehouse/export/student';
清除表中数据(Truncate)
 truncate table student;
基本查询

感觉很多和SQL一模一样,一模一样的就全跳过了先。
注意:

  1. SQL 语言大小写不敏感。
  2. SQL 可以写在一行或者多行
  3. 关键字不能被缩写也不能分行
  4. 各子句一般要分行写。
  5. 使用缩进提高语句的可读性。
全表和特定列查询

全表查询

hive (default)> select * from emp;
hive (default)> select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp ;

选择特定列查询

hive (default)> select empno, ename from emp;
Like 和 RLike
  1. 使用 LIKE 运算选择类似的值
  2. 选择条件可以包含字符或数字:% 代表零个或多个字符(任意个字符);_ 代表一个字符。
  3. RLIKE 子句:RLIKE 子句是 Hive 中这个功能的一个扩展,其可以通过 Java 的正则表达式这个更强大的语言来指定匹配条件。
hive (default)> select * from emp where ename RLIKE '[A]';
满外连接

满外连接:将会返回所有表中符合 WHERe 语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用 NULL 值替代。

hive (default)> 
select e.empno, e.ename, d.deptno 
from
emp as e
full join dept as d
on e.deptno = d.deptno;
多表连接

注意:连接 n 个表,至少需要 n-1 个连接条件。例如:连接三个表,至少需要两个连接条件。

hive (default)>
SELECt e.ename, d.dname, l.loc_name
FROM emp e 
JOIN dept d
ON d.deptno = e.deptno 
JOIN location l
ON d.loc = l.loc;

大多数情况下,Hive 会对每对 JOIN 连接对象启动一个 MapReduce 任务。
本例中会首先启动一个 MapReduce job 对表 e 和表 d 进行连接操作,然后会再启动一个 MapReduce job 将第一个 MapReduce job 的输出和表 l;进行连接操作。
优化:当对 3 个或者更多表进行 join 连接时,如果每个 on 子句都使用相同的连接键的话,那么只会产生一个 MapReduce job。

全局排序

Order By:全局排序,只有一个 Reducer

每个 Reduce 内部排序(Sort By)

Sort By:对于大规模的数据集 order by 的效率非常低。在很多情况下,并不需要全局排序,此时可以使用 sort by。
Sort by 为每个 reducer 产生一个排序文件。每个 Reducer 内部进行排序,对全局结果集来说不是排序。

  1. 设置 reduce 个数:hive (default)> set mapreduce.job.reduces=3;
  2. 查看设置 reduce 个数:hive (default)> set mapreduce.job.reduces;
  3. 根据部门编号降序查看员工信息:hive (default)> select * from emp sort by deptno desc;
  4. 将查询结果导入到文件中(按照部门编号降序排序):
insert overwrite local directory 
'/opt/module/data/sortby-result'
select * from emp sort by deptno desc;
分区(Distribute By)
  • Distribute By: 在有些情况下,我们需要控制某个特定行应该到哪个 reducer,通常是为了进行后续的聚集操作。
  • distribute by 子句可以做这件事。distribute by 类似 MR 中 partition
    (自定义分区),进行分区,结合 sort by 使用。

对于 distribute by 进行测试,一定要分配多 reduce 进行处理,否则无法看到 distribute by 的效果。

案例:先按照部门编号分区,再按照员工编号降序排序。Hive 要求 DISTRIBUTE BY 语句要写在 SORT BY 语句之前

hive (default)> set mapreduce.job.reduces=3;
hive (default)> insert overwrite local directory '/opt/module/data/distribute-result' 
select * from emp distribute by deptno sort by empno desc;
Cluster By

当 distribute by 和 sorts by 字段相同时,可以使用 cluster by 方式。
cluster by 除了具有 distribute by 的功能外还兼具 sort by 的功能。但是排序只能是升序排序,不能指定排序规则为 ASC 或者 DESC。
故以下两种写法等价:

hive (default)> select * from emp cluster by deptno;
hive (default)> select * from emp distribute by deptno sort by deptno;

注意:按照部门编号分区,不一定就是固定死的数值,可以是 20 号和 30 号部门分到一个分区里面去。

四个排序总结

order by:只有一个Reducer,不能用于生产排序。
sort by:区内排序,单独使用时会随机划分所在区。
distribute by:帮助sort by进行分区,进行区内排序。
Cluster By:sort by和distribute by字段相同,且升序排列。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/896270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号