天天看点

FileSystem closed 异常问题

在用Java来读取hdfs上的文件时遇到下面问题:

java.io.IOException: Filesystem closed

        at org.apache.hadoop.hdfs.DFSClient.checkOpen(DFSClient.java:761)

        at org.apache.hadoop.hdfs.DFSClient.open(DFSClient.java:1446)

        at org.apache.hadoop.hdfs.DistributedFileSystem$3.doCall(DistributedFileSystem.java:301)

        at org.apache.hadoop.hdfs.DistributedFileSystem$3.doCall(DistributedFileSystem.java:297)

        at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)

        at org.apache.hadoop.hdfs.DistributedFileSystem.open(DistributedFileSystem.java:297)

        at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:766)

        at hdfs.HdfsFile.readFromHdfs(HdfsFile.java:29)

        at tag.FileData.getDevice(FileData.java:74)

        at tag.FileData.run(FileData.java:66)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

        at java.lang.Thread.run(Thread.java:745)

原因:对于此问题,是因为Filesystem 是一个single 对象,当设置了JVM重用这个参数后,那么就会出现之前一个任务运行完成后,会关闭这个Filesystem,那么后面来的

任务在判断此文件对象是否打开时,就会抛出这样的错误。

解决方式两种:

一种,不要设置JVM重用。

一种,配置Filesystem不缓存,每次都会重新创建一个对象。

继续阅读