? ? 最近有興趣且工作需要來(lái)了解下spark蘸吓,首先就是環(huán)境的搭建。一開(kāi)始時(shí)覺(jué)得很困難以至于很早就開(kāi)始搭建有問(wèn)題就先放下了想起來(lái)再接著弄居凶,直到需要的時(shí)候才拾起來(lái)接著搭建嫁佳。再同事的幫助下成功運(yùn)行起來(lái)發(fā)現(xiàn)真的是太簡(jiǎn)單了颊埃,因此記錄下來(lái)以方便他人积蜻。
1.環(huán)境變量配置:
需要先將hadoop下載下來(lái)路徑添加到環(huán)境變量中丙笋;
2.在IDEA上安裝Scala插件
? ? 新建maven項(xiàng)目灶似,在file->settings->plugins右面輸入Scala點(diǎn)擊install鲫懒,再重新啟動(dòng)IDEA里面的scala項(xiàng)就會(huì)變成下圖所示的uninstall,此時(shí)插件安裝成功朦乏。
? ? 在IDEA的src上右鍵點(diǎn)擊new->Directory,輸入scala宙暇,新建一個(gè)scala文件目錄如下圖所示:
3.添加dependency至pom.xml中
? ? 將spark烫葬、scala等版本信息以及spark-hive闪朱、spark-core寓免、spark-streaming欢策、spark-sql鼠冕、spark-streaming-kafka该押、spark-mllib等信息如下所示添加進(jìn)pom.xml中囤躁,在pom.xml上點(diǎn)擊maven->reimport更新maven依賴消玄。
其中可選擇spark版本:
<spark.version>2.3.0.2.6.5.0-292</spark.version>? ? spark2.3.0
<spark.version>1.6.3</spark.version>? ? ? ? ? ? ? ? ? ? ? ?spark1.6.3
<profiles>
? ? ? ? <profile>
? ? ? ? ? ? <id>test</id>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <scope>compile</scope>
? ? ? ? ? ? </properties>
? ? ? ? </profile>
? ? ? ? <profile>
? ? ? ? ? ? <id>package</id>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <scope>provided</scope>
? ? ? ? ? ? </properties>
? ? ? ? </profile>
? ? </profiles>
? ? <properties>
? ? ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? ? <maven.compile.source>1.8</maven.compile.source>
? ? ? ? <maven.compile.target>1.8</maven.compile.target>
? ? ? ? <scala.version>2.10.5</scala.version>
? ? ? ? <!--<spark.version>2.3.0.2.6.5.0-292</spark.version>-->
? ? ? ? <spark.version>1.6.3</spark.version>
? ? ? ? <scala.spark.version>2.10</scala.spark.version>
? ? ? ? <file.name>${project.name}</file.name>
? ? ? ? <!--<scala.tools.version>2.10</scala.tools.version>-->
? ? ? ? <!--<spark.version>1.5.0</spark.version>-->
? ? </properties>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-hive_${scala.spark.version}</artifactId>
? ? ? ? ? ? <version>${spark.version}</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-core_${scala.spark.version}</artifactId>
? ? ? ? ? ? <version>${spark.version}</version>
? ? ? ? ? ? <scope>${scope}</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-streaming_${scala.spark.version}</artifactId>
? ? ? ? ? ? <version>${spark.version}</version>
? ? ? ? ? ? <scope>${scope}</scope>
? ? ? ? </dependency>
? ? ? ? <dependency><!-- Spark Streaming Kafka -->
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-streaming-kafka_2.10</artifactId>
? ? ? ? ? ? <version>1.6.3</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-sql_${scala.spark.version}</artifactId>
? ? ? ? ? ? <version>${spark.version}</version>
? ? ? ? ? ? <scope>${scope}</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.scala-lang</groupId>
? ? ? ? ? ? <artifactId>scala-library</artifactId>
? ? ? ? ? ? <version>${scala.version}</version>
? ? ? ? ? ? <scope>${scope}</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.spark</groupId>
? ? ? ? ? ? <artifactId>spark-mllib_${scala.spark.version}</artifactId>
? ? ? ? ? ? <version>${spark.version}</version>
? ? ? ? ? ? <scope>${scope}</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>io.codis.jodis</groupId>
? ? ? ? ? ? <artifactId>jodis</artifactId>
? ? ? ? ? ? <version>0.4.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>junit</groupId>
? ? ? ? ? ? <artifactId>junit</artifactId>
? ? ? ? ? ? <version>4.12</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.specs</groupId>
? ? ? ? ? ? <artifactId>specs</artifactId>
? ? ? ? ? ? <version>1.2.5</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>cz.mallat.uasparser</groupId>
? ? ? ? ? ? <artifactId>uasparser</artifactId>
? ? ? ? ? ? <version>0.6.0</version>
? ? ? ? </dependency>
? ? </dependencies>
? ? <build>
? ? ? ? <sourceDirectory>src/main/scala</sourceDirectory>
? ? ? ? <finalName>${file.name}</finalName>
? ? ? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-shade-plugin</artifactId>
? ? ? ? ? ? ? ? <version>3.2.0</version>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <phase>package</phase>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>shade</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? </executions>
? ? ? ? ? ? </plugin>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.scala-tools</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-scala-plugin</artifactId>
? ? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>compile</goal>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>testCompile</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? </executions>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <scalaVersion>${scala.version}</scalaVersion>
? ? ? ? ? ? ? ? ? ? <args>
? ? ? ? ? ? ? ? ? ? ? ? <arg>-target:jvm-1.7</arg>
? ? ? ? ? ? ? ? ? ? </args>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-eclipse-plugin</artifactId>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <downloadSources>true</downloadSources>
? ? ? ? ? ? ? ? ? ? <buildcommands>
? ? ? ? ? ? ? ? ? ? ? ? <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
? ? ? ? ? ? ? ? ? ? </buildcommands>
? ? ? ? ? ? ? ? ? ? <additionalProjectnatures>
? ? ? ? ? ? ? ? ? ? ? ? <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
? ? ? ? ? ? ? ? ? ? </additionalProjectnatures>
? ? ? ? ? ? ? ? ? ? <classpathContainers>
? ? ? ? ? ? ? ? ? ? ? ? <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
? ? ? ? ? ? ? ? ? ? ? ? <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
? ? ? ? ? ? ? ? ? ? </classpathContainers>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
4.最后配置
? ? 最后在測(cè)試時(shí),點(diǎn)開(kāi)最右側(cè)的Maven Project折柠,將頂部的Profiles里面的test選上。配置結(jié)束困乒,就可以寫(xiě)測(cè)試用例測(cè)試了。??
5測(cè)試程序
? ? ?在scala目錄下新建Scala Class膊升,并在Kind處選擇Object,測(cè)試代碼如下:
def main(args: Array[String]):Unit = {
//屏蔽日志
//? ? System.setProperty("hadoop.home.dir", "D:\\hadoop");//設(shè)置hadoop環(huán)境
? val conf0 =new SparkConf().setAppName("Base").setMaster("local[4]")
val sc =new SparkContext(conf0); //聲明一個(gè)sparkContext上下文
? val inputfile ="C:\\Users\\dong.shan\\Documents\\WXWork\\1688852247359795\\Cache\\File\\2018-10\\pom.xml"
? val textfile = sc.textFile(inputfile)
//? ? 查詢包含hello world的行
//? ? val lines = textfile.filter(line => line.contains("hello world"))
? textfile.foreach(println)
println("finished!")
}