一、提出任务
- 已知有以下用户访问历史数据,第一列为用户访问网站的日期,第二列为用户名:
2022-01-01,mike
2022-01-01,alice
2022-01-01,brown
2022-01-02,mike
2022-01-02,alice
2022-01-02,green
2022-01-03,alice
2022-01-03,smith
2022-01-03,brian
- 现需要根据上述数据统计每日新增的用户数量,期望统计结果:
2022-01-01,3
2022-01-02,1
2022-01-03,2
二、完成任务
(一)新建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.llj.rdd
包里创建SparkRDDRows
单例对象
package net.llj.rdd
import org.apache.spark.{SparkConf, SparkContext}
object SparkRDDRows {
def main(args: Array[String]): Unit = {
val conf = new SparkConf()
.setAppName("SparkRDDWordCount") //设置应用名称
.setMaster("local[*]") //设置主节点位置
//基于Spark配置对象创建Spark容器
val rdd1 = new SparkContext(conf);
val rdd2 = rdd1.textFile("hdfs://192.168.1.120:9000/data/time.txt") //读取文件
val rdd3 = rdd2.filter(line=> !line.isEmpty)
.map(
line =>{
val fields = line.split(",")
(fields(1),fields(0))
}
)
.groupByKey()
.map(line=>(line._2.min,1))
val result = rdd3.countByKey()
result.keys.foreach(key => println(key + "," + result(key)))
//停止Spark容器,结束任务
rdd1.stop()
}
}