# Spark-submit

spark submit 的動作可以把spark job 上 cluster 去執行。

基本指令如下，可以做一些參數設定，我們也可以在程式中建立 sparkSession(後面會講) 的時候做設定，先舉個例子

`spark-submit \`

`--master yarn \`

`--deploy-mode client \`

`--num-executors 40 \`

`--driver-memory 16g \`

`--executor-memory 16g \`

`--executor-cores 4 \`

`--queue high \`

`--conf spark.default.parallelism=600`

逐一介紹一下

> * **--master :** 指你的 spark job 要執行在什麼環境，這邊跑在 yarn 上所以指定 yarn ；倘若要在本地跑，也可以指定 local 或是 local\[\*]，前者沒有平行化運算，只會有一個 Worker，後者則是有多少個 cores 就用多少個 cores。
> * **--deploy-mode :** 這裡設定你的 driver 要部屬在哪裡，在本地的設 "client"，在叢集的設 "cluster"。
> * **--num-excecutors :** executor 的數量，相當於 configure 中設定的 spark.executor.instances。
> * **--driver-memory :** driver 的 記憶體大小，相當於 configure 中設定的 spark.driver.memory；在 client mode 的時候要注意，一定要用這個指令或是寫在 .conf 裡頭，因為在 client mode，driver在 submit 的時候就已經被建立起來了。預設是1g。
> * **--excutor-memory :** 每個 executor 的記憶體大小，相當於 configure 中設定的 spark.executor.memory。預設是 1g。
> * **--executor-cores :** 每個 executor 可以用到的 CPU 數量。
> * **--queue :** 在 hadoop，是可以設定 不同的 queue，可以根據需求有不同的配置，用途其實是在於區分 job 的用途或是即時性。
> * **--conf :** \<key>=\<value>，透過這樣的格是設定 參數，例如 : spark.executor.memory=16g
> * \--driver-class-path : 附加檔案到 driver 主要針對 class&#x20;
> * \--jar : 附加 jar 檔，會帶到每個 worker
>
> `spark.default.parallelism :設定的是預設 partition 數量。`\
> `但要注意的是 shuffle 階段，預設會變成 200 個 partitions 。`
>
> `spark.sql.shuffle.partitions :shuffle 階段最後會生成的 partition 數量，預設 200。`
>
> `spark.shuffle.memoryFraction :ExecutionMemory，這快記憶體區域的配置是為了解決 shuffles,joins, sorts and aggregations 過程中為了避免頻繁IO需要的buffer。預設是 0.2`
>
> `spark.storage.memoryFraction :StorageMemory，這塊記憶體區域是為了解決 block cache 以及 broadcasts 、 task results的存儲。預設是 0.2`

reference : <https://spark.apache.org/docs/latest/configuration.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lin-guan-ting.gitbook.io/apache-spark/launch-a-spark-program/spark-submit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
