使用java程序判断HDFS文件是否存在


 

使用java程序判断HDFS文件是否存在

public static boolean isPathExist(String server, String path) throws IOException {
    Configuration conf = new Configuration();
    conf.set("fs.defaultFS", server);
    conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");

    try(FileSystem fs = FileSystem.get(conf)) {
        if (fs.exists(new Path(path))) {
            return true;
        } else {
            return false;
        }
    }
}

 

获取当前读取文件所在的文件夹名称。只适用于FileInputFormat.setInputPaths方法添加的路径,MultipleInputs.addInputPath添加的路径不能使用这种方法,需要通过反射来使用内部方法取得

InputSplit inputSplit = context.getInputSplit();
String dirName = ((FileSplit) inputSplit).getPath().getParent().getName();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM