当前位置:网站首页>SparkRDD 案例:计算总成绩
SparkRDD 案例:计算总成绩
2022-06-22 00:12:00 【Yushijuj】
一、提出任务
- 成绩表,包含四个字段(姓名,语文,数学,英语),只有三条记录

二、完成任务
(一)将成绩文件上传到HDFS
- 执行命令: hdfs dfs -put scores.txt /input

- 查看成绩文件内容

(二)创建Maven项目
- 创建Maven项目 - CalculateScoreSum


- 将JAVA 目录改成Scala目录

(三)添加依赖和构建插件
- 在 pom.xml 文件里添加依赖和构建插件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.cb.rdd</groupId>
<artifactId>CalculateScoreSum</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.15</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 注意项目使用的Scala SDK版本

(四)创建日志属性文件
- 在resources 文件里创建log4j.properties文件
log4j.rootLogger=ERROR, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spark.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
(五)创建包与计算总成绩单例单例对象
- 创建net.cb.rdd包

- 在net.cb.rdd包里创建CalculateScoreSum单例对象

package net.cb.rdd
import org.apache.spark.{
SparkConf, SparkContext}
import scala.collection.mutable.ListBuffer
/** * 功能:计算总分 * 作者:cb * 日期:2022年05月24日 */
object CalculateScoreSum {
def main(args: Array[String]): Unit = {
// 设置HADOOP用户名属性,否则写HDFS文件会被拒绝
System.setProperty("HADOOP_USER_NAME", "root")
// 创建Spark配置对象
val conf = new SparkConf()
.setAppName("CalculateScoreSum")
.setMaster("local[*]")
// 基于配置创建Spark上下文
val sc = new SparkContext(conf)
// 读取HDFS文件
val lines = sc.textFile("hdfs://master:9000/input/scores.txt")
// 创建成绩列表
val scores = new ListBuffer[(String, Int)]()
lines.collect.foreach(
line => {
val rec = line.split(" ")
scores += Tuple2(rec(0), rec(1).toInt)
scores += Tuple2(rec(0), rec(2).toInt)
scores += Tuple2(rec(0), rec(3).toInt)
})
// 基于成绩列表创建RDD
val rdd1 = sc.makeRDD(scores)
// 对成绩RDD进行按键归约处理
val rdd2 = rdd1.reduceByKey((x, y) => x + y)
// 输出归约处理结果
rdd2.collect.foreach(println)
// 将归约处理结果写入HDFS文件
rdd2.saveAsTextFile("hdfs://master:9000/scoresum")
}
}
(六)运行程序,查看结果
- 查看控制台输出结果

- 查看HDFS 上生成的结果文件

- 查看结果文件内容

边栏推荐
- 四数之和[数组排序+双指针]
- It took 2 hours to build an Internet of things project, which is worth~
- .NET中获得hInstance的几个方法
- In the operation exchange of the points system, which behaviors of users can obtain points
- Compilation principle - recursive descent subroutine method
- 让人无法喜爱的STL
- 合理选择液压滑环密封间隙的重要性
- 对面积的曲面积分中dS与dxdy的转换
- find 查找不同扩展名的文件
- 12 initialization of beautifulsoup class
猜你喜欢

比特運算比特與

pytorch学习11:where 和 gather

Pytorch learning 05: indexing and slicing

0x00007ffff3d3ecd0 in _ IO_ vfprintf_ internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>

pytorch学习04:Tensor的创建

Meetup03 review: introduction to the new version of linkis and the application practice of DSS

Pytorch learning 10: statistical operations

Rotating frame target detection -- about the definition and solution of rotating frame

Pytorch learning 04: creation of tensor
![[dailyfresh] problems encountered in sending activation email](/img/08/b292f6c98615e51e666b78f305e92c.png)
[dailyfresh] problems encountered in sending activation email
随机推荐
Is it safe to open an account for futures in Huishang futures?
mysql数据库高版本 低版本
【Redis】ubuntu中安装redis以及redis的基本使用和配置
SQL语句——权限管理
[GStreamer] 插件编写 —— Test程序
WinCE 下 ListCtl virtual mode的使用
答应我, 不要再用 if (obj != null) 判空了
HDOJ - Is It A Tree?
聚宽 - 简单策略试验
eVC4编的程序不能在emulator上运行
pytorch学习12:自动求导
Powershell 函数在数学表达式中的使用
四数之和[数组排序+双指针]
lvgl使用demo示例及说明1
过孔式导电滑环怎么用
积分体系运营汇中,用户的哪些行为可以获得积分
English语法_副词 - loud /aloud / loudly
【环境踩坑】在自己电脑上搭建FastDFS
花了2小时,搭建了一个物联网项目,值了 ~
力扣每日一题-第24天-485.最大连续1的个数