apache-spark - 从本地 jupyter notebook 连接到 Spark 集群

标签 apache-spark pyspark jupyter-notebook py4j

我尝试从本地机器上的笔记本连接到远程 spark master。

当我尝试创建 sparkContext

sc = pyspark.SparkContext(master = "spark://remote-spark-master-hostname:7077", 
                          appName="jupyter notebook_test"),

我得到以下异常:
/opt/.venv/lib/python3.7/site-packages/pyspark/context.py in __init__(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, gateway, jsc, profiler_cls)
    134         try:
    135             self._do_init(master, appName, sparkHome, pyFiles, environment, batchSize, serializer,
--> 136                           conf, jsc, profiler_cls)
    137         except:
    138             # If an error occurs, clean up in order to allow future SparkContext creation:

/opt/.venv/lib/python3.7/site-packages/pyspark/context.py in _do_init(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, jsc, profiler_cls)
    196 
    197         # Create the Java SparkContext through Py4J
--> 198         self._jsc = jsc or self._initialize_context(self._conf._jconf)
    199         # Reset the SparkConf to the one actually used by the SparkContext in JVM.
    200         self._conf = SparkConf(_jconf=self._jsc.sc().conf())

/opt/.venv/lib/python3.7/site-packages/pyspark/context.py in _initialize_context(self, jconf)
    304         Initialize SparkContext in function to allow subclass specific initialization
    305         """
--> 306         return self._jvm.JavaSparkContext(jconf)
    307 
    308     @classmethod

/opt/.venv/lib/python3.7/site-packages/py4j/java_gateway.py in __call__(self, *args)
   1523         answer = self._gateway_client.send_command(command)
   1524         return_value = get_return_value(
-> 1525             answer, self._gateway_client, None, self._fqn)
   1526 
   1527         for temp_arg in temp_args:

/opt/.venv/lib/python3.7/site-packages/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
--> 328                     format(target_id, ".", name), value)
    329             else:
    330                 raise Py4JError(

Py4JJavaError: An error occurred while calling None.org.apache.spark.api.java.JavaSparkContext.
: java.lang.IllegalArgumentException: requirement failed: Can only call getServletHandlers on a running MetricsSystem
    at scala.Predef$.require(Predef.scala:224)
    at org.apache.spark.metrics.MetricsSystem.getServletHandlers(MetricsSystem.scala:91)
    at org.apache.spark.SparkContext.<init>(SparkContext.scala:516)
    at org.apache.spark.api.java.JavaSparkContext.<init>(JavaSparkContext.scala:58)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:238)
    at py4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)
    at py4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:745)


同时,我可以在交互模式下使用相同的解释器创建 Spark 上下文。

我应该怎么做才能从本地 jupyter 笔记本连接到远程 spark master?

最佳答案

我使用@HristoIliev 建议解决了我的问题。
就我而言,PYSPARK_PYTHON未在 jupyter 环境中设置。简单的解决方案:

import os
os.environ["PYSPARK_PYTHON"] = '/opt/.venv/bin/python'
os.environ["SPARK_HOME"] = '/opt/spark'

您也可以使用 findspark 为此,但我没有测试它。

关于apache-spark - 从本地 jupyter notebook 连接到 Spark 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61457363/

相关文章:

当在跟踪中设置悬停信息时,Python Plotly xaxis 悬停文本消失

java.lang.ClassNotFoundException : org. 用于 Spark 3.0.0 的 apache.spark.sql.sources.v2.DataSourceV2

apache-spark - 要创建多少个 Spark session ?

apache-spark - 什么是 DAGScheduler.messageProcessingTime?

python-3.x - ModuleNotFoundError : No module named 'py4j'

ipython - Ipython 笔记本中的 pyspark 引发 Py4JNetworkError

jupyter-notebook - 如何向 Jupyter/JupyterLab 笔记本添加目录?

html - 如何调整 jupyterlab 笔记本中的滚动输出高度?

performance - Spark 示例程序运行很慢

apache-spark - 如何在 Spark 和 Cassandra 之间配置 SSL?