之前初学spark用spark-shell执行小程序的时候, 每次执行action操作(比如count,collect或者println),都会报错:
WARN TaskSchedulerImpl: Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient resources
同时如果去spark ui上(公司默认为ip:18080)会看到spark-shell为核数core为0:
原因是启动spark-shell的时候没有给他分配资源, 所以我们应该在启动spark-shell的时候这么写:
/home/mr/spark/bin/spark-shell --executor-memory 4G \
--total-executor-cores 10 \
--executor-cores 1
其中 :
--executor-memory 是指定每个executor(执行器)占用的内存
--total-executor-cores是所有executor总共使用的cpu核数
--executor-cores是每个executor使用的cpu核数
对于spark-shell还可以在yarn上执行:
--master yarn-client
这里必须是client,不同于spark-submit的yarn-cluster, 因为spark-shell作为一个与用户交互的命令行,必须将Driver运行在本地,而不是yarn上, 其他的参数与submit一样.
以上参数就限制了总cpu核数为10, executor数目为10
但是, 每次执行都要写这么多参数显然很麻烦, 我们也可以通过修改spark-shell的方法将以上参数改成默认, 方法如下:
spark-shell之前代码:
... ...
function main() {
...
else
export SPARK_SUBMIT_OPTS
"$FWDIR"/bin/spark-submit --class org.apache.spark.repl.Main "$@"
fi
修改为:
... ...
function main() {
...
else
export SPARK_SUBMIT_OPTS
# CUN
RESOURCE_OPTIONS="--executor-memory 1G --total-executor-cores 10 --executor-cores 1 "
CMD_OPTIONS=$RESOURCE_OPTIONS$@
echo "CMD_OPTIONS: " $CMD_OPTIONS
"$FWDIR"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" $CMD_OPTIONS
fi
之后, 直接运行spark-shell即可
在使用Spark Shell时遇到任务未接受资源的问题,可以通过设置启动参数解决。如指定executor内存、总CPU核数和每个executor的CPU核数。例如:`--executor-memory 10g --total-executor-cores 10 --executor-cores 1`。在Yarn上运行Spark Shell时,Driver需运行在本地。可通过修改Spark Shell启动脚本,将这些参数设为默认,避免每次手动输入。
581

被折叠的 条评论
为什么被折叠?



