1.查看HDFS目录下得文件是否存在
1 package Hdfs;HDFS查看文件是否存在
2
3 import java.io.IOException;
4 import java.net.URI;
5 import org.apache.hadoop.conf.Configuration;
6 import org.apache.hadoop.fs.FileSystem;
7 import org.apache.hadoop.fs.Path;
8
9 public class CheckFileExist {
10 public static void main(String[] args) {
11 String uri = "hdfs://neusoft-master:9000/user/root/test1";
12 Configuration conf = new Configuration();
13 try {
14 FileSystem fs = FileSystem.get(URI.create(uri), conf);
15 Path delPath = new Path(uri);
16 boolean isExists = fs.exists(delPath);
17 System.out.println(isExists);
18 } catch (IOException e) {
19 e.printStackTrace();
20 }
21 }
22
23 }
2.打包jar运行并显示结果
[root@neusoft-master hadoop]# hadoop jar /usr/local/software/JarCheckFileExist.jar
3.验证结果
[root@neusoft-master hadoop]# hadoop dfs -ls /user/root/