使用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();
posted @ 2019-12-18 14:51  jerryzh2019  阅读(3384)  评论(0编辑  收藏  举报