本文假设Spark部署方式为YARN。
一个Container包含一个Executor(即一个JVM进程)。
一、固定资源申请
driver-cores:设置 Driver的核数,默认1
driver-memory:设置Driver进程的内存,默认1G
num-executors:设置Spark作业总共要用多少个Executor进程来执行,默认2
executor-cores:设置每个Executor进程的核数,默认1
executor-memory:设置每个Executor进程的内存,默认1G
spark.default.parallelism:设置每个Stage的默认Task数量,建议设定为CPU总核数的2-3倍,默认为空。
spark-submit \
--driver-cores 2 \
--driver-memory 15G \
--num-executors 4 \
--executor-cores 2 \
--executor-memory 20G \
--conf spark.default.parallelism=4
--queue root.default \
--verbose \
model.py
问题:通过parallelize()函数创建的RDD,分区数是怎么确定的?
首先根据函数参数numSlices;如果参数为空,则根据spark.default.parallelism;如果再为空,则取所有Executor的总核数,即num-executors*executor-cores(如果总核数小于2,则取2)。
问题:设定–num-executors 4 --executor-cores 2 --executor-memory 3G时,请问占用了多少资源(CPU和内存)?
除了Driver默认的1核1G外,Executor占用的CPU总核数=4×2=8 vcores,总内存=4×3=12G,可以简单理解为用了4台机器,每台机器都是2核3G的配置。
pyspark --queue gld --num-executors 4 --executor-cores 25 --executor-memory 100G
java.lang.IllegalArgumentException: Required executor memory (102400+2048 MB) is above the max threshold (65535 MB) of this cluster! Please check the values of ‘yarn.scheduler.maximum-allocation-mb’ and/or ‘yarn.nodemanager.resource.memory-mb’.
二、动态资源分配
Spark可以根据任务的情况动态申请资源,空闲时可以释放资源,避免资源的浪费。
配置例子:
spark-sql \
--queue gld \
--conf spark.shuffle.service.enabled=true \
--conf spark.dynamicAllocation.enabled=true \
--conf spark.dynamicAllocation.maxExecutors=90 \
--executor-cores 4 \
--executor-memory 4G