1、创建scala maven项目
选择骨架的时候为org.scala-tools.archetypes:scala-aechetype-simple 1.2
2、导入包,进入spark官网Documentation选择第二个-选择自己的spark版本-选择programming guides-选择第二个-查看spark版本对应的依赖的版本
将pom中的内容为:
<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/maven-v4_0_0.xsd"> <parent> <artifactId>bigdata</artifactId> <groupId>com.laotou</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spark_day01_01</artifactId> <dependencies> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>1.6.3</version> </dependency> </dependencies> </project>
代码: object HelloSpark { def main(args: Array[String]): Unit = { //spark主要是做计算 ---> 最终是不是要打包发布到spark集群中 因为集群中要运行多个spark程序,所以事先要给job取个名字 val conf = new SparkConf().setAppName("sparkTest").setMaster("local[2]") val sc = new SparkContext(conf) sc.textFile("D://helloSpark.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).foreach(println(_)) } }
3、如果采用的scala版本与spark要求的版本不一致时,会报错NoSunchMethodError
解决:
修改scala的版本。采用绿色版。(这里是将scala-sdk-2.10.6的压缩包)解压-->手动配置一下。
进去spark官网查看当前操作的spark版本对应的依赖scala版本
Spark runs on Java 7+, Python 2.6+ and R 3.1+. For the Scala API, Spark 1.6.3 uses Scala 2.10. You will need to use a compatible Scala version (2.10.x).
idea选择file-project structure-选择需要处理的项目-选择spark版本对应的dependencies-右边有个+号添加需要的依赖scala版本:scala-sdk-2.10.6