Hive实验2

1.正确建表,导入数据(三张表,三份数据),并验证是否正确
create table course(cid int ,cname string) row format delimited fields terminated by ‘,’;
load data local inpath ‘/home/hadoop/file/hive/course.csv’ into table course;
select * from course;

create table student(sid int ,sname string,grade int ,class int) row format delimited fields terminated by ‘,’;
load data local inpath ‘/home/hadoop/file/hive/student.csv’ into table student;
select * from student;

create table score(sid int ,cid int ,score int) row format delimited fields terminated by ‘,’;
load data local inpath ‘/home/hadoop/file/hive/score.csv’ into table score;
select * from score;

2.查询所有学生的成绩信息:学生姓名、课程名、课程成绩。

select sname,cname,score from student,course,score where student.sid=score.sid and score.cid=course.cid;

3.查询编号为10的课程比编号为20的课程成绩高的学生的编号及课程分数

select distinct x.sid,x.score,y.score from score x ,score y where (x.cid=10 and y.cid=20) and (x.score>y.score) and x.sid=y.sid;

4.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select x.sid,y.sname,avg(x.score) from score x,student y where x.sid=y.sid
group by x.sid,y.sname having avg (x.score)>=60 ;

5.查询并创建表格:从数据中获取学生的姓名和各科成绩,并将学生姓名,各科成绩的数组 形式数据保存在temp表格中。
结果形如:Eric [53,29,33,27,22,43,55]
insert overwrite local directory ‘/home/hadoop/temp’
select sname,collect_list(score) from student,score where student.sid=score.sid group by sname;

6.查询编号为10的课程的平均分
select avg(score) from score where cid=10 group by cid;

7.查询每门课程的平均分(课程编号,课程名,平均分)
select score.cid,course.cname,avg(score.score) from score ,course where course.cid=score.cid group by score.cid,course.cname;

8.按照课程对学生的成绩进行顺序排序(课程编号,学生编号,成绩,排名)
select cid,sid,score,rank() over (partition by cid order by score desc ) rank from score ;

9.查询每门课程第一名(所有第一名)(课程名称,学生姓名,成绩)
Select * from (select course.cname,student.sname,score.score,rank() over(partition by score.cid order by score.score desc) seq from student,score,course where student.sid=score.sid and course.cid=score.cid) tab where tab.seq=1 ;

10.统计每门课程不及格的学生人数(课程编号,不及格人数)
select cid,count(sid) from score where score.score<60 group by cid ;

11.统计每门课程不及格的学生的姓名(课程编号,不及格学生姓名集合)形如: 20 [“Eric”,“Joy”]
select cid,collect_list(sname) from score,student where score.sid=student.sid and score<60 group by cid;

12.查询两门及其以上不及格课程的学生的学号,姓名
Select student.sid,student.sname from student , score where student.sid=score.sid and score.score<60 group by student.sid,student.sname having count(score.score)>=2;

13.查询学生的总成绩并进行排名(姓名 总分 名次)
Select sname,sum(score),rank() over(order by sum(score) desc) from student,score where student.sid=score.sid group by student.sid,student.sname;

14.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 (sid,cid,score,average)
Select sid,cid,score,avg(score) over (partition by sid ) average from score order by average desc;

15.查询各科成绩最高分、最低分和平均分(课程编号,最高分,最低分,平均分)
Select cid,max(score),min(score),avg(score) from score group by cid;

### 关于 Hive 安装部署的实验报告或指南 以下是关于 Hive 安装部署的一个综合性的说明,涵盖了安装过程中的主要步骤以及可能遇到的问题。 #### 1. 环境准备 在开始安装之前,需要确保基础环境已经准备好。Hive 的运行依赖 Hadoop 和 Java 环境的支持[^4]。 - **Java 配置**: 如果系统提示“`-bash: jps: command not found`”,则表明 JAVA 的环境变量未正确配置。此时需按照标准流程设置 `JAVA_HOME` 并将其加入到系统的 PATH 中。 - **Hadoop 安装**: Hive 是构建在 HDFS 上的数据仓库工具,因此必须先完成 Hadoop 的集群搭建并验证其正常运行。 #### 2. 下载与解压 Hive 下载最新版本的 Apache Hive 压缩包,并将其解压缩至目标目录。例如: ```bash wget https://downloads.apache.org/hive/stable/apache-hive-3.1.2-bin.tar.gz tar -xzvf apache-hive-3.1.2-bin.tar.gz -C /opt/ ``` #### 3. 配置文件调整 编辑 Hive 的核心配置文件 `hive-site.xml` 来指定元数据存储的位置以及其他参数。通常情况下,这些信息会被保存在一个关系型数据库(如 MySQL 或 Derby)中。以下是一个简单的示例配置片段: ```xml <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost:3306/metastore?createDatabaseIfNotExist=true</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.cj.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>password</value> </property> </configuration> ``` #### 4. 初始化 Metastore 数据库 通过执行内置脚本来初始化 Hive 所使用的元数据库结构。假设使用的是 MySQL,则可以通过如下命令实现: ```bash schematool -dbType mysql -initSchema ``` #### 5. 启动服务测试 启动 Hive CLI 进入交互模式进行基本功能验证。也可以利用 `-f` 参数批量执行 SQL 脚本并将结果导出到文件中[^3]: ```bash bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt ``` #### 6. 性能优化选择 针对不同的应用场景可以选择合适的执行引擎。虽然 Tez 成为了当前主流推荐的选择之一,因为它能够带来更优的速度和资源利用率;但在某些特定条件下,传统的 MapReduce 可能更加适用[^2]。实际操作过程中应依据具体情况权衡两者之间的差异。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值