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

相关文章:

  • 2022-01-02
  • 2022-12-23
  • 2021-09-29
  • 2022-01-17
  • 2021-11-18
  • 2022-02-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
相关资源
相似解决方案