[Python] How to read large zip files in pyspark

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I do have n number of .zip files on s3, which I want to process and extract some data out of them. zip files contains a single json file. In Spark we can read .gz files, but I didn't find any way to read data within .zip files. Can someone please help me out how can I process large zip files over spark using python. I came across some options like newAPIHadoopFile, but didn't get any luck with them, nor found way to implement them in pyspark. Please note the zip files are >1G, some are of 20G as well.

    Below is code, which I used:

    import zipfile
    import io
    file_name = "s3 file path for zip file"

    def zip_extract(x):
    in_memory_data = io.BytesIO(x[1])
    file_obj = zipfile.ZipFile(in_memory_data, "r")
    files = [i for i in file_obj.namelist()]
    return dict(zip(files, [file_obj.open(file).read() for file in files]))


    zips = sc.binaryFiles(file_name)
    files_data = zips.map(zip_extract)


    But it's failing because of below reason. The instance which I'm using is r42x.large.

    Exit code: 52
    Stack trace: ExitCodeException exitCode=52:
    Py4JJavaError: An error occurred while calling z:eek:rg.apache.spark.api.python.PythonRDD.runJob.
    : org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 0.0 failed 4 times, most recent failure: Lost task 0.3 in stage 0.0

    Continue reading...

Compartilhe esta Página