实现 HDFS 的文件操作
时间: 2024-06-14 08:04:42 浏览: 139
以下是使用Hadoop API实现HDFS文件操作的示例代码:
1. 上传文件到HDFS:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsFileUpload {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path localPath = new Path("/path/to/local/file.txt");
Path hdfsPath = new Path("/path/to/hdfs/file.txt");
fs.copyFromLocalFile(localPath, hdfsPath);
fs.close();
}
}
```
2. 从HDFS下载文件:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsFileDownload {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path hdfsPath = new Path("/path/to/hdfs/file.txt");
Path localPath = new Path("/path/to/local/file.txt");
fs.copyToLocalFile(hdfsPath, localPath);
fs.close();
}
}
```
3. 删除HDFS上的文件:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsFileDelete {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path hdfsPath = new Path("/path/to/hdfs/file.txt");
fs.delete(hdfsPath, true);
fs.close();
}
}
```
阅读全文