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找 ...
随机推荐
- JAVA中的糕富帅技术——反射(一)
前言 突然发现好久没写博客了,前面写的都是关于Android的东西,今天心血来潮突然有一种冲动想写一篇基于JAVA技术的博客,别问我为什么?有钱.任性! 今天就来谈谈反射机制:学过JAVA的人不一定懂 ...
- Windows抓屏技术
Windows桌面共享中一些常见的抓屏技术 1. BitBlt 我想做Windows开发应该都知道这个API, 它能实现DC间的内容拷贝, 如果我们把源DC指定成Monitor DC或是桌面DC, ...
- 2018.4.25-ml笔记(梯度下降)
- JS 从斐波那契数列浅谈递归
一.前言 昨晚下班后,经理出于兴趣给我们技术组讲了讲算法相关的东西,全程一脸懵逼的听,中途还给我们出了一道比较有趣的爬楼问题,问题如下: 假设一个人从地面开始爬楼梯,规定一步只能爬一坎或者两坎,人只能 ...
- Ubuntu16.04安装后开发环境配置和常用软件安装
Ubuntu16.04安装后1.安装常用软件搜狗输入法+编辑器Atom+浏览器Chome+视频播放器vlc+图像编辑器GIMP Image Editor安装+视频录制软件RcordMyDesktop安 ...
- Spark集群的任务提交执行流程
本文转自:https://www.linuxidc.com/Linux/2018-02/150886.htm 一.Spark on Standalone 1.spark集群启动后,Worker向Mas ...
- 列表中文字太多 溢出使用省略号css方法
我们经常会遇到文字太多,而为了不打破原有布局,需要将多出文字用省略号代替,实现以下效果: 文字太太太太多多多啦...... 这个不多. html:这是个列表.ul/ol都行. <ul> & ...
- canvas动画效果新年祝福话语
html代码 <ul id="ul"></ul> css代码 * { margin:; padding:; } ul { list-style: none; ...
- pygame中模块说明
参考博客:https://blog.csdn.net/qq_27717921/article/details/53231762 pygame模块概览 1.display模块 功能:生成windows窗 ...
- cf1130E. Wrong Answer(构造)
题意 题目链接 Sol 对构造一无所知... 题解的方法比较神仙,,设第一个位置为\(-1\),\(S = \sum_{i=1}^n a_i\) 那么我们要让\(N * S - (N - 1) * ( ...