> 文章列表 > 在线考试系统学员答题批改日志,实战练习

在线考试系统学员答题批改日志,实战练习

在线考试系统学员答题批改日志,实战练习

一、环境要求

sandbox-hdp 2.6.4 或同等版本自建的Hadoop+Hive+Spark+HBase 开发环境。

二、数据描述

这是一份来自于某在线考试系统的学员答题批改日志,日志中记录了日志生成时间,题目难度系数,题目所属的知识点 ID,做题的学生 ID,题目 ID 以及作答批改果。日志的结构如下:
在线考试系统学员答题批改日志,实战练习

三、功能要求

1、数据准备

请在 HDFS 中创建目录/app/data/exam,并将 answer_question.log 传到该目录。

[root@hadoop02 ~]# hdfs dfs -put /opt/testdata/answer_question.log /app/data/exam

2、在 Spark-Shell 中,加载 HDFS 文件系统 answer_question.log 文件,并使用 RDD 完成以下分析,也可使用 Spark 的其他方法完成数据分析。

①提取日志中的知识点 ID,学生 ID,题目 ID,作答结果 4 个字段的值

scala> val rdd = fileRDD.map(x=>x.substring(x.indexOf("set "))).map(x=>x.split(",")).map(x=>x(0)).map(x=>{val a = x.split(" ");val b=a(1).split("_");(b(1),b(2),b(3),a(2))}).foreach(println)

在线考试系统学员答题批改日志,实战练习

②将提取后的知识点 ID,学生 ID,题目 ID,作答结果字段的值以文件的形式保存到 HDFS的/app/data/result 目录下。一行保留一条数据,字段间以“\\t”分割。文件格式如下所示。

scala> rdd.map(x=>x.productIterator.mkString("\\t")).saveAsTextFile("/app/data/exam2")

3、创建 HBase 数据表

在 HBase 中创建命名空间(namespace)exam,在该命名空间下创建 analysis 表,使用学生 ID 作为 RowKey,该表下有 2 个列族 accuracy、question。accuracy 列族用于保存学 员 答 题 正 确 率 统 计 数 据 ( 总 分 accuracy:total_score , 答 题 的 试 题 数accuracy : question_count,正确率accuracy:accuracy);question 列族用于分类保存学员正确,错 误和半对的题目 id (正确 question:right,错误 question:error,半对question:half)

hbase(main):007:0> create 'exam:analysis','accuracy','question'

4、请在 Hive 中创建数据库 exam,在该数据库中创建外部表 ex_exam_record 指向/app/data/result 下 Spark 处理后的日志数据 ;

create external table ex_exam_record_ana(topic_id string,student_id string,question_id string,score float
)
row format delimited fields terminated by "\\t"
stored as textfile location "/app/data/exam2";

在线考试系统学员答题批改日志,实战练习
创建外部表 ex_exam_anlysis 映射至 HBase中的 analysis 表的 accuracy 列族

ex_exam_analysis在线考试系统学员答题批改日志,实战练习
创建外部表 ex_exam_question 映射至 HBase 中的analysis 表的 question 列族
在线考试系统学员答题批改日志,实战练习

create external table  ex_exam_anlysis(student_id string,total_score float,question_count int,accuracy float
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,accuracy:total_score,accuracy:accuracy,accuracy:question_count")
tblproperties ("hbase.table.name"="exam:analysis")
create external table  ex_exam_question(student_id string,`right` string,`half` string,`error` float
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,question:right,question:half,question:error")
tblproperties ("hbase.table.name"="exam:analysis");

5、使用 ex_exam_record 表中的数据统计每个学员总分、答题的试题数和正确率,并保存到 ex_exam_anlysis 表中,其中正确率的计算方法如下:正确率=总分/答题的试题数

insert into table ex_exam_anlysis_ana(
select student_id,t1.totalscore,t1.`all`,double(t1.totalscore)/double(t1.`all`) `rate` from (
select student_id,sum(score) `totalscore`,count(1) `all` from ex_exam_record_ana group by student_id) t1)

6、使用 ex_exam_record 表中的数据统计每个作对,做错,半对的题目列表。

①题目 id 以逗号分割,并保存到 ex_exam_question表中。

with t1 as(
select student_id,case when score=1 then question_id end as `right`,case when score=0.5 then question_id end as `half`,case when score=0 then question_id end as `error`from ex_exam_record_ana)
select student_id,concat_ws(',',collect_list(`right`)),concat_ws(',',collect_list(`half`)),concat_ws(',',collect_list(`error`)) from t1 group by student_id;

在线考试系统学员答题批改日志,实战练习

②完成统计后,在 HBase Shell 中遍历 exam:analysis表并只显示 question 列族中的数据,