Hive sql练手

#在hive中创建表
create table student(sno int,sname string,ssex string,sage int,sdept string) row format delimited fields terminated by "," stored as textfile;
create table course(cno int,cname string) row format delimited fields terminated by "," stored as textfile;

create table sc(sno int,sco int ,grade int) row format delimited fields terminated by "," stored as textfile;

#数据准备

https://pan.baidu.com/s/1ek6UcQ

#将数据从hdfs中导入到hive表中
load data inpath "/data/student.txt" overwrite into table student
load data inpath "/data/course.txt" overwrite into table course;
load data inpath "/data/sc.txt" overwrite into table sc;
##########hql查询#############
(1)查询全体学生的学号与姓名
select sno ,sname from student;
(2)查询选修了课程的学生姓名(+课程数量)
select st.sname,count(*) from student st,sc where sc.sno=st.sno group by st.sname;
select st.sname from student st,sc where sc.sno=st.sno group by st.sname;
select distinct st.sname from student inner join sc on(sc.sno=st.sno);
(2)查询学生的总人数
select count(1) from student;
(4)计算1号课程的学生平均成绩
select avg(grade) from sc where sco = 1;
(5)查询各科成绩平均分(保留2位小数)
select co.cname,round(avg(sc.grade),2) from course co left join sc on (co.cno = sc.sco) group by co.cname;
(6)查询选修1号课程的学生最高分数
select max(sc.grade) from sc where sc.sco = 1;
(7)求各个课程号(或课程名称)及相应的选课人数
select sc.sco,count(sc.sno) from sc  group by sc.sco;
select sc.sco,course.cname,count(sc.sno) from sc left join course on(sc.sco = course.cno)  group by sc.sco , course.cname;
(8)***查询选修了3门以上的课程的学生学号(或者姓名)
select sc.sno,count(sc.sco) from sc group by sc.sno having count(sc.sco)>3;#分组后聚合
select sc.sno,student.sname,count(sc.sco) from sc left join student on (sc.sno=student.sno) group by sc.sno,student.sname having count(sc.sco)>3;
(9)查询学生信息,结果按学号全局有序
select*from student order by sno asc;
(10)***查询学生信息,结果区分性别按年龄有序
select*from student order by sage desc,ssex asc;
(11)查询每个学生及其选修课程的情况
select*from student,sc where student.sno = sc.sno; 
(12)查询学生的得分情况
select st.sname,st.sno,co.cname ,sc.grade from student st left join sc on(sc.sno = st.sno) left join course co on(co.cno = sc.sco);
(13)查询选修2号课程且成绩在90分以上的所有学生
select st.sname,st.sno,co.cname,sc.grade from student st left join sc on(sc.sno = st.sno) left join course co on(co.cno = sc.sco) where co.cno=2 and sc.grade>=90; 
(14)查询所有学生的信息,如果在成绩表中有成绩,则输出成绩表中的课程号
select st.*,sc.sco,sc.grade from student st left join sc on(sc.sno=st.sno) left join course co on(co.cno = sc.sco);
select st.*,sc.sco,sc.grade from student st left join sc on(sc.sno=st.sno) where sc.grade is not null;
(15)查询与“刘晨”在同一个系学习的学生
select st.* from student st where st.sdept in (select sdept from student where sname ="刘晨" );
select st.* from student st where st.sdept in (select sdept from student where sname ="刘晨" ) and st.sname != "刘晨";
1.上传tar包 2.解压 tar -zxvf hive-1.2.1.tar.gz 3.安装mysql数据库 推荐yum 在线安装 4.配置hive (a)配置HIVE_HOME环境变量 vi conf/hive-env.sh 配置其中的$hadoop_home (b)配置元数据库信息 vi hive-site.xml 添加如下内容: javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true JDBC connect string for a JDBC metastore javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver Driver class name for a JDBC metastore javax.jdo.option.ConnectionUserName root username to use against metastore database javax.jdo.option.ConnectionPassword hadoop password to use against metastore database 5.安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下 如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行) mysql -uroot -p #(执行下面的语句 *.*:所有库下的所有 %:任何IP地址或主机都可以连接) GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES; 6. Jline包版本不一致的问题,需要拷贝hive的lib目录中jline.2.12.jar的jar包替换掉hadoop中的 /home/hadoop/app/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar 启动hive bin/hive ---------------------------------------------------------------------------------------------------- Hive几种使用方式: 1.Hive交互shell bin/hive 2.Hive JDBC服务(参考java jdbc连接mysql) 3.hive启动为一个服务器,来对外提供服务 bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & 启动成功后,可以在别的节点上用beeline去连接 bin/beeline -u jdbc:hive2://mini1:10000 -n root 或者 bin/beeline ! connect jdbc:hive2://mini1:10000 4.Hive命令 hive -e ‘sql’ bin/hive -e 'select * from t_test'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值