使用 pyspark 创建 SparkSession 时出现问题

Ank*_*ita 5 python apache-spark pyspark jupyter-notebook

我是 Spark 新手。我正在尝试创建 Spark 会话pyspark.sql以加载 .csv 文件。但是,每次我尝试执行第二行(如下所示)时,该命令都会继续执行几个小时并且似乎永远不会生成代码的其他行。代码如下:

from pyspark.sql import SparkSession
sp = SparkSession.builder.appName("solution").config("spark.some.config.option", "some-value").getOrCreate()
df = sp.read.csv('walmart_stock.csv', header= True, inferSchema= True)
df.columns
Run Code Online (Sandbox Code Playgroud)

另外,如果我等待很长时间后杀死内核,则会出现以下异常:

<ipython-input-23-16c3797ce83f> in <module>
----> 1 sp = SparkSession.builder.appName("solution").config("spark.some.config.option", "some-value").getOrCreate()

~\anaconda3\lib\site-packages\pyspark\sql\session.py in getOrCreate(self)
    184                             sparkConf.set(key, value)
    185                         # This SparkContext may be an existing one.
--> 186                         sc = SparkContext.getOrCreate(sparkConf)
    187                     # Do not update `SparkConf` for existing `SparkContext`, as it's shared
    188                     # by all sessions.

~\anaconda3\lib\site-packages\pyspark\context.py in getOrCreate(cls, conf)
    369         with SparkContext._lock:
    370             if SparkContext._active_spark_context is None:
--> 371                 SparkContext(conf=conf or SparkConf())
    372             return SparkContext._active_spark_context
    373 

~\anaconda3\lib\site-packages\pyspark\context.py in __init__(self, master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, gateway, jsc, profiler_cls)
    126                 " is not allowed as it is a security risk.")
    127 
--> 128         SparkContext._ensure_initialized(self, gateway=gateway, conf=conf)
    129         try:
    130             self._do_init(master, appName, sparkHome, pyFiles, environment, batchSize, serializer,

~\anaconda3\lib\site-packages\pyspark\context.py in _ensure_initialized(cls, instance, gateway, conf)
    318         with SparkContext._lock:
    319             if not SparkContext._gateway:
--> 320                 SparkContext._gateway = gateway or launch_gateway(conf)
    321                 SparkContext._jvm = SparkContext._gateway.jvm
    322 

~\anaconda3\lib\site-packages\pyspark\java_gateway.py in launch_gateway(conf, popen_kwargs)
    100             # Wait for the file to appear, or for the process to exit, whichever happens first.
    101             while not proc.poll() and not os.path.isfile(conn_info_file):
--> 102                 time.sleep(0.1)
    103 
    104             if not os.path.isfile(conn_info_file):
Run Code Online (Sandbox Code Playgroud)

你能建议一下问题是什么吗?

小智 1

我遇到了同样的问题,甚至我无法创建 Spark 上下文,因此通过一些研究,我知道我们安装的 Spark 版本应该与我们的 Pyspark 版本匹配,这很奇怪,但这是事实,我在我的笔记本电脑,令人惊讶的是它能工作。

要检查您输入的 Spark 版本(在 cmd 中):

火花壳——版本

并且,要检查 Pyspark 版本,请输入(在 cmd 中):

点显示 pyspark

之后,
使用以下代码创建 SparkContext :

conf = pyspark.SparkConf() 
sqlcontext = pyspark.SparkContext.getOrCreate(conf=conf)
sc = SQLContext(sqlcontext)
Run Code Online (Sandbox Code Playgroud)

之后运行:

df.columns
Run Code Online (Sandbox Code Playgroud)

这种方式对我有用,我希望对你也有用。