(一)新建Maven项目
- 设置项目信息(项目名、保存位置、组编号、项目编号)
- 将java目录改成scala目录
(二)添加相关依赖和构建插件
- 在pom.xml文件里添加依赖与Maven构建插件
<?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.llj.spark</groupId>
<artifactId>SparkGradeTopNSQL</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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>
(三)创建日志属性文件
- 在资源文件夹里创建日志属性文件 -
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.huawei.sql包里创建GradeTopNSQL单例对象
package net.llj.sql
import org.apache.spark.sql.{Dataset, SparkSession}
object GradeTopNSQL {
case class Score(name:String,score: Int)
def main(args: Array[String]): Unit = {
//创建或得到Spark会话对象
val spark = SparkSession.builder()
.appName("GradeTopNSQL")
.master("local[*]")
.getOrCreate()
//读取HDFS上的成绩文件
val lines:Dataset[String] = spark.read.textFile("hdfs://192.168.1.120:9000/input/grades.txt")
//显示数据集内容
// lines.show()
//导入隐式转换
import spark.implicits._
//定义成绩样例类
//创建成绩数据集
val gradeDS =lines.map(
line => { val fields = line.split(" ")
val name =fields(0)
val score = fields(1).toInt
Score(name,score)
}
)
//显示
// gradeDS.show()
//将数据集转换成数据帧
val df = gradeDS.toDF()
//显示数据帧的内容
// df.show()
//基于数据帧创建临时表
df.createOrReplaceTempView("t_grade")
//查询临时表,得到分组排行榜
val top3 = spark.sql(
"""
|SELECT name,score FROM
| (SELECT name,score,row_number() OVER (PARTITION BY name ORDER BY score DESC) rank from t_grade) t
| WHERE t.rank<=3
|""".stripMargin
)
//
top3.show()
}
}
(五)本地运行程序,查看结果
-
原数据为:
-
处理排序后的数据为: