我应该关闭HDFS文件系统吗?

时间:2019-04-02 00:04:55

标签: apache-spark hadoop hive apache-spark-sql hdfs

我的火花工作:

def saveCount(spark: SparkSession, cnt: Long): Unit = {
  val fs = FileSystem.get(spark.sparkContext.hadoopConfiguration)
  val path = new org.apache.hadoop.fs.Path(s"hdfs://xxx/data/count")
  if(fs.exists(path)) fs.delete(path, true)

  val out = new BufferedOutputStream(fs.create(path))
  out.write(cnt.toString.getBytes("UTF-8"))
  out.flush()
  out.close()
  // fs.close()
}

main function {
  for-loop {
    val df = spark.sql("xxx").cache()
    val cnt = df.count()
    df.write.mode(SaveMode.Overwrite).json(s"yyy")
    saveCount(spark, cnt)
  }
}

spark作业是主要功能:有一个for循环,每次从spark sql查询数据帧。数据帧将被转储,并且计数将通过saveCount()函数保存到文件中。

我的问题:我应该用fs.close()saveCount()吗? (<=我认为答案是否定的

我担心的是: 如果调用它,是否会影响数据帧的转储?我在spark作业日志中发现了许多java.io.IOException: Filesystem closed异常

谢谢

0 个答案:

没有答案