HIVE实验

╄→尐↘猪︶ㄣ 提交于 2020-01-29 04:04:15

Hive基本操作实验

  1. show命令显示数据库和表

    show databases;
    show tables;
    
  2. create创建test数据库,并通过show命令查看。

    create database test;
    show database;
    
  3. create命令创建emp表,列名分别为empno、ename、deptno、mgr、和salary,并通过show命令查看。

    create table emp(empno string,ename string,deptno string,mgr string,salary int) row format delimited fields terminated by '\t';
    
    show tables;
    
  4. 将/root下面的emp.txt中的内容导入到emp表,数据之间以\t分隔开。

    load data local inpath '/root/emp.txt' overwrite into table emp;

    select * from emp;

  5. 向emp表增加列adress,然后显示emp表的结构;

    alter table emp add columns (adress string);

    desc emp;

  6. 建立以firstname为分区列的分区表testa;向分区表testa中插入数据,然后查看目录结构。

    create table testa(sno string,sname string,ssex,string,sage int) partition by (firstname string);

    insert into testa partition (firstname='east') select empno,ename,'male',30 from emp;

    insert into testa partiton (firstname='west') select empno,ename,'female',28 from emp;

  7. 创建表empt

    create table empt as select * from emp;

    desc emp2;

    select * from emp2;

  8. 向empt表中插入几条数据,并将empt重命名为empa;

    insert into empt() values();

    insert into empt() values();

    alter table empt rename ro empa;

  9. 将empa的数据写入到本地目录。

    insert overwrite local diretory 'empa' row format delimited fields terminated by '\t' select * from empa;

  10. 将表empa导出。

    export table empa to '/exportdir/empa';

  11. 删除empa,将empa导入时改名为empb,并查看信息和内容。

    drop table empa;
    import table empb from '/exportdir/empa';
    dsc empb;
    select * from empb;
    
  12. 定义外部表

    create external table employee (empno string,ename string,deptno string,mgr string,salary int) row fromat delimited fileds terminated by '\t' location '/hive/warehose/emp'
    

Hive综合查询

  • 统计学生总人数和平均年龄

    select count(1),avg(age) from student;
    
  • 查询每个系的男生人数

    select dept,count(1) from student where sex='男' group by dept;
    
  • 查询每个学生的学号、选修课程数和平均成绩。

    select s.sname,tmp.cnt,tmp.avg_grade from student s,(select sno,count(1) cnt,avg(grade) avg_grade from sc group by sno) tmp where s.sno=tmp.sno;
    
  • 查询选修了3门以上客户曾的学生学号、选修课程数和平均成绩

    hive> select sno,count(1) cnt,avg(grade) avg_grade from sc group by sno having count(1) >3;
    
    
  • 查询每个学生的学号、姓名、课程号、课程名和成绩。

    select s.sno,s.sname,sc.cno,c.cname,sc.grade from student s join sc on s.sno=sc.sno join course c on sc.cno=c.cno;
    
    
  • 查询学生学号,要求每门课程都在85分以上。

    select sno from sc group by sno having min(grade) > 80; 
    
    
  • 查询学了1号课,没学2号课的学生学号。

    select distinct t1.sno from (select sno from sc where cno=1) t1 where not exists(select sno from sc where cno=2 and sno=t1.sno);
    
    select t1.sno from (select sno from sc where cno=1) t1 left outer join  (select sno from sc where cno=2) t2 on t1.sno=t2.sno where t2.sno is null;
    
    
  • 查询考试成绩大于所属课程平均成绩的学号、课程号和成绩。

    select sc.* from sc,(select cno,avg(grade) avg_grade from sc group by sno) tmp where sc.cno=tmp.cno and sc.grade > tmp.avg_grade;
    
    
  • 查询已选课的学生的信息.

    select * from student where sno in(select distinct sno from sc);
    
    
  • 查询学生的学生学号、姓名、课程号和成绩,包括那些没有选课的学生

    select s.sno,s.sname,sc.cno,sc.grade from student s left outer join sc on
    s.sno =sc.sno;
    
    
  • 查询平均成绩最高的课程号

    select cno,avg(grade) avg_grade from sc group by cno order by avg_grade desc limit 1;
    
    
  • 查询与学号为15001学生有共同选修课的学生学号。

    select sno from sc where sno not in
    (select sno from sc where cno not in(select cno from sc where sno='15001'))
    and sno<>'15001' group by sno having count(1) = (select count(*) from sc where sno='15001')
    
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!