在pyspark中,运行:sdf = sqlContext.sql("""SELECT * FROM t1 JOIN t2 on t1.c1 = t2.c1 """)
然后:sdf.explain(extended=True)
它打印查询执行的逻辑和物理计划。
我的问题是:如何捕获变量中的输出,而不是打印它? v = sdf.explain(extended=True)
自然,不起作用
最佳答案
如果查看 explain
(2.4或更低版本)的源代码,您会发现:
def explain(self, extended=False):
if extended:
print(self._jdf.queryExecution().toString())
else:
print(self._jdf.queryExecution().simpleString())
因此,如果要直接检索解释计划,只需在数据帧上使用_jdf.queryExecution()
方法:v = sdf._jdf.queryExecution().toString() # or .simpleString()
在3.0中,代码为:
print(
self._sc._jvm.PythonSQLUtils.explainString(self._jdf.queryExecution(), explain_mode)
)
删除打印件,您会得到explain
作为字符串。关于apache-spark - 在pyspark中捕获explain()的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54124386/