【问题标题】:Closing stream at the end in Apache Spark results in java.io.IOException: Stream closed在 Apache Spark 中最后关闭流会导致 java.io.IOException: Stream closed
【发布时间】:2018-04-10 11:59:22
【问题描述】:

前段时间,我遇到了在 Apache Spark读取 zip 文件的问题。我分享了@98​​7654321@。

正如@Programmer 恰当地指出的那样,我没有关闭打开的流。我尝试使用takeWhile (inspiration) 中的部分函数来实现它

Stream.continually(zis.getNextEntry)
  .takeWhile {
    case null => zis.close(); false
    case _ => true
  }
  .flatMap { _ =>
    val br = new BufferedReader(new InputStreamReader(zis))
    Stream.continually(br.readLine())
      .takeWhile{
        case null => br.close(); false
        case _ => true
      }
  } 

但它不起作用!

在读取 zip 文件时,我现在收到此错误:

Job aborted due to stage failure: Task 0 in stage 5.0 failed 1 times, most recent failure: Lost task 0.0 in stage 5.0 (TID 20, localhost): java.io.IOException: Stream closed
    at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:67)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:116)

只需让它保持打开状态 - 它就可以正常工作。

看来我正在关闭流,然后尝试再次读取它。但我不知道为什么以及如何解决它。

【问题讨论】:

  • 是不是因为当你关闭br流时,你一直在关闭zis

标签: scala apache-spark stream inputstream apache-spark-2.0


【解决方案1】:

根据@alexandre-dupriez 的评论,我只关闭了外部Stream,这有助于...

Stream.continually(zis.getNextEntry)
  .takeWhile {
    case null => zis.close(); false
    case _ => true
  }
  .flatMap { _ =>
    val br = new BufferedReader(new InputStreamReader(zis))
    Stream.continually(br.readLine()).takeWhile(_ != null)
  }

因此,我需要一段时间来确认它是否正常工作。

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多