使用scala編寫一個簡單例項到spark叢集執行
實際工作上很少在虛擬機器上直接使用spark-shell去編寫程式,更多的是在IDEA等編輯器上將寫好的程式打包,使用spark-submit提交到叢集上去執行。
我們使用scala去編寫程式,不會的自己百度學下,不解釋。
1,安裝jdk
因為scala也是執行在jvm上的,所以需要安裝jdk。(jdk安裝方法不解釋,自己百度,建議安裝1.7以上版本)
2,安裝scala
筆者安裝的是scala 2.10.6版本,需要jdk1.7及以上版本支援。
設定系統變數,新增一個SCALA_HOME,設定值為SCALA指定的安裝目錄,
在Path路徑的末尾加
;%SCALA_HOME%\bin;%SCALA_HOME%\jre\bin;
在CLASSPATH路徑末尾新增
;%SCALA_HOME%\bin;%SCALA_HOME%\lib\dt.jar;%SCALA_HOME%\lib\tools.jar.;
配置後按 win + R 輸入cmd 召喚視窗輸入 scala -version ,檢視是否配置安裝成功。
3,在IDEA上使用編寫程式
建立一個maven專案,建立過程不解釋,提供一個我的 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>cn.itcast.spark</groupId> <artifactId>hello-spark</artifactId> <version>1.0</version> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <encoding>UTF-8</encoding> <scala.version>2.10.6</scala.version> <spark.version>1.6.1</spark.version> <hadoop.version>2.6.4</hadoop.version> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
編寫一個wordCount小程式,程式碼如下:
package cn.itcast.spark import org.apache.spark. {SparkConf, SparkContext} /** * Created by mrwanghc on 2018/7/17. */ object WordCount { def main(args: Array[String]) { val conf = new SparkConf().setAppName("WC") val sc = new SparkContext(conf) sc.textFile(args(0)).flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,false).saveAsTextFile(args(1)) sc.stop() } }
寫完後將專案打包:
這裡會生成兩個包,一個是隻包含程式碼的簡潔包,另一個是包含jar包依賴的大包,保險起見我們使用打包即可。
將包上傳到spark叢集上, spark叢集的搭建啟動這裡不解釋,之前文章有詳解,請自行檢視。
在spark目錄下輸入命令
bin/spark-submit --master spark://weekend02:7077 --class cn.itcast.spark.WordCount --executor-memory 512m --total-executor-cores 2 /home/bigdata/hello-spark-1.0.jar hdfs://weekend02:9000/wc hdfs://weekend02:9000/out2
--master 指定叢集master
--class 指定類所在地址
--executor-memory 512m 指定每個work執行記憶體為512m
--total-executor-cores 2 指定總共提供2個核處理給所有work
/home/bigdata/hello-spark-1.0.jar 提供上傳的jar包所在目錄
hdfs://weekend02:9000/wc 提供所需分析的檔案所在hdfs中的目錄
hdfs://weekend02:9000/out2 提供處理完後的檔案要放到hdfs中某目錄
輸入完這條命令回車執行,若不報錯,則完活!