环境准备
创建表结构
drop table if exists student_info;
create table if not exists student_info(
stu_id string comment '学生ID',
stu_name string comment '学生姓名',
birthday string comment '出生日期',
sex string comment '性别'
)
row format delimited fields terminated by ','
stored as textfile;
drop table if exists course_info;
create table if not exists course_info(
course_id string comment '课程ID',
course_name string comment '课程名',
tea_id string comment '任课老师ID'
)
row format delimited fields terminated by ','
stored as textfile;
drop table if exists teacher_info;
create table if not exists teacher_info(
tea_id string comment '老师ID',
tea_name string comment '老师姓名'
)
row format delimited fields terminated by ','
stored as textfile;
drop table if exists score_info;
create table if not exists score_info(
stu_id string comment '学生ID',
course_id string comment '课程ID',
score int comment '成绩'
)
row format delimited fields terminated by ','
stored as textfile;
数据准备
(base) [link999@hadoop102 hive]$ mkdir test_data
vim student_info.txt
001,夏常安,1998-04-23,男
002,张保庆,1994-03-20,男
003,庄文杰,1999-09-21,男
004,林惊羽,1997-08-28,男
005,王凯莉,1996-08-12,女
vim course_info.txt
01,语文,1003
02,数学,1001
03,英语,1004
04,体育,1002
05,音乐,1002
vim teacher_info.txt
1001,张高数
1002,李体音
1003,王子文
1004,刘丽英
vim score_info.txt
001,01,94
002,01,74
004,01,85
005,01,64

加载数据
load data local inpath '/opt/module/hive/test_data/course_info.txt' into table course_info;
load data local inpath '/opt/module/hive/test_data/score_info.txt' into table score_info;
load data local inpath '/opt/module/hive/test_data/student_info.txt' into table student_info;
load data local inpath '/opt/module/hive/test_data/teacher_info.txt' into table teacher_info;
基础查询
简单查询
查询姓名中带“安”的学生名单
select * from student_info where stu_name like "%安%";

查询姓“王”老师的个数
select count(1)