spark 提交任务参数说明

本文介绍如何使用spark-submit命令提交Spark应用程序,并通过不同集群管理器的示例展示配置选项。同时提供了一个简单的统计任务示例。
[size=medium]1.参数选取

当我们的代码写完,打好jar,就可以通过bin/spark-submit 提交到集群,命令如下:

./bin/spark-submit \
--class <main-class>
--master <master-url> \
--deploy-mode <deploy-mode> \
--conf <key>=<value> \
... # other options
<application-jar> \
[application-arguments]
一般情况下使用上面这几个参数就够用了

--class: The entry point for your application (e.g. org.apache.spark.examples.SparkPi)

--master: The master URL for the cluster (e.g. spark://23.195.26.187:7077)

--deploy-mode: Whether to deploy your driver on the worker nodes (cluster) or locally as an external client (client) (default: client) †

--conf: Arbitrary Spark configuration property in key=value format. For values that contain spaces wrap “key=value” in quotes (as shown).

application-jar: Path to a bundled jar including your application and all dependencies. The URL must be globally visible inside of your cluster, for instance, an hdfs:// path or a file:// path that is present on all nodes.

application-arguments: Arguments passed to the main method of your main class, if any

对于不同的集群管理,对spark-submit的提交列举几个简单的例子

# Run application locally on 8 cores

./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master local[8] \
/path/to/examples.jar \
100

# Run on a Spark standalone cluster in client deploy mode

./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://207.184.161.138:7077 \
--executor-memory 20G \
--total-executor-cores 100 \
/path/to/examples.jar \
1000

# Run on a Spark standalone cluster in cluster deploy mode with supervise
# make sure that the driver is automatically restarted if it fails with non-zero exit code

./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://207.184.161.138:7077 \
--deploy-mode cluster
--supervise
--executor-memory 20G \
--total-executor-cores 100 \
/path/to/examples.jar \
1000

# Run on a YARN cluster export HADOOP_CONF_DIR=XXX

./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master yarn-cluster \ # can also be `yarn-client` for client mode
--executor-memory 20G \
--num-executors 50 \
/path/to/examples.jar \
1000

# Run a Python application on a Spark standalone cluster

./bin/spark-submit \
--master spark://207.184.161.138:7077 \
examples/src/main/python/pi.py \
1000
2.具体提交步骤

代码实现一个简单的统计

public class SimpleSample {
public static void main(String[] args) {
String logFile = "/home/bigdata/spark-1.5.1/README.md";
SparkConf conf = new SparkConf().setAppName("Simple Application");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> logData = sc.textFile(logFile).cache();

long numAs = logData.filter(new Function<String, Boolean>() {
public Boolean call(String s) {
return s.contains("a");
}
}).count();

long numBs = logData.filter(new Function<String, Boolean>() {
public Boolean call(String s) {
return s.contains("b");
}
}).count();

System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);
}

}
打成jar


上传命令

./bin/spark-submit --class cs.spark.SimpleSample --master spark://spark1:7077 /home/jar/spark-test-0.0.1-SN[/size]

本文转自:https://my.oschina.net/u/2529303/blog/541685
<think> 我们被要求回答关于Spark提交任务参数问题,并参考了提供的引用内容。根据引用[1]和引用[2],我们可以整理出一些常用的spark-submit参数。同时,引用[3]也提供了一些例子,但这里我们主要关注参数列表。 注意:用户要求使用中文回答,并且按照指定的格式要求(如数学公式的格式等),但本问题不涉及数学公式,因此我们主要关注参数列表的描述。 根据引用[1]和引用[2],常用的参数包括: --master: 指定集群的master URL(例如:spark://host:port, yarn, local[*]等) --deploy-mode: 部署模式,可以是client或cluster --name: 应用的名称 --conf: 指定额外的Spark配置属性(key=value) --py-files: 用于Python应用,指定额外的.zip, .egg, .py文件 --class: 指定主类(对于Java/Scala应用) --executor-memory: 每个executor的内存大小(例如:2g) --executor-cores: 每个executor的CPU核心数 --num-executors: 启动的executor数量 --driver-memory: driver程序的内存大小 --driver-cores: driver程序的核心数(在cluster模式下,driver运行在集群上,这个参数才有效) 此外,引用[2]还提到了可以传递应用参数(放在最后)。 我们按照用户要求的结构清晰、逐步回答,并保证真实可靠(基于提供的引用)。 注意:引用标记应放在引用内容的末尾,如[^1]。 </think> ### Spark 提交任务的常用参数详解 Spark 提交任务时通过 `spark-submit` 命令配置参数,主要分为**核心参数**、**资源参数**和**高级配置**三类: #### 一、核心参数 1. `--master`:集群管理器地址 - `local[*]`:本地模式(* 表示使用所有核心) - `yarn`:YARN 集群模式 - `spark://host:port`:Standalone 模式 - `k8s://host:port`:Kubernetes 模式 [^3] 2. `--deploy-mode`:Driver 部署位置 - `client`(默认):Driver 在提交节点运行 - `cluster`:Driver 在集群的 Worker 节点运行 [^1] 3. `--name`:应用名称(显示在集群监控界面) ```bash --name "My_Spark_Job" ``` 4. `--class`:主程序入口类(Java/Scala 应用) ```bash --class org.example.MainApp ``` 5. `--py-files`:Python 依赖文件 - 支持 `.py`, `.zip`, `.egg` 文件 ```bash --py-files libs.zip,utils.py ``` #### 二、资源参数 1. **Driver 配置** - `--driver-memory`:Driver 内存(默认 1g) ```bash --driver-memory 4g ``` - `--driver-cores`:Driver 的 CPU 核心数 [^2] 2. **Executor 配置** - `--executor-memory`:单个 Executor 内存 ```bash --executor-memory 2g ``` - `--executor-cores`:单个 Executor 的 CPU 核心数(默认 1) - `--num-executors`:Executor 数量(YARN 模式专用) ```bash --num-executors 10 ``` #### 三、高级配置(`--conf`) 使用 `key=value` 格式覆盖默认配置: 1. **动态资源分配** ```bash --conf spark.dynamicAllocation.enabled=true ``` 2. **Shuffle 优化** ```bash --conf spark.sql.shuffle.partitions=200 ``` 3. **网络超时设置** ```bash --conf spark.network.timeout=300s ``` 4. **Kubernetes 专用配置** ```bash --conf spark.kubernetes.container.image=spark:3.4 ``` #### 完整提交示例(YARN 模式) ```bash bin/spark-submit \ --master yarn \ --deploy-mode cluster \ --name "Data_ETL_Job" \ --executor-memory 4g \ --executor-cores 2 \ --num-executors 20 \ --conf spark.sql.shuffle.partitions=1000 \ --py-files dependencies.zip \ /app/main.py ``` > **关键说明**: > - 资源参数需根据集群实际资源调整,避免超额申请 > - `cluster` 模式适合生产环境,`client` 模式便于调试 [^1] > - GPU 资源需添加 `--conf spark.executor.resource.gpu.amount=1` [^3]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值