import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class HdfsClient {
@Test
public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
//获取文件系统
Configuration conf = new Configuration();
//配置集群
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop1:9000"),conf,"master");
//创建目录
fs.mkdirs(new Path("/master/demo2"));
//上传文件
fs.copyFromLocalFile(new Path("/home/master/Documents/app/data_format1/test_format1.csv"),new Path("/master/demo2/demo3.csv"));
//下载文件
fs.copyToLocalFile(false,new Path("/master/demo2/demo3.csv"),new Path("/home/master/Documents/app/test.csv"),true);
//删除文件
fs.delete(new Path("/master/demo2"),true);
///文件重命名
fs.rename(new Path("/master/demo/demo.txt"),new Path("/master/demo/test.txt"));
//关闭资源
fs.close();
}
}
大数据开发之Hdfs文件基本操作API
最新推荐文章于 2024-09-07 21:34:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)