简介:本篇博客主要数用于Spark安装用来测试用的。
1、创建一个maven工程
详细结构如上图
2、配置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>com.lqs</groupId>
<artifactId>WordCount</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.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>WordCount</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
打包给集群运行配置
<?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>com.lqs</groupId>
<artifactId>WordCount</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.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>WordCount</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包依赖的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.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>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.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>
</plugins>
</build>
</project>
3、本地运行Spark代码
package com.lqs.spark
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* @Author lqs
* @Date 2022年01月24日 10:20:39
* @Version 1.0.0
* @ClassName WordCount
* @Describe
*/
object WordCount {
def main(args: Array[String]): Unit = {
//创建SparkConf并设置APP名称
val conf: SparkConf = new SparkConf().setAppName("WordCount").setMaster("local[*]")
//2创建SparkContext,该对象是提交Spark APP的人口
val context: SparkContext = new SparkContext(conf)
//需要使用行动算子,读取指定位置文件
val lineRdd: RDD[String] = context.textFile("input")
//读取的一行一行的数据分解成一个一个的单词(扁平化)
val wordRdd: RDD[String] = lineRdd.flatMap(line => line.split(" "))
//将数据转换结构
val wordToOneRdd: RDD[(String, Int)] = wordRdd.map(word => (word, 1))
//将转换结构后的数据进行聚合处理
val wordToSumRdd: RDD[(String, Int)] = wordToOneRdd.reduceByKey((v1, v2) => v1 + v2)
//将统计结构采集到控制台打印
val wordToCountArray: Array[(String, Int)] = wordToSumRdd.collect()
wordToCountArray.foreach(println)
//一行写完,打包时也用本行代码,把其它一样的注释掉即可
// context.textFile(args(0)).flatMap(_.split(" ")).map((_,1)).reduceByKey(_ + _).saveAsTextFile(args(1))
//暂停一段时间,好在http://lqs:4040访问
// Thread.sleep(1000000)
//关闭连接
context.stop()
}
}
4、集群运行打包代码
package com.lqs.spark
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* @Author lqs
* @Date 2022年01月24日 10:20:39
* @Version 1.0.0
* @ClassName WordCount
* @Describe
*/
object WordCount {
def main(args: Array[String]): Unit = {
//创建SparkConf并设置APP名称
val conf: SparkConf = new SparkConf().setAppName("WordCount").setMaster("local[*]")
//2创建SparkContext,该对象是提交Spark APP的人口
val context: SparkContext = new SparkContext(conf)
//一行写完,打包时也用本行代码,把其它一样的注释掉即可
context.textFile(args(0)).flatMap(_.split(" ")).map((_,1)).reduceByKey(_ + _).saveAsTextFile(args(1))
context.stop()
}
}