在IDEA中使用Scala語言編寫WordCount程式
阿新 • • 發佈:2018-11-10
1.使用IDEA建立Maven專案
2.匯入pom.xml檔案
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <scala.version>2.11.8</scala.version> <spark.version>2.1.0</spark.version> <hadoop.version>2.6.0</hadoop.version> <encoding>UTF-8</encoding> </properties> <dependencies> <!-- 匯入scala的依賴 --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!-- 匯入spark的依賴 --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency> <!-- 指定hadoop-client API的版本 --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <!-- 編譯scala的外掛 --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</version> </plugin> <!-- 編譯java的外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!-- 打jar外掛 --> <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>
注意: 這裡的Scala Spark Hadoop版本必須按照叢集上的修改,特別是Scala和Spark的,要和你叢集上的版本號一致,可以在Spark叢集中使用Spark Shell模式檢視版本號
3.編寫WordCount程式
package cn.ysjh0014 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object ScalaWordCount { def main(args: Array[String]): Unit = { //建立Spark配置,應用程式的名字 val conf = new SparkConf().setAppName("ScalaWordCount") //建立Spark程式執行的入口 val sc = new SparkContext(conf) //指定以後從哪讀取資料建立RDD(彈性分散式資料集) val line = sc.textFile(args(0)) //切分壓平 val word = line.flatMap(_.split(" ")) //將單詞和1組成元組 val WordOne = word.map((_, 1)) //按照key進行聚合 val reduce = WordOne.reduceByKey(_ + _) //排序 val sort = reduce.sortBy(_._2, false) //將結果儲存到hdfs sort.saveAsTextFile(args(1)) //釋放資源 sc.stop() } }
4.使用Maven打成jar包
在IDEA中view---->Tool Windows--->Maven Projects--->Package,jar包在target下,有兩個jar包,original-Spark-1.0-SNAPSHOT.jar是隻將程式碼打成了jar包,Spark-1.0-SNAPSHOT.jar是將所有依賴也打成了jar包
5.提交到Spark叢集上測試
bin/spark-submit \ --master spark://cdh0:7077 \ --class cn.ysjh0014.ScalaWordCount \ 包名+專案名 /opt/package/original-Spark-1.0-SNAPSHOT.jar \ jar包所在目錄 hdfs://cdh0:8020/usr/ys/input/test.txt \ 讀取資料的hdfs路徑 hdfs://cdh0:8020/usr/output 儲存資料到hdfs的路徑
6.檢視執行結果
至此執行成功