- Apache Spark 教程
- Apache Spark - 首頁
- Apache Spark - 簡介
- Apache Spark - RDD
- Apache Spark - 安裝
- Apache Spark - 核心程式設計
- Apache Spark - 部署
- 高階 Spark 程式設計
- Apache Spark 有用資源
- Apache Spark - 快速指南
- Apache Spark - 有用資源
- Apache Spark - 討論
Apache Spark - 部署
Spark 應用程式使用 `spark-submit` 命令列工具部署到叢集。它透過統一的介面使用所有相應的叢集管理器,因此無需為每個管理器單獨配置應用程式。
示例
讓我們以之前用過的單詞計數為例,使用shell命令。這裡我們將相同的例子作為一個Spark應用程式。
示例輸入
以下文字是輸入資料,檔名是 in.txt。
people are not as beautiful as they look, as they walk or as they talk. they are only as beautiful as they love, as they care as they share.
檢視以下程式:
SparkWordCount.scala
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark._
object SparkWordCount {
def main(args: Array[String]) {
val sc = new SparkContext( "local", "Word Count", "/usr/local/spark", Nil, Map(), Map())
/* local = master URL; Word Count = application name; */
/* /usr/local/spark = Spark Home; Nil = jars; Map = environment */
/* Map = variables to work nodes */
/*creating an inputRDD to read text file (in.txt) through Spark context*/
val input = sc.textFile("in.txt")
/* Transform the inputRDD into countRDD */
val count = input.flatMap(line ⇒ line.split(" "))
.map(word ⇒ (word, 1))
.reduceByKey(_ + _)
/* saveAsTextFile method is an action that effects on the RDD */
count.saveAsTextFile("outfile")
System.out.println("OK");
}
}
將上述程式儲存到名為 SparkWordCount.scala 的檔案中,並將其放置在名為 spark-application 的使用者定義目錄中。
注意:將 inputRDD 轉換為 countRDD 時,我們使用 flatMap() 將文字檔案中的行標記化為單詞,使用 map() 方法計算單詞頻率,並使用 reduceByKey() 方法計算每個單詞的重複次數。
請按照以下步驟提交此應用程式。在終端透過 spark-application 目錄執行所有步驟。
步驟 1:下載 Spark Jar
編譯需要 Spark core jar 包,因此,請從以下連結下載 spark-core_2.10-1.3.0.jar Spark core jar 並將 jar 檔案從下載目錄移動到 spark-application 目錄。
步驟 2:編譯程式
使用以下命令編譯上述程式。此命令應從 spark-application 目錄執行。這裡,/usr/local/spark/lib/spark-assembly-1.4.0-hadoop2.6.0.jar 是從 Spark 庫中獲取的 Hadoop 支援 jar 包。
$ scalac -classpath "spark-core_2.10-1.3.0.jar:/usr/local/spark/lib/spark-assembly-1.4.0-hadoop2.6.0.jar" SparkPi.scala
步驟 3:建立 JAR 包
使用以下命令建立 Spark 應用程式的 jar 檔案。這裡,wordcount 是 jar 檔案的檔名。
jar -cvf wordcount.jar SparkWordCount*.class spark-core_2.10-1.3.0.jar/usr/local/spark/lib/spark-assembly-1.4.0-hadoop2.6.0.jar
步驟 4:提交 Spark 應用程式
使用以下命令提交 Spark 應用程式:
spark-submit --class SparkWordCount --master local wordcount.jar
如果成功執行,您將看到以下輸出。以下輸出中的 OK 用於使用者標識,它是程式的最後一行。如果您仔細閱讀以下輸出,您會發現不同的內容,例如:
- 成功啟動服務“sparkDriver”在埠 42954 上
- MemoryStore 以 267.3 MB 的容量啟動
- SparkUI 啟動於 http://192.168.1.217:4040
- 已新增 JAR 檔案:/home/hadoop/piapplication/count.jar
- ResultStage 1 (saveAsTextFile at SparkPi.scala:11) 在 0.566 秒內完成
- 停止 Spark web UI 在 http://192.168.1.217:4040
- MemoryStore 已清除
15/07/08 13:56:04 INFO Slf4jLogger: Slf4jLogger started 15/07/08 13:56:04 INFO Utils: Successfully started service 'sparkDriver' on port 42954. 15/07/08 13:56:04 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://sparkDriver@192.168.1.217:42954] 15/07/08 13:56:04 INFO MemoryStore: MemoryStore started with capacity 267.3 MB 15/07/08 13:56:05 INFO HttpServer: Starting HTTP Server 15/07/08 13:56:05 INFO Utils: Successfully started service 'HTTP file server' on port 56707. 15/07/08 13:56:06 INFO SparkUI: Started SparkUI at http://192.168.1.217:4040 15/07/08 13:56:07 INFO SparkContext: Added JAR file:/home/hadoop/piapplication/count.jar at http://192.168.1.217:56707/jars/count.jar with timestamp 1436343967029 15/07/08 13:56:11 INFO Executor: Adding file:/tmp/spark-45a07b83-42ed-42b3b2c2-823d8d99c5af/userFiles-df4f4c20-a368-4cdd-a2a7-39ed45eb30cf/count.jar to class loader 15/07/08 13:56:11 INFO HadoopRDD: Input split: file:/home/hadoop/piapplication/in.txt:0+54 15/07/08 13:56:12 INFO Executor: Finished task 0.0 in stage 0.0 (TID 0). 2001 bytes result sent to driver (MapPartitionsRDD[5] at saveAsTextFile at SparkPi.scala:11), which is now runnable 15/07/08 13:56:12 INFO DAGScheduler: Submitting 1 missing tasks from ResultStage 1 (MapPartitionsRDD[5] at saveAsTextFile at SparkPi.scala:11) 15/07/08 13:56:13 INFO DAGScheduler: ResultStage 1 (saveAsTextFile at SparkPi.scala:11) finished in 0.566 s 15/07/08 13:56:13 INFO DAGScheduler: Job 0 finished: saveAsTextFile at SparkPi.scala:11, took 2.892996 s OK 15/07/08 13:56:13 INFO SparkContext: Invoking stop() from shutdown hook 15/07/08 13:56:13 INFO SparkUI: Stopped Spark web UI at http://192.168.1.217:4040 15/07/08 13:56:13 INFO DAGScheduler: Stopping DAGScheduler 15/07/08 13:56:14 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped! 15/07/08 13:56:14 INFO Utils: path = /tmp/spark-45a07b83-42ed-42b3-b2c2823d8d99c5af/blockmgr-ccdda9e3-24f6-491b-b509-3d15a9e05818, already present as root for deletion. 15/07/08 13:56:14 INFO MemoryStore: MemoryStore cleared 15/07/08 13:56:14 INFO BlockManager: BlockManager stopped 15/07/08 13:56:14 INFO BlockManagerMaster: BlockManagerMaster stopped 15/07/08 13:56:14 INFO SparkContext: Successfully stopped SparkContext 15/07/08 13:56:14 INFO Utils: Shutdown hook called 15/07/08 13:56:14 INFO Utils: Deleting directory /tmp/spark-45a07b83-42ed-42b3b2c2-823d8d99c5af 15/07/08 13:56:14 INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
步驟 5:檢查輸出
程式成功執行後,您將在 spark-application 目錄中找到名為 outfile 的目錄。
以下命令用於開啟和檢查 outfile 目錄中的檔案列表。
$ cd outfile $ ls Part-00000 part-00001 _SUCCESS
檢查 part-00000 檔案中輸出的命令:
$ cat part-00000 (people,1) (are,2) (not,1) (as,8) (beautiful,2) (they, 7) (look,1)
檢查 part-00001 檔案中輸出的命令:
$ cat part-00001 (walk, 1) (or, 1) (talk, 1) (only, 1) (love, 1) (care, 1) (share, 1)
請閱讀下一節以瞭解有關“spark-submit”命令的更多資訊。
Spark-submit 語法
spark-submit [options] <app jar | python file> [app arguments]
選項
| 序號 | 選項 | 描述 |
|---|---|---|
| 1 | --master | spark://host:port, mesos://host:port, yarn 或 local。 |
| 2 | --deploy-mode | 是否在本地 ("client") 或叢集中的一個工作機器上 ("cluster") 啟動驅動程式程式(預設:client)。 |
| 3 | --class | 應用程式的主類(對於 Java/Scala 應用程式)。 |
| 4 | --name | 應用程式的名稱。 |
| 5 | --jars | 要包含在驅動程式和執行程式類路徑中的逗號分隔的本地 jar 檔案列表。 |
| 6 | --packages | 要包含在驅動程式和執行程式類路徑中的 jar 檔案的 Maven 座標的逗號分隔列表。 |
| 7 | --repositories | 用於搜尋使用 --packages 給出的 Maven 座標的其他遠端儲存庫的逗號分隔列表。 |
| 8 | --py-files | 要放在 Python 應用程式的 PYTHON PATH 上的 .zip、.egg 或 .py 檔案的逗號分隔列表。 |
| 9 | --files | 要放在每個執行程式的工作目錄中的檔案的逗號分隔列表。 |
| 10 | --conf (prop=val) | 任意的 Spark 配置屬性。 |
| 11 | --properties-file | 要從中載入額外屬性的檔案的路徑。如果未指定,這將查詢 conf/spark-defaults.conf。 |
| 12 | --driver-memory | 驅動程式的記憶體(例如 1000M、2G)(預設:512M)。 |
| 13 | --driver-java-options | 要傳遞給驅動程式的額外 Java 選項。 |
| 14 | --driver-library-path | 要傳遞給驅動程式的額外庫路徑條目。 |
| 15 | --driver-class-path | 要傳遞給驅動程式的額外類路徑條目。 請注意,使用 --jars 新增的 jar 包會自動包含在類路徑中。 |
| 16 | --executor-memory | 每個執行程式的記憶體(例如 1000M、2G)(預設:1G)。 |
| 17 | --proxy-user | 提交應用程式時要模擬的使用者。 |
| 18 | --help, -h | 顯示此幫助訊息並退出。 |
| 19 | --verbose, -v | 列印額外的除錯輸出。 |
| 20 | --version | 列印當前 Spark 的版本。 |
| 21 | --driver-cores NUM | 驅動程式的核心數(預設:1)。 |
| 22 | --supervise | 如果給出,則在發生故障時重新啟動驅動程式。 |
| 23 | --kill | 如果給出,則終止指定的驅動程式。 |
| 24 | --status | 如果給出,則請求指定的驅動程式的狀態。 |
| 25 | --total-executor-cores | 所有執行程式的總核心數。 |
| 26 | --executor-cores | 每個執行程式的核心數。(預設:在 YARN 模式下為 1,或在獨立模式下為工作程式上的所有可用核心)。 |