【问题标题】:How to include external Spark library while using PySpark in Jupyter notebook如何在 Jupyter 笔记本中使用 PySpark 时包含外部 Spark 库
【发布时间】:2018-12-08 20:52:10
【问题描述】:

我正在尝试在 Jupyter Notebook 中运行以下 PySpark-Kafka 流 example。这是我在笔记本中使用的代码的第一部分:

from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils

sc = pyspark.SparkContext(master='local[*]',appName="PySpark streaming")
ssc = StreamingContext(sc, 2)

topic = "my-topic"
brokers = "localhost:9092"
kvs = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers})

如果我运行单元格,我会收到以下错误/描述:

Spark Streaming's Kafka libraries not found in class path. Try one of the following.

1. Include the Kafka library and its dependencies with in the
 spark-submit command as

$ bin/spark-submit --packages org.apache.spark:spark-streaming-kafka-0-8:2.3.0 ...

2. Download the JAR of the artifact from Maven Central http://search.maven.org/,
Group Id = org.apache.spark, Artifact Id = spark-streaming-kafka-0-8-assembly, Version = 2.3.0.
Then, include the jar in the spark-submit command as

$ bin/spark-submit --jars <spark-streaming-kafka-0-8-assembly.jar> ...

我的问题是:如何将 --jars 或 --package 参数传递给 Jupyter Notebook?或者,我可以下载这个包并将其永久链接到 Python/Jupyter(可能通过 .bashrc 中的导出)?

【问题讨论】:

    标签: python apache-spark pyspark jupyter-notebook jupyter


    【解决方案1】:

    至少有两种方法,大致对应错误信息中建议的两个选项:

    第一种方法是相应地更新您各自的 Jupyter 内核(如果您尚未使用 Jupyter 内核,您应该 - 请参阅 this answer 了解在 Jupyter for Pyspark 中使用内核的详细信息) .

    更具体地说,您应该使用env 下的以下条目更新您各自的Pyspark kernel.json 配置文件(如果您使用--master local 以外的其他内容,请进行相应修改):

    "PYSPARK_SUBMIT_ARGS": "--master local --packages org.apache.spark:spark-streaming-kafka-0-8:2.3.0 pyspark-shell"
    

    第二种方法是将以下条目放入您的spark-defaults.conf 文件中:

    spark.jars.packages org.apache.spark:spark-streaming-kafka-0-8:2.3.0
    

    在这两种情况下,您都不需要手动下载任何内容 - 第一次使用更新的配置运行 Pyspark 时,将下载必要的文件并将其放入适当的目录中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2017-01-15
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多