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找 ...
随机推荐
- 【OSX】多个JDK共存时选择要使用的JDK版本
10.5以后的$JAVA_HOME没有被在.bash_profile中设置的话会被默认设置为 /usr/libexec/java_home. 如果一台mac里面安装了多个JDK, 可以通过/usr/l ...
- Android_自适应布局
1.屏幕相关概念1.1分辨率是指屏幕上有横竖各有多少个像素1.2屏幕尺寸指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸android将屏幕大小分为四个级别(smal ...
- OpenGL10-骨骼动画原理篇(1)
视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 本例程展示如何建立骨骼动画,有些人叫蒙皮动画 定义如下: 当前 ...
- iOS开发(1):设置APP的图标与启动图 | iOS图标的尺寸 | LaunchScreen的使用
每个APP都应该有自己的图标跟启动图. 这里介绍怎么设置iOS的APP的图标跟启动图. (1)图标 小程的xcode是10.0版本,设置图标的入口如下: 点击入口后,进到设置页面,如下: 可以看到有很 ...
- nginx介绍(六) - 通过反向代理实现跨域访问
前言 跨域访问问题, 相信很多人都遇到过, 并且都用不同的办法去解决过. 方法有很多种, 不一一叙述了. 这里主要使用nginx反向代理来解决跨域问题. 啥是跨域 假如你是百度开发人员, 在百度页面去 ...
- 简明awk教程(Simple awk tutorial)
整理翻译.原文地址:http://www.hcs.harvard.edu/~dholland/computers/awk.html 简明awk教程 为什么选awk? awk小巧.快速.简单.awk语言 ...
- ruby Enumerator::lazy
当一个很大的数组或集合需要做循环操作的时候,一次性把数据放到内存会有很大弊端.这时lazy就派上用场了.Float::INFINITY 是无穷大意思 举个例子 取出1到无穷大对7整除余数为0的前10个 ...
- Modifying namespace in XML document programmatically
Modifying namespace in XML document programmatically static XElement stripNS(XElement root) { return ...
- SpringMVC教程2
接上篇文章-SpringMVC教程1 五.基本操作 1.响应请求的方式 1.1ModeAndView /** * 查询方法 * @return */ @RequestMapping("/qu ...
- 搭建前端监控系统(三)NodeJs服务器部署篇
===================================================================== 监控系统预览地址: DEMO地址 GIT代码仓库地址 ...