使用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