使用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();