Hive 外部表的練習
hive創建庫和表操作
hive加載數據,4種發放
1.數據放在Linux創建表結構 進行加載
2.先定義表結構、將一個查詢select語句結果插入到表中
3.數據先放在hdfs \ 創建表結構\ 進行加載(hive 只能加載目標文件的上級目錄)
4.外部數據 external 內部表和外部表 使用上沒有任何區別,刪除時則有差別數據:
創建表,以及添加數據:
create external table if not exists my_course(
courseid string,
couresename string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_course';
create external table if not exists my_source(
userid string
courseid string,
score string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_sourcet'
create external table if not exists my_student(
userid string
name string
sex string
age string,
xi string
)
row format delimited fields terminated by ','
stored as textfile
location '/hive/my_student'

1.問題:查詢全體學生的學號與姓名
select
userid,name
from my_student;
2.問題:查詢選修了課程的學生姓名和課程名稱
select student.name,t.couresename
from(
select course.couresename couresename,score.userid userid
from my_score score,my_course course
where score.courseid=course.courseid) t,my_student student
where t.userid=student.userid;
3.問題:查詢每個選修的課程共選了多少人
select t.couresename,count(*) num
from(
select course.couresename couresename,score.userid userid
from my_score score,my_course course
where score.courseid=course.courseid) t,my_student student
where t.userid=student.userid
group by t.couresename
order by num desc
limit 3;
4.問題:查詢學生的總人數
select count(distinct(userid))
from my_student;
5.問題:計算數據庫課程的學生平均成績
select course.couresename,avg(score.score)
from my_score score,my_course course
where course.couresename='數據庫' and score.courseid=course.courseid
group by course.couresename;
6.問題:查詢選修數學課程的學生最高分數
select max(score.score)
from my_score score,my_course course
where course.couresename='數學' and score.courseid=course.courseid;
7.問題:查詢選修了3門以上的課程的學生姓名
select student.name,t.num
from(
select userid,count(*) num
from my_score
group by userid ) t,my_student student
where t.userid =student.userid and t.num>=3;
8.問題:按照年齡排序並直接輸出到不同的文件中
create table if not exists result2(
userid string,
name string,
sex string,
age string,
xi string
)
row format delimited fields terminated by ','
stored as textfile;
insert into result2
select *
from my_student
order by age desc;
9.問題:查詢學生的得分情況。
select student.name,score.score
from my_score score,my_student student
where score.userid=student.userid;
10.問題:查詢選修信息系統課程且成績在90分以上的所有學生。
select distinct(t2.name)
from(
select score.userid userid
from my_score score,my_course course
where score.score>=90 and score.courseid=course.courseid) t1,my_student t2
where t1.userid=t2.userid;
11.問題:查詢與“劉晨”在同一個系學習的其他學生
select t3.name
from(
select t2.name name
from my_student t1,my_student t2
where t1.name='劉晨' and t1.xi=t2.xi) t3
where t3.name<>'劉晨';