访问文件作为 PySpark 驱动程序的参数 - Google Dataproc 作业

Accessing file as an argument to PySpark driver - Google Dataproc Jobs

作为 gcloud dataprocPySpark 作业的一部分,我们有多个文件,其中一个是 json,它被传递给驱动程序 python 文件。驱动程序文件本身位于 google 存储(gs 文件系统)中。

我们正尝试使用 python 的 gcloud dataproc api 提交此职位。

submit_job中作业对象使用的配置是:

job_details = {
        'placement': {
            'cluster_name': cluster_name
        },
        'pyspark_job': {
            'main_python_file_uri': 'gs://driver_file.py',
            'python_file_uris':['gs://package.whl'],
            'file_uris':['gs://config.json'],
            'args':['gs://config.json']
        }
    }

我的理解是 config.json 应该提供给驱动程序,gcloud 从日志中正确地做到了这一点 - Downloading gs://config.json to /tmp/tmprandomnumber/fop_gcp_1.json

gcloud documentation pagefile_uris 来看,这似乎是正确的-

HCFS URIs of files to be copied to the working directory of Python drivers and distributed tasks. Useful for naively parallel tasks.

现在经过大量的调试我们偶然发现了SparkFiles.get('config.json'),它的目的是根据这个

获取上传到驱动程序的文件

但这也失败了 [Errno 2] No such file or directory: '/hadoop/spark/tmp/spark-random-number/userFiles-random-number/config.json'

好的,想通了,把它贴出来,这样它可以帮助那里的人!

使用 SparkContext.addFile -

Add a file to be downloaded with this Spark job on every node.

SparkContext.addFile(config_file_name)

然后是一个简单的

from pyspark import SparkFiles
SparkFiles.get(config_file_name)

注意:所有这些都只能在您的代码中初始化 SparkContext 后才能完成。