编写一个Java应用程序,用来检测HDFS中是否存在一个文件。
时间: 2024-03-22 16:38:47 浏览: 91
可以使用Hadoop的Java API来实现检测HDFS中是否存在一个文件的功能。下面是一个简单的实现示例:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsFileChecker {
public static void main(String[] args) throws Exception {
String hdfsUri = "hdfs://localhost:9000"; // HDFS的URI
String filePath = "/user/hadoop/test.txt"; // 要检测的文件路径
Configuration conf = new Configuration();
conf.set("fs.defaultFS", hdfsUri);
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean exists = fs.exists(path);
if (exists) {
System.out.println("文件存在。");
} else {
System.out.println("文件不存在。");
}
fs.close();
}
}
```
在上面的示例中,首先设置了HDFS的URI和要检测的文件路径。然后创建了一个Configuration对象,并设置了fs.defaultFS属性为HDFS的URI,这样就可以获取HDFS的FileSystem对象。接着创建了一个Path对象表示要检测的文件,使用FileSystem对象的exists方法判断文件是否存在,最后关闭FileSystem对象。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)