大家好,我是不溫卜火,是一名計算機學院大數據專業大二的學生,暱稱來源於成語—
不溫不火
,本意是希望自己性情溫和
。作爲一名互聯網行業的小白,博主寫博客一方面是爲了記錄自己的學習過程,另一方面是總結自己所犯的錯誤希望能夠幫助到很多和自己一樣處於起步階段的萌新。但由於水平有限,博客中難免會有一些錯誤出現,有紕漏之處懇請各位大佬不吝賜教!暫時只有csdn這一個平臺,博客主頁:https://buwenbuhuo.blog.csdn.net/
此篇爲大家帶來的是Spark 運行模式之Local本地模式。
目錄
Local 模式就是指的只在一臺計算機上來運行 Spark.
通常用於測試的目的來使用 Local 模式, 實際的生產環境中不會使用 Local 模式.
一. 解壓 Spark 安裝包
- 1. 把安裝包上傳到
/opt/software/
- 2. 把安裝包上傳到
/opt/module/
[bigdata@hadoop002 software]$ tar -zxvf spark-2.1.1-bin-hadoop2.7.tgz -C /opt/module
- 3. 重命名爲spark-local(爲了方便複製一個並重新命名)
[bigdata@hadoop002 module]$ cp -r spark-2.1.1-bin-hadoop2.7 spark-local
- 4. 注意
如果有權限問題,可以修改爲root,方便學習時操作,實際中使用運維分配的用戶和權限即可。
chown -R root /opt/module/spark-local
chgrp -R root /opt/module/spark-local
- 5. 解壓目錄說明
bin 可執行腳本
conf 配置文件
data 示例程序使用數據
examples 示例程序
jars 依賴 jar 包
python pythonAPI
R R 語言 API
sbin 集羣管理命令
yarn 整合yarn需要的文件
二. 運行官方求PI的案例
[bigdata@hadoop002 spark-local]$ bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master local[2] \
./examples/jars/spark-examples_2.11-2.1.1.jar 100
注意:
如果你的shell
是使用的zsh
, 則需要把local[2]
加上引號:'local[2]'
說明:
使用spark-submit
來發布應用程序.
2.1 spark-submit
語法
./bin/spark-submit \
--class <main-class> \
--master <master-url> \
--deploy-mode <deploy-mode> \
--conf <key>=<value> \
... # other options
<application-jar> \
[application-arguments]
- –
master
指定master
的地址,默認爲local
. 表示在本機運行. - –
class
你的應用的啓動類 (如org.apache.spark.examples.SparkPi
) - –
deploy-mode
是否發佈你的驅動到worker
節點(cluster
模式) 或者作爲一個本地客戶端 (client
模式) (default: client
) - –
conf
: 任意的 Spark 配置屬性, 格式key=value
. 如果值包含空格,可以加引號"key=value"
application-jar
: 打包好的應用jar
,包含依賴. 這個URL
在集羣中全局可見。 比如hdfs:// 共享存儲系統
,
如果是file:// path
, 那麼所有的節點的path
都包含同樣的jar
application-arguments
: 傳給main()
方法的參數- –
executor-memory 1G
指定每個executor
可用內存爲1G
- –
total-executor-cores 6
指定所有executor
使用的cpu
核數爲6個 - –
executor-cores
表示每個executor
使用的cpu
的核數
2.2 關於 Master URL
的說明
Master URL | Meaning |
---|---|
local |
Run Spark locally with one worker thread (i.e. no parallelism at all). |
local[K] |
Run Spark locally with K worker threads (ideally, set this to the number of cores on your machine). |
local[*] |
Run Spark locally with as many worker threads as logical cores on your machine. |
spark://HOST:PORT |
Connect to the given Spark standalone cluster master. The port must be whichever one your master is configured to use, which is 7077 by default. |
mesos://HOST:PORT |
Connect to the given Mesos cluster. The port must be whichever one your is configured to use, which is 5050 by default. Or, for a Mesos cluster using ZooKeeper, use mesos://zk://… To submit with --deploy-mode cluster, the HOST:PORT should be configured to connect to the MesosClusterDispatcher. |
yarn |
Connect to a YARNcluster in client or cluster mode depending on the value of --deploy-mode. The cluster location will be found based on the HADOOP_CONF_DIR or YARN_CONF_DIR variable. |
2.3 結果展示
2.4 另一種方法
也可以使用run-examples來運行
[bigdata@hadoop002 spark-local]$bin/run-example SparkPi 100
三. 使用 Spark-shell
Spark-shell
是 Spark
給我們提供的交互式命令窗口(類似於 Scala 的 REPL)
本案例在 Spark-shell
中使用 Spark
來統計文件中各個單詞的數量.
3.1 創建 2 個文本文件
mkdir input
cd input
touch 1.txt
touch 2.txt
3.2 打開 Spark-shell
[bigdata@hadoop002 spark-local]$ bin/spark-shell
3.3 查看進程和通過 web 查看應用程序運行情況
3.4 運行 wordcount
程序
scala> sc.textFile("./input").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).collect
3.5 登錄hadoop102:4040查看程序運行
四. 提交流程
五. wordcount 數據流程分析
textFile("input")
:讀取本地文件input文件夾數據;flatMap(_.split(" "))
:壓平操作,按照空格分割符將一行數據映射成一個個單詞;map((_,1))
:對每一個元素操作,將單詞映射爲元組;reduceByKey(_+_)
:按照key將值進行聚合,相加;collect
:將數據收集到Driver端展示。
本次的分享就到這裏了,
好書不厭讀百回,熟讀課思子自知。而我想要成爲全場最靚的仔,就必須堅持通過學習來獲取更多知識,用知識改變命運,用博客見證成長,用行動證明我在努力。
如果我的博客對你有幫助、如果你喜歡我的博客內容,請“點贊” “評論”“收藏”
一鍵三連哦!聽說點讚的人運氣不會太差,每一天都會元氣滿滿呦!如果實在要白嫖的話,那祝你開心每一天,歡迎常來我博客看看。
碼字不易,大家的支持就是我堅持下去的動力。點贊後不要忘了關注
我哦!