繁体   English   中英

使用 spark-submit 时在哪里执行 python 脚本?

[英]Where is executed python script when use spark-submit?

Python : 3.7.3
OS: CentOS 7
Spark: 2.2.0
In Cloudera
YARN : 2.6.0-cdh5.10.2

嗨,我尝试使用python脚本和pyspark执行Apache Spark ,但我不明白它是如何工作的工作流程。 我尝试在执行spark-submit时使用--archives参数在客户端模式下使用纱线发送整个conda 环境 但问题是,主要的 python 脚本在哪里运行,因为我需要指定我的共享 conda 环境的位置以在没有错误的情况下执行,因为在我尝试执行spark-submit的主机中我没有安装依赖项,并且我不想安装它。

I use this feature to pack the enviroment https://conda.github.io/conda-pack/spark.html , and I need to import the dependencies outside of a map (because inside a map, the yarn shipped the dependencies and the执行器很好地导入了这个依赖项)。

有没有一种方法可以在不打开并在主机上使用的情况下使用附带的环境执行主要的 python 脚本?

我的环境是:

PYSPARK_DRIVER_PYTHON=./enviroment/bin/python
PYSPARK_PYTHON=./enviroment/bin/python

其中 environment 是 yarn 附带的依赖项的符号链接

--archives ~/dependencies.tar.gz#enviroment

并配置执行者

--conf spark.yarn.appMasterEnv.PYSPARK_DRIVER_PYTHON=./environment/bin/python

所以最后的命令是

PYSPARK_DRIVER_PYTHON=./enviroment/bin/python \
PYSPARK_PYTHON=./environment/bin/python \
spark-submit \
--conf spark.yarn.appMasterEnv.PYSPARK_PYTHON=./environment/bin/python \
--master yarn --deploy-mode client \
--archives enviroment/dependencies.tar.gz#enviroment \
cluster-import-check.py

我的代码是

# coding=utf8
from pyspark import SparkConf
from pyspark import SparkContext

import sys
import numpy

def check_import(version, info=None):
    print("=====VERSION : ", version)
    if info and type(info) == list and len(info) != 0:
        for i in info:
            print("=====INFO EXTRA : ", i)

def python_import(x):
    import sys
    print("\n===PYTHON")
    check_import(sys.version, [sys.executable])

def numpy_import(x):
    import numpy
    print("\n===NUMPY")
    check_import(numpy.__version__, [numpy.__file__])

def printInfo(object):
    print("=====NAME : ", object.__name__)
    if object.__name__ == 'sys':
        print("=====VERSION", object.version)
        print("=====LOCATED IN", object.executable)
    else:
        print("=====VERSION : ", object.__version__)
        print("=====LOCATED IN : ", object.__file__)

    if object.__name__ == 'elasticsearch':
        es = elasticsearch.Elasticsearch(['172.22.248.206:9201'])
        print("=====MORE INFO : ", es.info())

def init_spark():
    conf = SparkConf()
    conf.setAppName("imports-checking")

    sc = SparkContext(conf=conf).getOrCreate()

    return conf, sc


def main():
    conf, sc = init_spark()
    print(sc.getConf().getAll())

    print(sc.parallelize([0]).map(lambda x: python_import(x)).collect())

    sc.stop()

if __name__ == '__main__':
    printInfo(sys)
    printInfo(numpy)
    main()

一个错误是no module named numpy或位于的python是其他的,因为集群中有另一个版本的python,但我想在集群上使用yarn自带的整个环境。

我最近了解了 PysPark 与 Yarn 的工作流程,答案是如果你想在客户端模式下运行你需要安装(在你执行 spark-submit 的主机上)在 function 映射之外导入的所有库。 另一方面,如果你想在集群模式下运行,你只需要在 spark-submit 命令中使用 --archives 选项来发送库。

  1. 当地的

当它被执行时,它必须在本地完成,必须在 spark-submit 的执行中配置 PYSPARK_DRIVER_PYTHON

PYSPARK_DRIVER_PYTHON=./dependencies/bin/python spark-submit --master local --py-files cognition.zip MainScript.py
  1. 纱线客户端

执行在执行 spark-submit 命令的主机上进行。 必须添加环境变量

PYSPARK_DRIVER_PYTHON=./dependencies/bin/python spark-submit --conf spark.yarn.appMasterEnv.PYSPARK_PYTHON=./dependencies/bin/python --master yarn --deploy-mode client --archives dependencies.tar.gz#dependencies MainScript.py
  1. 纱线簇

执行在它创建的 yarn 容器内部进行,在集群的任意节点执行

spark-submit --conf spark.yarn.appMasterEnv.PYSPARK_PYTHON=./dependencies/bin/python --master yarn --deploy-mode cluster --archives dependencies.tar.gz#dependencies MainScript.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM