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_案例操作的更多相关文章

  1. Arcgis案例操作教程——去掉Z值和M值

    Arcgis案例操作教程--去掉Z值和M值 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 处理前 处理后: 处理方法     商务合作,科技咨 ...

  2. Hive的基本知识与操作

    Hive的基本知识与操作 目录 Hive的基本知识与操作 Hive的基本概念 为什么使用Hive? Hive的特点: Hive的优缺点: Hive应用场景 Hive架构 Client Metastor ...

  3. 41、Hive数据源复杂综合案例

    一.Hive数据源案例 1.概述 Spark SQL支持对Hive中存储的数据进行读写.操作Hive中的数据时,必须创建HiveContext,而不是SQLContext.HiveContext继承自 ...

  4. Hive、Spark优化案例

    一.Join原则 将条目少的表/子查询放在Join的左边.原因:在Join的reduce阶段,位于Join左边的表的内容会被加载进内存,条目少的表放在左边,可以减少发生内存溢出的几率. 小表关联大表: ...

  5. Hive常用的SQL命令操作

    Hive提供了很多的函数,可以在命令行下show functions罗列所有的函数,你会发现这些函数名与mysql的很相近,绝大多数相同的,可通过describe function functionN ...

  6. Playmaker 基础使用与案例操作

    首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...

  7. hive数据库的哪些函数操作是否走MR

    平时我们用的HIVE 我们都知道 select * from table_name 不走MR 直接走HTTP hive 0.10.0为了执行效率考虑,简单的查询,就是只是select,不带count, ...

  8. hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])

    方案一:请参考<数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])>,该方案是sqlserver,orac ...

  9. hive元数据库表分析及操作

    在安装Hive时,需要在hive-site.xml文件中配置元数据相关信息.与传统关系型数据库不同的是,hive表中的数据都是保存的HDFS上,也就是说hive中的数据库.表.分区等都可以在HDFS找 ...

随机推荐

  1. android开发(3):列表listview的实现 | 下拉刷新

    APP里面的列表太常用了,系统提供的listview或grideview可以做到.另外,我希望这个列表能够下拉时触发刷新,于是考虑使用封装了这个功能的开源项目,这里介绍这个: https://gith ...

  2. (转)深度学习目标检测指标mAP

    深度学习目标检测指标mAP https://github.com/rafaelpadilla/Object-Detection-Metrics 参考上面github链接中的readme,有详细描述

  3. zabbix 自定义监控项简单案例

    例如:获取被监控主机的登录用户数 以uptime为例: 输入命令:uptime | awk '{print $6}'  可以获得当前登录用户数(不通终端打印出的位置不同) 1.被监控主机修改zabbi ...

  4. 对python pickle的理解

    python 提供了pickle模块,能将对象进行序列化,将对象以文件形式存放在磁盘. 几乎所有的数据类型(列表,字典,集合,类等)都可以用pickle来序列化.但是序列化后的数据可读性很差. pic ...

  5. 详解@EnableWebMvc

    最近看了<Spring in Action>的开头,就被Spring注解开发(完全不写web.xml)惊叹了,也第一次知道了@EnableWebMvc是SpringMVC的注解 @Enab ...

  6. Angular2入门:TypeScript的类型 - 数组解构

  7. MySQL 事务机制

    事务处理是保证数据安全的重要机制,事务有四个重要属性 ,根据它们的英文名称可以记为ACID: 原子性(Atomic): 事务操作是不可分割的; 事务只存在已执行和未执行两种状态,不存在只执行了部分指令 ...

  8. 模块,import,from xxx import xxx

    一,模块 模块就是一个包含了python定义和声明的文件,文件名就是模块的名字加上.py后缀,总体来说,import加载的模块一共分成四个通用的类别: 1,使用python编写的py文件 2,已被变异 ...

  9. Sharepoint 2010 工作流状态值

    在Sharepoint2010中,如果要使用工作流状态值进行筛选,必须使用内部值,不能使用文字,要不然是筛选不出来的. 进行中:2 已取消:4 已批准:16 拒绝:17 下边是已取消的工作流状态:

  10. 无法初始化 PowerShell 主机解决方案

    Question无法初始化 PowerShell 主机.如果您的 PowerShell 执行策略设置设为 AllSigned,请先打开程序包管理器控制台以初始化该主机. --------------- ...