如何获得PySpark中的工作人员(执行者)数量?

Ame*_*url 9 scala apache-spark pyspark

我需要使用这个参数,那么我怎样才能得到工人的数量?像Scala一样,我可以打电话sc.getExecutorMemoryStatus来获得可用的工人数量.但在PySpark中,似乎没有公开API获取此数字.

Ram*_*ram 18

在Scala中,getExecutorStorageStatus并且getExecutorMemoryStatus都返回执行者包括驱动程序的数量.如下面的示例代码段

/** 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
       }
Run Code Online (Sandbox Code Playgroud)

但在python api中它没有实现

@DanielDarabos的回答也证实了这一点.

但是,我不是pyspark的专家.你可以尝试一些相当于这个python的东西......

sc.getConf().get("spark.executor.instances")
Run Code Online (Sandbox Code Playgroud)

  • python语法是`sc._conf.get('spark.executor.instances')`.它返回一个字符串. (5认同)
  • 配置!=现实:p (4认同)
  • 由于这个问题已经结束并且这个答案是错误的,所以我将在这里回答: `sc = spark_session._jsc.sc()` `result1 = sc.getExecutorMemoryStatus().keys()` `result2 = [executor.host()对于 sc.statusTracker().getExecutorInfos() 中的执行程序]` (3认同)