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. .NET内存管理、垃圾回收

    1. Stack和Heap    每个线程对应一个stack,线程创建的时候CLR为其创建这个stack,stack主要作用是记录函数的执行情况.值类型变量(函数的参数.局部变量 等非成员变量)都分配 ...

  2. Spring Cloud简介

    一.本文介绍 Web应用由最早的单体应用发展成为集群式的部署,再到现在的分布式系统.尤其是这两年分布式相关的技术发展的很快,一方面是以Dubbo为代表的,另一方面则是以Spring Cloud系列为代 ...

  3. 逆向知识之CS辅助/外挂专题.2.实现CS1.6透视原理

    逆向知识之CS辅助/外挂专题.2.实现CS1.6透视原理 一丶透视简介 我们涉及到FPS游戏.免不了说透视.自瞄什么的. 在CS1.6中. 有OpenGl.也有D3D. 透视的方法很多. gl透视(也 ...

  4. 【杂谈】线程中断——Interrupt

    前言 以前有一个错误的认识,以为中断操作都会抛出异常,后来才发现并不是这样,所以今天就来做一个关于中断的总结. 如何关闭线程 已被弃用的Stop方法 早期,Thread类中有一个stop方法,用于强行 ...

  5. 反射的所有api

    Extension [ extension #17 Reflection version $Id: 1cf65cee164ed57874ce2d29e5c46b82f6139524 $ ] { - C ...

  6. lightswitch binding custom control

    Listing 1: Setting up data binding for the Rating control usingSystem.Windows.Controls;usingSystem.W ...

  7. FLV 封装格式解析

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10662941.html FLV (Flash Video) 是由 Adobe 公司推出的 ...

  8. Java基础——Oracle(六)

    一.数据字典和动态性能视图 数据字典: oracle中的重要组成部分,提供了数据库的一些系统信息,记录了数据库的系统信息,它是只读表和视图的集合,数据字典的所有者为 sys 用户.用户只能在数据字典上 ...

  9. ubuntu安装docker以及基本用法

    ubuntu安装docker以及基本用法 一.安装 安装前先更新apt-get源到最新版本 apt-get update 使用ubuntu自带的docker安装包安装docker apt-get in ...

  10. JqGrid: Add,Edit,Del in asp.net

    https://github.com/1rosehip/jplist https://github.com/free-jqgrid/jqGrid https://plugins.jquery.com/ ...