環境:
scala:2.12.10
spark:3.0.3
1、創建scala maven項目,如下圖所示:
2、
不同版本scala編譯參數可能略有不同,筆者使用的scala版本是2.12.10,scala-archetype-simple插件生成的pom文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<plugin> <groupid>org.scala-tools</groupid> <artifactid>maven-scala-plugin</artifactid> <version> 2.15 . 0 </version> <executions> <execution> <goals> <goal>compile</goal> <goal>testcompile</goal> </goals> <configuration> <args> <arg>-make:transitive</arg> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args> </configuration> </execution> </executions> </plugin> |
要去除-make:transitive這個參數,否則會報錯。
3、創建sparkpi object類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
object sparkpi { def main(args: array[string]): unit = { val spark = sparksession .builder .appname( "spark pi" ) .master( "spark://172.21.212.114:7077" ) .config( "spark.jars" , "e:\\work\\polaris\\polaris-spark\\spark-scala\\target\\spark-scala-1.0.0.jar" ) .config( "spark.executor.memory" , "2g" ) .config( "spark.cores.max" , "2" ) .config( "spark.driver.host" , "172.21.58.28" ) .config( "spark.driver.port" , "9089" ) .getorcreate() //spark = new sparkcontext(conf). val slices = if (args.length > 0 ) args( 0 ).toint else 2 val n = math.min(100000l * slices, int .maxvalue).toint // avoid overflow val count = spark.sparkcontext.parallelize( 1 until n, slices).map { i => val x = random * 2 - 1 val y = random * 2 - 1 if (x*x + y*y <= 1 ) 1 else 0 }.reduce(_ + _) println(s "pi is roughly ${4.0 * count / (n - 1)}" ) spark.stop() } } |
4、執行打包命令:
5、點擊idea run執行即可:
6、結果如下所示:
ps:
1、創建sparksession時需要指定idea所在機器ip地址,因為默認會把spark driver所在機器域名發送過去,導致無法解析(在spark 服務器上配置idea所在機器域名也可以,但是這樣太不靈活)
2、spark-3.0.3默認使用的scala版本是2.12.10,所以要注意idea使用scala版本,否則會出現serailizableid不一致的兼容問題
到此這篇關于idea創建spark maven項目并連接遠程spark集群的文章就介紹到這了,更多相關idea spark集群內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/john1337/article/details/119995531