使用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回车换行
然后导入数据进入一个新表:
load data local inpath '/usr/local/hive/data/kewaiduodong.txt' into table kewaihuodong;
再通过全表查询表'kewaihuodong'中的数据:select * from kewaihuodong;
其中要求一:按专业统计学生综合素质情况,判断标准为:
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的判断条件可以有很多个)
运行结果展示:要求一
要求二:对数据进行探索,分析对学生的综合素质能力评价指标
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()计算极差
需求三:统计每一类活动参加的学生人数
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)
运行结果展示:需求三
需求四:统计每个学生参加了几个活动
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无法保证全局有序。
运行结果展示:需求四