寒假学习报告12
Posted baimafeima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寒假学习报告12相关的知识,希望对你有一定的参考价值。
把昨天剩余的Scala基础编程做了,也按要求做了下一个实验的内容
3. 统计学生成绩
学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名 1、课程名 2 等,后面每一行代表一个学生的信息,各字段之间用空白符隔开
给定任何一个如上格式的清单(不同清单里课程数量可能不一样),要求尽可能采用函 数式编程,统计出各门课程的平均成绩,最低成绩,和最高成绩;另外还需按男女同学分开, 分别统计各门课程的平均成绩,最低成绩,和最高成绩。 测试样例 1 如下:
样例 1 的统计结果输出为:
import scala.collection.mutable.ArrayBuffer object exercise4 { def main(args: Array[String]): Unit = { var students:ArrayBuffer[Student] = new ArrayBuffer[Student]() students.append(new Student(301610, "male", 80, 64, 78)) students.append(new Student(301611, "female", 65, 87, 58)) students.append(new Student(301612, "female", 44, 71, 77)) students.append(new Student(301613, "female", 66, 71, 91)) students.append(new Student(301614, "female", 70, 71, 100)) students.append(new Student(301615, "male", 72, 77, 72)) students.append(new Student(301616, "female", 73, 81, 75)) students.append(new Student(301617, "female", 69, 77, 75)) students.append(new Student(301618, "male", 73, 61, 65)) students.append(new Student(301619, "male", 74, 69, 68)) students.append(new Student(301620, "male", 76, 62, 76)) students.append(new Student(301621, "male", 73, 69, 91)) students.append(new Student(301622, "male", 55, 69, 61)) students.append(new Student(301623, "male", 50, 58, 75)) students.append(new Student(301624, "female", 63, 83, 93)) students.append(new Student(301625, "male", 72, 54, 100)) students.append(new Student(301626, "male", 76, 66, 73)) students.append(new Student(301627, "male", 82, 87, 79)) students.append(new Student(301628, "female", 62, 80, 54)) students.append(new Student(301629, "male", 89, 77, 72)) println(students.length) println(students(1).toString) getData(students) getDataMale(students) getDataFeMale(students) } def getData(value: ArrayBuffer[Student]): Unit ={ var Mathaverage:Double=0; var MathSum:Double = 0; var MathMin:Double = value(0).Math; var MathMax:Double = 0; var Englishaverage:Double=0; var EnglishSum:Double = 0; var EnglishMin:Double = value(0).English; var EnglishMax:Double = 0; var Physicsaverage:Double=0; var PhysicsSum:Double = 0; var PhysicsMin:Double = value(0).Physics; var PhysicsMax:Double = 0; var number = value.length; for (i <- 0 until value.length){ //三科总数 MathSum = value(i).Math+MathSum EnglishSum = value(i).English+EnglishSum PhysicsSum = value(i).Physics+PhysicsSum //最大和最小 if(value(i).Math>MathMax){ MathMax=value(i).Math; } if(value(i).English>EnglishMax){ EnglishMax=value(i).English; } if(value(i).Physics>PhysicsMax){ PhysicsMax=value(i).Physics; } if(value(i).Math<MathMin){ MathMin=value(i).Math; } if(value(i).English<EnglishMin){ EnglishMin=value(i).English; } if(value(i).Physics<PhysicsMin){ PhysicsMin=value(i).Physics; } } //平均值 Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course average min max ") println("Math: "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } def getDataMale(value: ArrayBuffer[Student]): Unit = { var Mathaverage: Double = 0; var MathSum: Double = 0; var MathMin: Double = value(0).Math; var MathMax: Double = 0; var Englishaverage: Double = 0; var EnglishSum: Double = 0; var EnglishMin: Double = value(0).English; var EnglishMax: Double = 0; var Physicsaverage: Double = 0; var PhysicsSum: Double = 0; var PhysicsMin: Double = value(0).Physics; var PhysicsMax: Double = 0; var number = 0; for (i <- 0 until value.length) { if (value(i).gender.equals("male")) { number = number + 1 //三科总数 MathSum = value(i).Math + MathSum EnglishSum = value(i).English + EnglishSum PhysicsSum = value(i).Physics + PhysicsSum //最大和最小 if (value(i).Math > MathMax) { MathMax = value(i).Math; } if (value(i).English > EnglishMax) { EnglishMax = value(i).English; } if (value(i).Physics > PhysicsMax) { PhysicsMax = value(i).Physics; } if (value(i).Math < MathMin) { MathMin = value(i).Math; } if (value(i).English < EnglishMin) { EnglishMin = value(i).English; } if (value(i).Physics < PhysicsMin) { PhysicsMin = value(i).Physics; } } } //平均值 Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course average min max (Male)") Mathaverage=Mathaverage.toInt Englishaverage=Englishaverage.toInt Physicsaverage=Physicsaverage.toInt println("Math: "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } def getDataFeMale(value: ArrayBuffer[Student]): Unit ={ var Mathaverage:Double=0; var MathSum:Double = 0; var MathMin:Double = value(1).Math; var MathMax:Double = 0; var Englishaverage:Double=0; var EnglishSum:Double = 0; var EnglishMin:Double = value(1).English; var EnglishMax:Double = 0; var Physicsaverage:Double=0; var PhysicsSum:Double = 0; var PhysicsMin:Double = value(1).Physics; var PhysicsMax:Double = 0; var number = 0; for (i <- 0 until value.length){ if(value(i).gender.equals("female")){ number=number+1 //三科总数 MathSum = value(i).Math+MathSum EnglishSum = value(i).English+EnglishSum PhysicsSum = value(i).Physics+PhysicsSum //最大和最小 if(value(i).Math>MathMax){ MathMax=value(i).Math; } if(value(i).English>EnglishMax){ EnglishMax=value(i).English; } if(value(i).Physics>PhysicsMax){ PhysicsMax=value(i).Physics; } if(value(i).Math<MathMin){ MathMin=value(i).Math; } if(value(i).English<EnglishMin){ EnglishMin=value(i).English; } if(value(i).Physics<PhysicsMin){ PhysicsMin=value(i).Physics; } } } //平均值 Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course average min max (FeMale)") Mathaverage=Mathaverage.toInt Englishaverage=Englishaverage.toInt Physicsaverage=Physicsaverage.toInt println("Math: "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } } class Student(id1:Int,gender1:String,Math1:Double,English1:Double,Physics1:Double){ var id:Int = id1 var gender:String = gender1 var Math:Double = Math1 var English:Double = English1 var Physics:Double = Physics1 override def toString = s"Student($id, $gender, $Math, $English, $Physics)" }
1.安装 Hadoop 和 Spark
进入 Linux 系统,参照本教程官网“实验指南”栏目的“Hadoop 的安装和使用”,完 成 Hadoop 伪分布式模式的安装。完成 Hadoop 的安装以后,再安装 Spark(Local 模式)。
2.HDFS 常用操作
使用 hadoop 用户名登录进入 Linux 系统,启动 Hadoop,参照相关 Hadoop 书籍或网络 资料,或者也可以参考本教程官网的“实验指南”栏目的“HDFS 操作常用 Shell 命令”,使用 Hadoop 提供的 Shell 命令完成如下操作:
(1) 启动 Hadoop,在 HDFS 中创建用户目录“/user/hadoop”;
(2) 在 Linux 系统的本地文件系统的“/home/hadoop”目录下新建一个文本文件 test.txt,并在该文件中随便输入一些内容,然后上传到 HDFS 的“/user/hadoop” 目录下;
(3) 把 HDFS 中“/user/hadoop”目录下的 test.txt 文件,下载到 Linux 系统的本地文 件系统中的“/home/hadoop/下载”目录下;
(4) 将HDFS中“/user/hadoop”目录下的test.txt文件的内容输出到终端中进行显示;
(5) 在 HDFS 中的“/user/hadoop”目录下,创建子目录 input,把 HDFS 中 “/user/hadoop”目录下的 test.txt 文件,复制到“/user/hadoop/input”目录下;
(6) 删除HDFS中“/user/hadoop”目录下的test.txt文件,删除HDFS中“/user/hadoop” 目录下的 input 子目录及其子目录下的所有内容。
3. Spark 读取文件系统的数据
(1)在 spark-shell 中读取 Linux 系统本地文件“/home/hadoop/test.txt”,然后统计出文 件的行数;
(2)在 spark-shell 中读取 HDFS 系统文件“/user/hadoop/test.txt”(如果该文件不存在, 请先创建),然后,统计出文件的行数;
(3)编写独立应用程序,读取 HDFS 系统文件“/user/hadoop/test.txt”(如果该文件不存在, 请先创建),然后,统计出文件的行数;通过 sbt 工具将整个应用程序编译打包成 JAR 包, 并将生成的 JAR 包通过 spark-submit 提交到 Spark 中运行命
以上是关于寒假学习报告12的主要内容,如果未能解决你的问题,请参考以下文章