Spark的本地模式安裝部署與初體驗

Spark開箱即用,測試使用的是:spark-2.2.0-bin-2.6.0-cdh5.14.0版本。
下載地址:spark-2.2.0-bin-2.6.0-cdh5.14.0

下載

其他版本請訪問apache官方:http://spark.apache.org/downloads.html

local本地模式 - Spark初體驗

上傳與解壓

將壓縮包上傳至Linux後解壓

cd /export/servers
tar -zxvf ./spark-2.2.0-bin-2.6.0-cdh5.14.0.tgz -C ./

圖片內容爲window解壓目錄,只做內容展示效果,具體以linux爲準
bin:可執行腳本
conf:配置文件
data:示例程序使用數據
examples:示例程序
jars:依賴jar包
python:pythonAPI
sbin:集羣管理命令
yarn:整合yarn需要的內容

啓動 spark-shell

直接啓動bin目錄下的spark-shell
說明:

  • 直接使用 ./spark-shell表示使用local模式啓動,在本機啓動一個sparksubmit進程
  • 還可指定參數 --master,如:
    • spark-shell --master local[N]表示在本地模擬N個線程來運行當前任務
    • spark-shell --master local[*]表示使用當前機器上所有可用的資源
  • 不攜帶參數默認就是:spark-shell --master local[*]
  • 如部署spark集羣模式,還可以使用--master spark://host:port把任務提交到集羣上運行
  • 退出spark-shell使用::quit

Spark初體驗 - 讀取本地文件

準備數據:vim /root/words.txt
hello me you her
hello you her
hello her
hello

val textFile = sc.textFile("file:///root/words.txt")
val counts = textFile.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).collect

結果:

Array[(String, Int)] = Array((you,2), (hello,4), (me,1), (her,3))

Spark初體驗 - 讀取HDFS文件

準備數據:
將文件上傳至hdfs:hadoop fs -put /root/words.txt /wordcount/input/words.txt

val textFile = sc.textFile("hdfs://node01:8020/wordcount/input/words.txt")
val counts = textFile.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)
counts.saveAsTextFile("hdfs://node01:8020/wordcount/output")

訪問HDFS輸出路徑查看結果
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
輸出爲兩個文件的原因是:spark默認的分區數量爲2個

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章