第1关:将文件中的数据导入到Hive表中
编程要求
student
表结构:
INFO | TYPE |
---|---|
Sno | INT |
name | STRING |
age | INT |
sex | STRING |
score | STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT> |
本地文件/home/student.txt
的内容为:
-
创建数据库
test1
; -
切换到
test1
数据库; -
在
test1
中创建相应格式的表student
(未分区),表结构如上所示,分隔符根据/home/student.txt
的内容设置; -
将
/home/student.txt
的数据导入到表student
中。
/********* Begin *********/
create database if not exists test1 location '/hive/test1';
use test1;
create table if not exists student(Sno INT,name STRING,age INT,sex STRING,score STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '-';
load data local inpath '/home/student.txt' overwrite into table student;
/********* End *********/
select * from student;
第2关:select操作
编程要求
test2
数据库中student
表结构为:
INFO | TYPE | COMMENT |
---|---|---|
Sno | INT | student sno |
name | STRING | student name |
age | INT | student age |
sex | STRING | student sex |
score | STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT> | student score |
表中的数据为:
-
切换到
test2
数据库; -
查询
student
表中所有的行和列; -
查询年龄
age > 17
的女生female
; -
查询语文成绩
Chinese > 90
的记录; -
从
student
表中查询前3
条记录; -
返回按年龄降序的前
2
条记录。
--Begin
use test2;
select * from student;
select * from student where age>17 and sex="female";
select * from student where score.Chinese>90;
select * from student limit 3;
select * from student sort by age desc limit 2;
--End
第3关:将select查询结果插入hive表中
编程要求
在test3
数据库中有student
表,表中数据如下:
Sno | name | age | sex | score(Chinese-Math-English) |
---|---|---|---|---|
001 | Xiaohong | 18 | female | 96-88-90.5 |
002 | Xiaoliang | 17 | male | 95-88-93.5 |
003 | Xiaoming | 19 | male | 86.5-98-91 |
004 | Xiaoguang | 18 | male | 88-80-94 |
005 | Xiaohua | 16 | female | 97-58.5-88 |
-
复制
student
表两份,分别名为:student2
、student3
(只复制表结构不复制数据,可参考:Hive表DDL操作(一)第二关) -
以覆盖插入的方式把
student
表中前两条数据插入到student2
中 -
以追加插入的方式把
student
表中前两条数据插入到student2
中 -
以覆盖插入的方式把
student
表中年龄大于17岁的数据插入到student2
、student3
中 -
以追加插入的方式把
student
表中的男生数据插入到student2
,以覆盖插入的方式把女生数据插入到student3
中
--Begin
--使用test3数据库
use test3;
--复制student表两份,分别名为:student2、student3
create table if not exists student2 like student;
create table if not exists student3 like student;
--以覆盖插入的方式把student表中前两条数据插入到student2中
insert overwrite table student2 select * from student limit 2;
--评测代码,勿删
select * from student2;
--以追加插入的方式把student表中前两条数据插入到student2中
insert into table student2 select * from student limit 2;
--评测代码,勿删
select * from student2;
--以覆盖插入的方式把student表中年龄大于17岁的数据插入到student2、student3中
from student
insert overwrite table student3 select * where student.age>17
insert overwrite table student2 select * where student.age>17;
--评测代码,勿删
select * from student2;
select * from student3;
--以追加插入的方式把student表中的男生数据插入到student2,以覆盖插入的方式把女生数据插入到student3中
from student
insert into table student2 select * where student.sex='male'
insert overwrite table student3 select * where student.sex='female';
--评测代码,勿删
select * from student2;
select * from student3;
--End
第4关:将select查询结果写入文件
编程要求
在test4
数据库中有student
表,表中数据如下:
Sno | name | age | sex | score(Chinese-Math-English) |
---|---|---|---|---|
001 | Xiaohong | 18 | female | 96-88-90.5 |
002 | Xiaoliang | 17 | male | 95-88-93.5 |
003 | Xiaoming | 19 | male | 86.5-98-91 |
004 | Xiaoguang | 18 | male | 88-80-94 |
005 | Xiaohua | 16 | female | 97-58.5-88 |
-
查询
student
表中的前两条数据写入到本地文件/home/test4
目录下 -
查询
student
表中男生的数据写入到本地文件/home/test4_1
目录下,女生的数据写入到本地文件/home/test4_2
目录下
--使用test4数据库
use test4;
--Begin
insert overwrite local directory '/home/test4' select * from student limit 2;
from student st
insert overwrite local directory '/home/test4_1' select * where st.sex='male'
insert overwrite local directory '/home/test4_2' select * where st.sex='female';
--End