使用hive完成一个学生课外活动分析

使用hive完成学生课外活动分析。其中
userid      学生ID
name       学生姓名
classid      班级ID
countScore          综合素质能力分数(后边所有项的和求平均)
expressionscore  语言表达能力分数(辅导员给分,一定有)
innovationscore   创新能力分数(辅导员给分,一定有)
studentscore        学生工作实践分数(辅导员给分,一定有)
reading                 阅读经典实践分数
assistant               助教实践分数
society                  社会实践分数(学工处任务)
art                         文艺活动实践分数(所有学校文艺活动实践)
sport                     体育活动实践分数
special                 专业活动实践(大三方向专业成果展示)
scientific              学生科研实践分数(学生独立申请的科研项目)
manage              论文答辩学生管理工作分数
enterprise           创业计划实践(表现优秀,开展落地项目)

首先建立数据表,设置字段类型:
create table kewaihuodong(
      name string,
      id int,
      classid int,
      countScore int,
      expressionscore int,
      innovationscore int,
      studentscore int,
      reading int,
      assistant int,
      society int,
      art int,
      sport int,
      special int,
      scientific int,
      manage int,
      enterprise int)row format delimited fields terminated by '\t' lines terminated by '\n';
其中使用create table创建表,使用\t补全当前字符串长度到8的整数倍,\n回车换行

16033a653c754ec78357a3135b12a850.jpg
  然后导入数据进入一个新表:
load data local inpath '/usr/local/hive/data/kewaiduodong.txt' into table kewaihuodong;

37e1a42ebffb4677a90141174d0b744f.jpg

再通过全表查询表'kewaihuodong'中的数据:select * from kewaihuodong;

7853fcaa745b4c488d2b61ec663cb17c.jpg

其中要求一:按专业统计学生综合素质情况,判断标准为:
90-100优;80-90良;70-80:一般;60-70:合格
select id,name,classid,
case when countscore >= 90 then 'wonderful'
when countscore >= 80 then 'good'
when countscore >= 70 then 'normal'
when countscore >= 60 then 'pass' else 'inappropriate'
end as score from kewaihuodong;

知识点:case函数:
语法: case when a then b [when c then d]* [else e] end (用于处理单个列的查询结果,说明:如果a为TRUE,则返回b;如果c为TRUE,则返回d;否则返回e)
语法: case a when b then c [whend then e]* [else f] end (说明:如果a等于b,那么返回c;如果a等于d,那么返回e;否则返回f。注意这种when的判断条件可以有很多个)
运行结果展示:要求一

951c023ddd2f4af19eddfd4d313591c8.jpg

 

要求二:对数据进行探索,分析对学生的综合素质能力评价指标
select round(avg(countScore),2) as average,
max(countScore) as maximum,min(countScore) as minimum,
max(countScore)-min(countScore) as max_min 
from kewaihuodong;


知识点:
round(x,2)对x值保留两位小数
avg()计算指定列平均值
max()计算最大值
min()计算最小值
max()-min()计算极差

3e15c3903d034f70870c28348a909a18.jpg

 

需求三:统计每一类活动参加的学生人数
select count( expressionscore ),count( innovationscore ),
count( studentscore ),count( reading ),count( assistant ),count(society ),count( art) ,count( sport ),count( special ),
count( scientific ),count( manage ),count( enterprise ) 
from kewaihuodong;


知识点:Count( )函数
count(*):所有行进行统计,包括null行
count(1):所有行进行统计,包括null行
count(column):对column中非null进行统计
count(distinct column):对column中非null进行去重统计
count(distinct col1,col2,...):对col1、col2,...多个字段同时去重并统计
count(case when plat=1 then u else null end)
count(distinct case when plat=1 then u else null end)
count(case when (type=2 or type=6) then u else null end)
count(distinct case when (type=2 or type=6) then u else null end)
运行结果展示:需求三

8147754b427a46c49ebe2baa7b80956e.jpg
 

需求四:统计每个学生参加了几个活动
select classid,id,name,
(if(expressionscore is null,0,1)+if(innovationscore is null,0,1)+
if(studentscore is null,0,1)+if(reading is null,0,1)+if(assistant is null,0,1)+if(society is null,0,1)+if(art is null,0,1)+if(sport is null,0,1)+if(special is null,0,1)+if(scientific is null,0,1)+if(manage is null,0,1)+if(enterprise is null,0,1)) sum_activity 
from kewaihuodong order by classid;


知识点:
If(条件,条件为True的对应值,条件为False 的对应值)
select if(1=2,’a’,’b’) from table;-------------->b
select if(1=1,’a’,’b’) from table;-------------->a
order by 语句:
    用于根据指定的列对结果集进行排序,默认升序排序。
order by 会做全局排序,因此只有一个reducer(只有一个reducer,会导致当输入规模较大时,需要较长的时间),若为多个reducer无法保证全局有序。
 运行结果展示:需求四 

73aefabb734f4d3b93fa4983d0b339dd.jpg 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值