參考資料:
- https://blog.csdn.net/weixin_43893397/article/details/105289992
- https://zhuanlan.zhihu.com/p/75779188
- https://blog.csdn.net/u010675669/article/details/81744386
- https://blog.csdn.net/weixin_43924642/article/details/89388432
首先將需要的input檔案本地傳至HDFS上(本次程式輸入為words.txt)

- 用spark-shell直接執行
sc.textFile("/words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).collect().foreach(println)
即可看到結果,(PS:注意路徑都是HDFS路徑)
以下這些是別人的寫法,我這運行不了,卡死,
sc.textFile("hdfs://linux139report3:7077/words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).collect().foreach(println)
另外sc.textFile的路徑默認是HDFS路徑,也可以用本地路徑(測驗通過,但有warning)
sc.textFile("file://home/mr/words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).collect().foreach(println)
也可以指定HDFS路徑,測驗通過
sc.textFile("hdfs:///words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).collect().foreach(println)
可以將結果輸出到HDFS中
sc.textFile("/words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).saveAsTextFile("/out")
運行之后在HDFS中的/out看到計算結果
2、在本地IDEA上撰寫程式,打包到集群上運行
準備好版本
IDEA 2019.3
Scala插件scala-intellij-bin-2019.3.23.zip(不太清楚有沒有用,但我裝了)
ScalaSDK 2.11.8(https://www.scala-lang.org/download/2.11.8.html)
安裝程序可參考
https://blog.csdn.net/u013973379/article/details/82826034
(以下是我的安裝程序)
下載Scala插件并安裝(我無法在線自動安裝,官網上下載下來手動安裝了,結果如下)

建立專案檔案

修改pom.xml
一般來說,這兩項我們是必須加上的
<properties>
<spark.version>2.2.1</spark.version>
<scala.version>2.11.8</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</dependency>
<properties>
<spark.version>2.2.1</spark.version>
<scala.version>2.11.8</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</depen
編(fu)寫(zhi)Scala程式

打包找到共兩種方法:
第一種(經過第一二步之后就有左邊的jar包)

第二種(我選這種):
file->Project Structure->Artifacts

一定要注意wc.jar的包中,Manifest File路徑和Main Class要正確,
之后build即可,Build->Build Artifacts->wc.jar->build
打包時候一定要注意,這個問題坑爹啊!
https://www.cnblogs.com/chenjfblog/p/10166331.html
打包時候出現Error: Error compiling the sbt component
https://blog.csdn.net/weixin_45793819/article/details/107243819
打包好之后,我們在路徑中找到jar包,打開jar取出其中的wc.jar(我們只需要這個即可,其余是依賴包)

在裝有spark的node上執行:
(PS:每個換行是一個空格,拉成一行執行)
spark-submit
--class wordcount
--master spark://linux139report3:7077
--executor-memory 1G
--total-executor-cores 2
/home/mr/wc.jar
/words.txt
/out10000
spark-submit
--class [classname]
--master spark://[nodename]:7077
--executor-memory [source size]
--total-executor-cores [core number]
/home/mr/wc.jar [local path:jar name]
/words.txt [HDFS path+file name]
/out10000 [HDFS path/output path]

最后,執行成功之后,查看結果檔案/out10000,里面就已經是計算結果了,可能存在多個檔案里面,

那究竟為何輸出是兩個檔案呢??我這給出我的想法
如果采用的是HashPartitioner磁區方法則會根據Key值進行磁區,Key一樣的可能分到一個磁區了,(PS:真的不太確定)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287896.html
標籤:其他
上一篇:全網最全Linux命令總結!!(史上最全,建議收藏)
下一篇:環境大資料MapReduce
