如果在获取SparkContext()之前打开一个文件,Pyspark将引发Java网关异常

2024-04-25 14:38:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Jupyter笔记本电脑上使用pyspark,其中Spark 2.1.1集群运行在IP“spark://远程:端口(spark主IP) 我能够成功地创建SparkContext。

但是,我想读spark_master_-ip和spark.cores.max从.properties文件(而不是硬编码)。当我试图在myspark中读取我的自定义spark属性文件时_配置属性'文件(我成功地解析和读取了该文件),但是当我尝试创建SparkContext()时,我得到了以下Java网关异常。 这是我的代码:

import pyspark
from pprint import pprint
from pyspark import SparkConf
def getproperties():
    """Get Spark configuration properties in python dictionary"""
    global properties
    properties = dict()
    with open('myspark_config.properties') as f:
        for line in f:
                if not line.startswith('#') and not line.startswith('\n'):
                    tokens = line.split('=')
                    tokens[0] = tokens[0].strip()
                    tokens[1] = "=".join(tokens[1:])
                    properties[tokens[0]] = tokens[1].strip()

        f.close()
    pprint(properties)
    return(properties)
properties = getproperties()

conf = (SparkConf()
            .setMaster(properties["spark_master_url"])
            .setAppName("testApp")
            .set('spark.cores.max',properties["spark_app_cores"])
            .set('spark.executor.memory',properties["spark_app_memory"])
            .set('spark.dynamicAllocation.enabled','true')
            .set('spark.shuffle.service.enabled','true')

            )
# conf = (SparkConf()
#             .setMaster("spark://remote:port")
#             .setAppName("testApp")
#             .set('spark.cores.max',"2")
#             .set('spark.executor.memory',"2G")
#             .set('spark.dynamicAllocation.enabled','true')
#             .set('spark.shuffle.service.enabled','true')
#         )
sc = pyspark.SparkContext(conf=conf)

如果我不从文件中读取并在SparkConf()中硬编码sparkmaster(当前有注释),则不会出现任何异常,代码运行平稳。“JAVA_HOME”、“SPARK_HOME”“PYTHONPATH”设置成功,我没有使用Python。我在ubuntu和spark-2.1.1上使用Python2.7上的Jupyter笔记本

^{pr2}$

我浏览了各种链接,但没有找到解决方案: pyspark java gateway error stackoverflowGithub issue on java Gateway


Tags: 文件importtrueconflineenabledpropertiescores
1条回答
网友
1楼 · 发布于 2024-04-25 14:38:36

But, I want to read spark_master_ip and spark.cores.max from a .properties file (instead of hard coding it).

这是个很棒的主意,但是你忽略了一个事实,那就是$SPARK_HOME/conf/spark-defaults.conf的作用。只需将所需属性放在那里。在

but I get the following Java gateway exception,

这看起来不对:

"=".join(tokens[1:])

为什么要在属性中使用=?在

否则就没有效果了。Python还提供属性解析器https://docs.python.org/3/library/configparser.html

相关问题 更多 >