| 模式 | 適用場景 | 優點 | 缺點 |
|---|---|---|---|
| spark-submit | 批次離線作業 | 原生、無額外依賴 | 啟動慢、無法即時互動 |
| Livy REST API | 互動式查詢、多租戶 | HTTP/JSON、支援會話、多語言 | 需額外部署 Livy 服務 |
| Spark Job Server | 低延遲、長運行作業 | JAR 熱載入、Context 復用、非同步 | 社群較小、維護頻率低 |
// 1. 提交批次作業
POST /batches
{
"file": "hdfs:///jars/my-spark-app.jar",
"className": "com.example.Main",
"args": ["arg1", "arg2"],
"conf": {"spark.executor.memory": "2g"}
}
// 2. 互動式 Session
POST /sessions
{ "kind": "spark", "conf": {"spark.sql.shuffle.partitions": "10"} }
// 3. 在 Session 中執行程式碼
POST /sessions/0/statements
{ "code": "val df = spark.read.parquet(\"hdfs:///data\")\ndf.show()" }
使用 Livy Java Client 或直接呼叫 HttpClient:
HttpPost post = new HttpPost("https://livy-host:8998/batches");
post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
CloseableHttpResponse resp = client.execute(post);
// 解析 batch ID,輪詢 /batches/{id} 狀態直到 success