目录
1.任务目标
有多科成绩表,比如python.txt、spark.txt、django.txt,计算每个学生三科平均分
2.准备工作
在master
虚拟机上创建三个成绩文件
在HDFS上新建/score
目录
将三个成绩文件上传到HDFS的/score
目录
3.新建Maven项目
将java
目录改成scala
目录
4.添加相关依赖和构建插件
<?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.luog.sql</groupId>
<artifactId>CalculateAverage</artifactId>
<version>1.0-SNAPSHOT</version><dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.12</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.1</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>
5.创建日志属性文件
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
6.创建分组排行榜单例对象
package net.luog.sql
import org.apache.spark.sql.{Dataset, SparkSession}
/**
* 功能:利用Spark SQL计算平均分
*/
object CalculateAverageBySQL {
def main(args: Array[String]): Unit = {
// 创建或得到Spark会话对象
val spark = SparkSession.builder()
.appName("CalculateAverageBySQL")
.master("local[*]")
.getOrCreate()
// 读取HDFS上“/score”目录里的成绩文件
val lines: Dataset[String] = spark.read.textFile("hdfs://master:9000/score")
// 导入隐式转换
import spark.implicits._
// 创建成绩数据集
val gradeDS: Dataset[Grade] = lines.map(
line => {
val fields = line.split(" ")
val id = fields(0).toInt
val name = fields(1)
val score = fields(2).toInt
Grade(id, name, score)
})
// 将数据集转换成数据帧
val df = gradeDS.toDF();
// 基于数据帧创建临时表
df.createOrReplaceTempView("t_grade")
// 查询临时表,计算平均分
val avg = spark.sql(
"""
|select first(id) as id, name, cast(avg(score) as decimal(5, 2)) as average
| from t_grade
| group by name
| order by id
|""".stripMargin
)// 按照指定格式输出平均成绩
println()
avg.collect.foreach(row => println(row(0) + " " + row(1) + " " + row (2)))// 关闭Spark会话
spark.close()
}// 定义成绩样例类
case class Grade(id: Int, name: String, score: Int)
}