将本地 IPython 笔记本连接到气隙集群上的 Spark

Connect local IPython notebook to Spark on air gapped cluster

我有一台用于日常工作的 W7 机器。我的公司在私有云上也有一个气隙 Hadoop 集群。我只能通过腻子访问云。 当我想在集群上使用 Spark 时,我会启动 putty,然后执行以下两个操作之一:

有没有办法使用我的本地 W7 IPython 笔记本连接到 Spark?


在 Daniel Darabos 发表评论后经过一些尝试和错误后进行编辑

我在 this tutorial 之后在我的 W7 机器上本地安装了 Spark。 然后我创建了一个新的 pyspark 配置文件并更改了 this tutorial 之后的启动文件。 此时我可以在本地启动 Ipython 并成功创建一个 spark 上下文。 当我 运行:

sc.stop()
conf = SparkConf().setAppName('SPark Test').setMaster('localhost:7077')
sc = SparkContext(conf=conf)

然后我得到一个错误:

---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<ipython-input-15-1e8f5b112924> in <module>()
      1 sc.stop()
      2 conf = SparkConf().setAppName('SPark Test').setMaster('localhost:7077')
----> 3 sc = SparkContext(conf=conf)

C:\Spark\python\pyspark\context.pyc in __init__(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, gateway, jsc, profiler_cls)
    111         try:
    112             self._do_init(master, appName, sparkHome, pyFiles, environment, batchSize, serializer,
--> 113                           conf, jsc, profiler_cls)
    114         except:
    115             # If an error occurs, clean up in order to allow future SparkContext creation:

C:\Spark\python\pyspark\context.pyc in _do_init(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, jsc, profiler_cls)
    168 
    169         # Create the Java SparkContext through Py4J
--> 170         self._jsc = jsc or self._initialize_context(self._conf._jconf)
    171 
    172         # Create a single Accumulator in Java that we'll send all our updates through;

C:\Spark\python\pyspark\context.pyc in _initialize_context(self, jconf)
    222         Initialize SparkContext in function to allow subclass specific initialization
    223         """
--> 224         return self._jvm.JavaSparkContext(jconf)
    225 
    226     @classmethod

C:\Spark\python\lib\py4j-0.8.2.1-src.zip\py4j\java_gateway.py in __call__(self, *args)
    699         answer = self._gateway_client.send_command(command)
    700         return_value = get_return_value(answer, self._gateway_client, None,
--> 701                 self._fqn)
    702 
    703         for temp_arg in temp_args:

C:\Spark\python\pyspark\sql\utils.pyc in deco(*a, **kw)
     34     def deco(*a, **kw):
     35         try:
---> 36             return f(*a, **kw)
     37         except py4j.protocol.Py4JJavaError as e:
     38             s = e.java_exception.toString()

C:\Spark\python\lib\py4j-0.8.2.1-src.zip\py4j\protocol.py in get_return_value(answer, gateway_client, target_id, name)
    298                 raise Py4JJavaError(
    299                     'An error occurred while calling {0}{1}{2}.\n'.
--> 300                     format(target_id, '.', name), value)
    301             else:
    302                 raise Py4JError(

Py4JJavaError: An error occurred while calling None.org.apache.spark.api.java.JavaSparkContext.
: org.apache.spark.SparkException: Could not parse Master URL: 'localhost:7077'
    at org.apache.spark.SparkContext$.org$apache$spark$SparkContext$$createTaskScheduler(SparkContext.scala:2693)
    at org.apache.spark.SparkContext.<init>(SparkContext.scala:506)
    at org.apache.spark.api.java.JavaSparkContext.<init>(JavaSparkContext.scala:61)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:234)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:379)
    at py4j.Gateway.invoke(Gateway.java:214)
    at py4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:79)
    at py4j.commands.ConstructorCommand.execute(ConstructorCommand.java:68)
    at py4j.GatewayConnection.run(GatewayConnection.java:207)
    at java.lang.Thread.run(Unknown Source)

使用 PuTTY 创建 SSH 隧道,将本地端口(例如 7077)转发到 Spark 主机(例如 spark-master:7077)。然后在你本地的 IPython Notebook 中使用本地端口 (spark://localhost:7077) 作为 Spark master 的地址。