Spark - 为我的 Spark 作业分配了多少执行器和内核

Spark - How many Executors and Cores are allocated to my spark job

Spark 架构完全围绕执行器和内核的概念展开。我想看看集群中我的 spark 应用程序 运行 实际上有多少执行程序和内核 运行。

我试图在我的应用程序中使用下面的代码片段,但没有成功。

val conf = new SparkConf().setAppName("ExecutorTestJob")
val sc = new SparkContext(conf)
conf.get("spark.executor.instances")
conf.get("spark.executor.cores")

有没有办法使用 SparkContext 对象或 SparkConf 对象等获取这些值。

Scala(编程方式):

getExecutorStorageStatusgetExecutorMemoryStatus 都是 return 执行者的数量,包括驱动程序。 就像下面的示例片段一样。

/** Method that just returns the current active/registered executors
        * excluding the driver.
        * @param sc The spark context to retrieve registered executors.
        * @return a list of executors each in the form of host:port.
        */
       def currentActiveExecutors(sc: SparkContext): Seq[String] = {
         val allExecutors = sc.getExecutorMemoryStatus.map(_._1)
         val driverHost: String = sc.getConf.get("spark.driver.host")
         allExecutors.filter(! _.split(":")(0).equals(driverHost)).toList
       }

sc.getConf.getInt("spark.executor.instances", 1)

类似地获取所有属性并像下面这样打印你也可以获得核心信息..

sc.getConf.getAll.mkString("\n")

sc.getConf.toDebugString

大多数 spark.executor.cores 执行程序 spark.driver.cores 驱动程序应该有这个值。

Python :

Above methods getExecutorStorageStatus and getExecutorMemoryStatus, In python api were not implemented

编辑 但是可以使用从 SparkSession 公开的 Py4J 绑定进行访问。

sc._jsc.sc().getExecutorMemoryStatus()

这是python获取核心数量的示例(包括主核心) def workername(): import socket return str(socket.gethostname()) anrdd=sc.parallelize(['','']) namesRDD = anrdd.flatMap(lambda e: (1,workername())) namesRDD.count()

这是一个老问题,但这是我在 Spark 2.3.0 上解决这个问题的代码:

+ 414     executor_count = len(spark.sparkContext._jsc.sc().statusTracker().getExecutorInfos()) - 1
+ 415     cores_per_executor = int(spark.sparkContext.getConf().get('spark.executor.cores','1'))