Hive-1.2.1_05_案例操作
1. 建库建表
# 建库
create database exercise;
# 建表
create table student(Sno int,Sname string,Sex string,Sage int,Sdept string)
row format delimited fields terminated by ','; create table course(Cno int,Cname string)
row format delimited fields terminated by ','; create table sc(Sno int,Cno int,Grade int)
row format delimited fields terminated by ','; # 查看有哪些表
hive (exercise)> show tables;
OK
course
sc
student
Time taken: 0.033 seconds, Fetched: row(s)
2. 数据准备
相关数据
[yun@mini01 exercise]$ pwd
/app/software/hive/exercise
[yun@mini01 exercise]$ ll /app/software/hive/exercise/
total
-rw-rw-r-- yun yun Jul : course.dat
-rw-rw-r-- yun yun Jul : sc.dat
-rw-rw-r-- yun yun Jul : students.dat
[yun@mini01 exercise]$ cat /app/software/hive/exercise/course.dat
,数据库
,数学
,信息系统
,操作系统
,数据结构
,数据处理
[yun@mini01 exercise]$ cat /app/software/hive/exercise/sc.dat
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
[yun@mini01 exercise]$ cat /app/software/hive/exercise/students.dat
,李勇,男,,CS
,刘晨,女,,IS
,王敏,女,,MA
,张立,男,,IS
,刘刚,男,,MA
,孙庆,男,,CS
,易思玲,女,,MA
,李娜,女,,CS
,梦圆圆,女,,MA
,孔小涛,男,,CS
数据导入
# desc formatted course;
# desc formatted sc;
# desc formatted student;
# 查看location信息
# 数据导入
: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/course.dat' into table course; # 导入数据
INFO : Loading data to table exercise.course from file:/app/software/hive/exercise/course.dat
INFO : Table exercise.course stats: [numFiles=, totalSize=]
No rows affected (0.35 seconds)
: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/sc.dat' into table sc; # 导入数据
INFO : Loading data to table exercise.sc from file:/app/software/hive/exercise/sc.dat
INFO : Table exercise.sc stats: [numFiles=, totalSize=]
No rows affected (0.25 seconds)
: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/students.dat' into table student; # 导入数据
INFO : Loading data to table exercise.student from file:/app/software/hive/exercise/students.dat
INFO : Table exercise.student stats: [numFiles=, totalSize=]
No rows affected (0.381 seconds)
3. 常用操作
3.1. 学生表基本查询
查询学号为95001, 95005, 95008的学生信息
select * from student a where a.sno in(, , );
+--------+----------+--------+---------+----------+--+
| a.sno | a.sname | a.sex | a.sage | a.sdept |
+--------+----------+--------+---------+----------+--+
| | 李勇 | 男 | | CS |
| | 刘刚 | 男 | | MA |
| | 李娜 | 女 | | CS |
+--------+----------+--------+---------+----------+--+
rows selected (0.076 seconds)
查询学号中有9500字符串的学生信息
select * from student a where a.sno like '%9500%';
+--------+----------+--------+---------+----------+--+
| a.sno | a.sname | a.sex | a.sage | a.sdept |
+--------+----------+--------+---------+----------+--+
| | 李勇 | 男 | | CS |
| | 刘晨 | 女 | | IS |
| | 王敏 | 女 | | MA |
| | 张立 | 男 | | IS |
| | 刘刚 | 男 | | MA |
| | 孙庆 | 男 | | CS |
| | 易思玲 | 女 | | MA |
| | 李娜 | 女 | | CS |
| | 梦圆圆 | 女 | | MA |
+--------+----------+--------+---------+----------+--+
rows selected (0.081 seconds)
查询全体学生的学号与姓名
select a.Sno, a.Sname from student a;
+--------+----------+--+
| a.sno | a.sname |
+--------+----------+--+
| | 李勇 |
| | 刘晨 |
| | 王敏 |
| | 张立 |
| | 刘刚 |
| | 孙庆 |
| | 易思玲 |
| | 李娜 |
| | 梦圆圆 |
| | 孔小涛 |
+--------+----------+--+
rows selected (0.085 seconds)
查询学生的总人数
select count(distinct sno) count from student;
+--------+--+
| count |
+--------+--+
| |
+--------+--+
row selected (21.355 seconds)
3.2. 成绩表相关查询
查询选修了课程的学生姓名
# 表和表字段,大小写不明感 distinct 去重复
select distinct a.sno, a.sname from student a inner join sc b on a.sno = b.sno;
+--------+----------+--+
| a.sno | a.sname |
+--------+----------+--+
| | 李勇 |
| | 刘晨 |
| | 王敏 |
| | 张立 |
| | 刘刚 |
| | 孙庆 |
| | 李娜 |
| | 孔小涛 |
+--------+----------+--+
rows selected (32.694 seconds)
计算1号课程的学生平均成绩
select avg(a.grade) from sc a where a.cno = ;
+--------------------+--+
| _c0 |
+--------------------+--+
| 82.55555555555556 |
+--------------------+--+
row selected (20.72 seconds)
查询各科成绩平均分
select a.cno, avg(a.grade) from sc a group by a.cno;
+--------+--------------------+--+
| a.cno | _c1 |
+--------+--------------------+--+
| | 82.55555555555556 |
| | 83.5 |
| | 88.125 |
| | 78.57142857142857 |
| | 78.83333333333333 |
| | 90.8 |
+--------+--------------------+--+
rows selected (20.084 seconds)
查询选修1号课程的学生最高分数【查课程和分数,不要学生信息】
select a.cno, a.grade from sc a where a.cno = order by a.grade desc limit ;
+--------+----------+--+
| a.cno | a.grade |
+--------+----------+--+
| | |
+--------+----------+--+
row selected (19.005 seconds)
3.3. 函数查询
求各个课程号及相应的选课人数
select a.cno, count(a.sno) from sc a group by a.cno;
+--------+------+--+
| a.cno | _c1 |
+--------+------+--+
| | |
| | |
| | |
| | |
| | |
| | |
+--------+------+--+
rows selected (20.967 seconds)
查询选修了3门以上的课程的学生学号
select * from (select a.sno, count(a.cno) countCno from sc a group by a.sno) x where x.countCno > ;
或 select a.sno, count(a.cno) countCno from sc a group by a.sno having countCno > ;
或 select a.sno, count(a.cno) countCno from sc a group by a.sno having count(a.cno) > ;
+--------+-------------+--+
| x.sno | x.countcno |
+--------+-------------+--+
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+--------+-------------+--+
rows selected (19.556 seconds)
查询学生信息,结果按学号全局有序
select * from student a order by a.sno;
+--------+----------+--------+---------+----------+--+
| a.sno | a.sname | a.sex | a.sage | a.sdept |
+--------+----------+--------+---------+----------+--+
| | 李勇 | 男 | | CS |
| | 刘晨 | 女 | | IS |
| | 王敏 | 女 | | MA |
| | 张立 | 男 | | IS |
| | 刘刚 | 男 | | MA |
| | 孙庆 | 男 | | CS |
| | 易思玲 | 女 | | MA |
| | 李娜 | 女 | | CS |
| | 梦圆圆 | 女 | | MA |
| | 孔小涛 | 男 | | CS |
+--------+----------+--------+---------+----------+--+
rows selected (18.736 seconds)
查询学生信息,按性别分区,在分区内按年龄有序
# 设置map数量
: jdbc:hive2://mini01:10000> set mapreduce.job.reduces = 2;
No rows affected (0.007 seconds)
: jdbc:hive2://mini01:10000> set mapreduce.job.reduces;
+--------------------------+--+
| set |
+--------------------------+--+
| mapreduce.job.reduces= |
+--------------------------+--+
row selected (0.01 seconds)
### 或者 **************
: jdbc:hive2://mini01:10000> set mapred.reduce.tasks;
+-------------------------+--+
| set |
+-------------------------+--+
| mapred.reduce.tasks=- |
+-------------------------+--+
row selected (0.008 seconds)
: jdbc:hive2://mini01:10000> set mapred.reduce.tasks=2;
No rows affected (0.004 seconds)
: jdbc:hive2://mini01:10000> set mapred.reduce.tasks;
+------------------------+--+
| set |
+------------------------+--+
| mapred.reduce.tasks= |
+------------------------+--+
row selected (0.008 seconds)
##################################################################
# 具体操作
select * from student a distribute by a.sex sort by a.sage;
+--------+----------+--------+---------+----------+--+
| a.sno | a.sname | a.sex | a.sage | a.sdept |
+--------+----------+--------+---------+----------+--+
| | 刘刚 | 男 | | MA |
| | 孔小涛 | 男 | | CS |
| | 张立 | 男 | | IS |
| | 李勇 | 男 | | CS |
| | 孙庆 | 男 | | CS |
| | 梦圆圆 | 女 | | MA |
| | 李娜 | 女 | | CS |
| | 易思玲 | 女 | | MA |
| | 刘晨 | 女 | | IS |
| | 王敏 | 女 | | MA |
+--------+----------+--------+---------+----------+--+
rows selected (22.323 seconds)
3.4. 多表查询
查询每个学生及其选修课程的情况
select a.sno, a.sname, c.cno, c.cname from
student a inner join sc b on a.sno = b.sno
inner join course c on b.cno = c.cno
order by a.sno, c.cno;
+--------+----------+--------+----------+--+
| a.sno | a.sname | c.cno | c.cname |
+--------+----------+--------+----------+--+
| | 李勇 | | 数据库 |
| | 李勇 | | 数学 |
| | 李勇 | | 信息系统 |
| | 李勇 | | 操作系统 |
| | 刘晨 | | 数学 |
| | 刘晨 | | 信息系统 |
| | 刘晨 | | 操作系统 |
| | 刘晨 | | 数据结构 |
| | 王敏 | | 数据库 |
| | 王敏 | | 信息系统 |
| | 王敏 | | 数据结构 |
| | 张立 | | 数据库 |
| | 张立 | | 数学 |
| | 张立 | | 操作系统 |
| | 张立 | | 数据结构 |
| | 刘刚 | | 数据库 |
| | 刘刚 | | 数学 |
| | 刘刚 | | 信息系统 |
| | 刘刚 | | 数据处理 |
| | 孙庆 | | 数据库 |
| | 孙庆 | | 数学 |
| | 孙庆 | | 信息系统 |
| | 孙庆 | | 操作系统 |
| | 孙庆 | | 数据结构 |
| | 孙庆 | | 数据处理 |
| | 李娜 | | 数据库 |
| | 李娜 | | 信息系统 |
| | 李娜 | | 数据处理 |
| | 孔小涛 | | 数学 |
| | 孔小涛 | | 数据结构 |
| | 孔小涛 | | 数据处理 |
+--------+----------+--------+----------+--+
rows selected (26.356 seconds)
查询学生的得分情况
select a.sno, a.sname, c.cno, c.cname, b.grade from
student a inner join sc b on a.sno = b.sno
inner join course c on b.cno = c.cno
order by a.sno, c.cno;
+--------+----------+--------+----------+----------+--+
| a.sno | a.sname | c.cno | c.cname | b.grade |
+--------+----------+--------+----------+----------+--+
| | 李勇 | | 数据库 | |
| | 李勇 | | 数学 | |
| | 李勇 | | 信息系统 | |
| | 李勇 | | 操作系统 | |
| | 刘晨 | | 数学 | |
| | 刘晨 | | 信息系统 | |
| | 刘晨 | | 操作系统 | |
| | 刘晨 | | 数据结构 | |
| | 王敏 | | 数据库 | |
| | 王敏 | | 信息系统 | |
| | 王敏 | | 数据结构 | |
| | 张立 | | 数据库 | |
| | 张立 | | 数学 | |
| | 张立 | | 操作系统 | |
| | 张立 | | 数据结构 | |
| | 刘刚 | | 数据库 | |
| | 刘刚 | | 数学 | |
| | 刘刚 | | 信息系统 | |
| | 刘刚 | | 数据处理 | |
| | 孙庆 | | 数据库 | |
| | 孙庆 | | 数学 | |
| | 孙庆 | | 信息系统 | |
| | 孙庆 | | 操作系统 | |
| | 孙庆 | | 数据结构 | |
| | 孙庆 | | 数据处理 | |
| | 李娜 | | 数据库 | |
| | 李娜 | | 信息系统 | |
| | 李娜 | | 数据处理 | |
| | 孔小涛 | | 数学 | |
| | 孔小涛 | | 数据结构 | |
| | 孔小涛 | | 数据处理 | |
+--------+----------+--------+----------+----------+--+
rows selected (24.469 seconds)
查询选修2号课程且成绩在90分以上的所有学生
select a.sno, a.sname, b.cno, b.grade
from student a inner join sc b on a.sno = b.sno
where b.cno = and b.grade >= ;
+--------+----------+--------+----------+--+
| a.sno | a.sname | b.cno | b.grade |
+--------+----------+--------+----------+--+
| | 刘晨 | | |
| | 张立 | | |
| | 刘刚 | | |
| | 孔小涛 | | |
+--------+----------+--------+----------+--+
rows selected (16.728 seconds)
查询所有学生的信息,如果在成绩表中有成绩,则输出成绩表中的课程号和成绩
如果student的sno值对应的sc在中没有值,则会输出student.Sname null.如果用right out join会保留右边的值,左边的为null。
Join 发生在WHERE 子句之前。如果你想限制 join 的输出,应该在 WHERE 子句中写过滤条件——或是在join 子句中写。
select a.sno, a.sname, b.cno, b.grade
from student a left join sc b on a.sno = b.sno;
+--------+----------+--------+----------+--+
| a.sno | a.sname | b.cno | b.grade |
+--------+----------+--------+----------+--+
| | 李勇 | | |
| | 李勇 | | |
| | 李勇 | | |
| | 李勇 | | |
| | 刘晨 | | |
| | 刘晨 | | |
| | 刘晨 | | |
| | 刘晨 | | |
| | 王敏 | | |
| | 王敏 | | |
| | 王敏 | | |
| | 张立 | | |
| | 张立 | | |
| | 张立 | | |
| | 张立 | | |
| | 刘刚 | | |
| | 刘刚 | | |
| | 刘刚 | | |
| | 刘刚 | | |
| | 孙庆 | | |
| | 孙庆 | | |
| | 孙庆 | | |
| | 孙庆 | | |
| | 孙庆 | | |
| | 孙庆 | | |
| | 易思玲 | NULL | NULL |
| | 李娜 | | |
| | 李娜 | | |
| | 李娜 | | |
| | 梦圆圆 | NULL | NULL |
| | 孔小涛 | | |
| | 孔小涛 | | |
| | 孔小涛 | | |
+--------+----------+--------+----------+--+
rows selected (17.467 seconds)
查询与“王敏”在同一个系学习的学生
select a.sno, a.sname
from student a inner join student b
on a.sdept = b.sdept and b.sname = '王敏';
+--------+----------+--+
| a.sno | a.sname |
+--------+----------+--+
| | 王敏 |
| | 刘刚 |
| | 易思玲 |
| | 梦圆圆 |
+--------+----------+--+
rows selected (16.954 seconds)
4. 各种连接查询
4.1. 数据准备
[yun@mini01 exercise2]$ cat /app/software/hive/exercise2/a.dat
,a
,b
,c
,d
,y
,u
[yun@mini01 exercise2]$ cat /app/software/hive/exercise2/b.dat
,bb
,cc
,yy
,pp
4.2. 建表并导入数据
# 建表
# 使用的库和上面的一样,所以就不单独建库了
create table a(id int,name string)
row format delimited fields terminated by ','; create table b(id int,name string)
row format delimited fields terminated by ',';
# 导入数据
load data local inpath '/app/software/hive/exercise2/a.dat' into table a;
load data local inpath '/app/software/hive/exercise2/b.dat' into table b;
# 表数据查询
: jdbc:hive2://mini01:10000> select * from a;
+-------+---------+--+
| a.id | a.name |
+-------+---------+--+
| | a |
| | b |
| | c |
| | d |
| | y |
| | u |
+-------+---------+--+
rows selected (0.083 seconds)
: jdbc:hive2://mini01:10000> select * from b;
+-------+---------+--+
| b.id | b.name |
+-------+---------+--+
| | bb |
| | cc |
| | yy |
| | pp |
+-------+---------+--+
rows selected (0.071 seconds)
4.3. 各种各种join查询
select * from a inner join b on a.id=b.id;
select * from a left join b on a.id=b.id;
select * from a right join b on a.id=b.id;
select * from a full outer join b on a.id=b.id;
# 类似 inner join 但是只取左表的信息
select * from a left semi join b on a.id = b.id;
具体如下:
select * from a inner join b on a.id=b.id;
+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 7 | y | 7 | yy |
+-------+---------+-------+---------+--+
3 rows selected (17.714 seconds)
select * from a left join b on a.id=b.id;
+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 1 | a | NULL | NULL |
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 4 | d | NULL | NULL |
| 7 | y | 7 | yy |
| 8 | u | NULL | NULL |
+-------+---------+-------+---------+--+
6 rows selected (16.359 seconds)
select * from a right join b on a.id=b.id;
+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 7 | y | 7 | yy |
| NULL | NULL | 9 | pp |
+-------+---------+-------+---------+--+
4 rows selected (22.34 seconds)
select * from a full outer join b on a.id=b.id;
+-------+---------+-------+---------+--+
| a.id | a.name | b.id | b.name |
+-------+---------+-------+---------+--+
| 1 | a | NULL | NULL |
| 2 | b | 2 | bb |
| 3 | c | 3 | cc |
| 4 | d | NULL | NULL |
| 7 | y | 7 | yy |
| 8 | u | NULL | NULL |
| NULL | NULL | 9 | pp |
+-------+---------+-------+---------+--+
7 rows selected (24.746 seconds)
select * from a left semi join b on a.id = b.id;
+-------+---------+--+
| a.id | a.name |
+-------+---------+--+
| 2 | b |
| 3 | c |
| 7 | y |
+-------+---------+--+
3 rows selected (19.147 seconds)
Hive-1.2.1_05_案例操作的更多相关文章
- Arcgis案例操作教程——去掉Z值和M值
Arcgis案例操作教程--去掉Z值和M值 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 处理前 处理后: 处理方法 商务合作,科技咨 ...
- Hive的基本知识与操作
Hive的基本知识与操作 目录 Hive的基本知识与操作 Hive的基本概念 为什么使用Hive? Hive的特点: Hive的优缺点: Hive应用场景 Hive架构 Client Metastor ...
- 41、Hive数据源复杂综合案例
一.Hive数据源案例 1.概述 Spark SQL支持对Hive中存储的数据进行读写.操作Hive中的数据时,必须创建HiveContext,而不是SQLContext.HiveContext继承自 ...
- Hive、Spark优化案例
一.Join原则 将条目少的表/子查询放在Join的左边.原因:在Join的reduce阶段,位于Join左边的表的内容会被加载进内存,条目少的表放在左边,可以减少发生内存溢出的几率. 小表关联大表: ...
- Hive常用的SQL命令操作
Hive提供了很多的函数,可以在命令行下show functions罗列所有的函数,你会发现这些函数名与mysql的很相近,绝大多数相同的,可通过describe function functionN ...
- Playmaker 基础使用与案例操作
首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...
- hive数据库的哪些函数操作是否走MR
平时我们用的HIVE 我们都知道 select * from table_name 不走MR 直接走HTTP hive 0.10.0为了执行效率考虑,简单的查询,就是只是select,不带count, ...
- hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])
方案一:请参考<数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])>,该方案是sqlserver,orac ...
- hive元数据库表分析及操作
在安装Hive时,需要在hive-site.xml文件中配置元数据相关信息.与传统关系型数据库不同的是,hive表中的数据都是保存的HDFS上,也就是说hive中的数据库.表.分区等都可以在HDFS找 ...
随机推荐
- EOS生产区块:解析插件producer_plugin
producer_plugin是控制区块生产的关键插件. 关键字:producer_plugin,同步区块的处理,pending区块,生产区块,最后不可逆区块,生产循环,生产安排,水印轮次,计时器,确 ...
- C语言第六讲,数组
C语言第六讲,数组 一丶什么是数组 数组,就是一整块的连续内存空间. 且类型都是一样的.大小一样 比如: 1.1数组元素的访问 我们要访问数组,例如上面,我们访问元算2,元素3等等怎么访问.. 比如有 ...
- asp.net MVC 5 Scaffolding多层架构代码生成向导开源项目(邀请你的参与)
Visual Studio.net 2013 asp.net MVC 5 Scaffolding代码生成向导开源项目 提高开发效率,规范代码编写,最好的方式就是使用简单的设计模式(MVC , Repo ...
- 判断使用设备是PC还是phone
<script type="text/javascript"> //如果是手机设备,则.. if(/Android|webOS|iPhone|iPod|BlackBer ...
- SpringBoot入门之Thymeleaf的使用
在.net的MVC3 或更高版本等支持 Razor 的框架里使用cshtml,Razor是一种简单的编程语法,用于在网页中嵌入服务器端代码.在使用springboot开发mvc时也有与.net类似的视 ...
- 【转】 Apk文件及其编译过程
Apk文件概述 Android系统中的应用程序安装包都是以apk为后缀名,其实apk是Android Package的缩写,即android安装包. 注:apk包文件其实就是标准的zip文件,可以直接 ...
- 程序员快速掌握的UI设计技巧
一.概要 功能与内在很关键,UI与外表也重要. 1.1.选择主色调 1.1.1.三原色 三原色指色彩中不能再分解的三种基本颜色,我们通常说的三原色,即红.黄.蓝.三原色可以混合出所有的颜色,同时相加为 ...
- T-SQL基础(一)之简单查询
名词解释 SQL: Structured Query Language,结构化查询语言,是一种在关系型数据库中用于管理数据的标准语言.SQL是一种声明式编程语言,即只需表明需要什么而无需关注实现细节( ...
- org.apache.catalina.LifecycleException错误解决方案
1.org.apache.catalina.LifecycleException错误 一般是由于在tomcat中运行web应用时为所在的jvm分配的堆空间过小,具体错误截图如下所示: 2.为特定程序分 ...
- Spring Security OAuth2 SSO 单点登录
基于 Spring Security OAuth2 SSO 单点登录系统 SSO简介 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自 ...