Spark3.2教程(十)编程Scala程序在Standalone集群中运行
2021/12/24 22:07:35
本文主要是介绍Spark3.2教程(十)编程Scala程序在Standalone集群中运行,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如果运行出现以下报错:
Exception in thread "main" java.lang.NoSuchMethodError: scala.reflect.ClassTag$.Int()Lscala/reflect/ManifestFactory$IntManifest;
是Scala版本不匹配造成,请参见:
Spark3.2教程(前置)关于Spark3.2.0与Scala版本的坑
在IDEA中开发:
package com.alan import org.apache.spark.{SparkConf, SparkContext} object Test1 { def main(args: Array[String]): Unit = { //创建SparkContexta //本地运行 // val conf=new SparkConf().setMaster("local[2]").setAppName("WordCount") //提交运行 val conf = new SparkConf().setAppName("WordCound"); val sc = new SparkContext(conf) //加载文件 val file = sc.textFile("hdfs://hp301:9000/ee/b.txt") //val file=sc.textFile("file:///d:/test/words.txt") //处理 val flatFile = file.flatMap(item => item.split(" ")) val flatFileMap = flatFile.map(item => (item, 1)) val aggCount = flatFileMap.reduceByKey((curr, agg) => curr + agg) //得到结果 aggCount.foreach(item => println(item)) } }
注意:本次是从HDFS中取数据进行计算,如果没有配置Hadoop,可以改成从Linux本地读取。
配置pom.xml中打包插件,为了将依赖都打入包中:
<bulid> <!--指定源码包和测试包的位置--> <sourceDirectory>src/main/scala</sourceDirectory> <plugins> <!-- 该插件用于将 Scala 代码编译成 class 文件 --> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <!-- 声明绑定到 maven 的 compile 阶段 --> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-target:jvm-1.8</arg> </args> </configuration> </execution> </executions> </plugin> <!--maven打包的插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.alan.Test1</mainClass> </manifest> </archive> <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>
双击package命令,执行Maven打包:
将打好的包上传到linux的/apps文件夹下,执行命令:
spark-submit --master spark://hp301:7077 /apps/test_s2-1.0-SNAPSHOT-jar-with-dependencies.jar
在Web UI中可以看到结果:
这篇关于Spark3.2教程(十)编程Scala程序在Standalone集群中运行的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03如何安装 App 并连接到飞牛 NAS?-icode9专业技术文章分享
- 2024-10-03如何安装飞牛 TV 并连接到影视服务器?-icode9专业技术文章分享
- 2024-10-03如何在PVE和ESXI上安装飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS安装系统异常情况处理-icode9专业技术文章分享
- 2024-10-03飞牛NAS如何创建存储空间?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS硬盘会自动休眠吗?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何安装飞牛影视和创建媒体库?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何为家人朋友开通影视账号?-icode9专业技术文章分享