如何从PySpark中的JavaSparkContext获取SparkContext?

时间:2015-01-22 02:35:15

标签: apache-spark py4j pyspark

当我运行PySpark时,执行

sc._gateway.help(sc._jsc)

成功地给了我一些不错的输出,比如

JavaSparkContext extends org.apache.spark.api.java.JavaSparkContextVarargsWorkaround implements java.io.Closeable {
|  
|  Methods defined here:
|  
|  accumulable(Object, String, AccumulableParam) : Accumulable
|  
|  accumulable(Object, AccumulableParam) : Accumulable
|  
|  accumulator(double, String) : Accumulator
|  
|  accumulator(Object, AccumulatorParam) : Accumulator
|  
|  accumulator(Object, String, AccumulatorParam) : Accumulator
|  
|  accumulator(double) : Accumulator
...
跑步时

sc._gateway.help(sc._jsc.sc())

给我一​​个带有Java NPE的Py4J错误

Py4JError: An error occurred while calling None.None. Trace:
java.lang.NullPointerException
at py4j.model.Py4JMember.compareTo(Py4JMember.java:54)
at py4j.model.Py4JMember.compareTo(Py4JMember.java:39)
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at py4j.model.Py4JClass.buildClass(Py4JClass.java:88)
at py4j.commands.HelpPageCommand.getHelpObject(HelpPageCommand.java:118)
at py4j.commands.HelpPageCommand.execute(HelpPageCommand.java:74)
at py4j.GatewayConnection.run(GatewayConnection.java:207)
at java.lang.Thread.run(Thread.java:744)

通过Py4J,为什么我无法访问SparkContext中包含的JavaSparkContext

1 个答案:

答案 0 :(得分:6)

sc._jsc.sc()是访问底层SparkContext的正确方法。举例说明:

>>> sc._jsc.sc()
JavaObject id=o27
>>> sc._jsc.sc().version()
u'1.1.0'
>>> sc._jsc.sc().defaultMinSplits()
2

你在这里看到的问题是Py4J的help命令无法显示这个类的帮助(可能是Py4J错误)。