hdfs 操作 入门api
2021-06-27 16:05
标签:配置 api file 执行 als void div exce nbsp 获取分布式文件系统 URI 对象是指向hadoop集群中的namenode 节点, 端口也是配置的 , user = “”ljs“” 用户 上传文件到hdfs文件系统 从hdfs文件系统下载文件 给hdfs文件系统创建目录s 删除某文件 给某个文件或目录改名字 hdfs 操作 入门api 标签:配置 api file 执行 als void div exce nbsp 原文地址:https://www.cnblogs.com/lijins/p/10067749.html // 获取文件系统
@Test
public void getFileSystem() throws Exception{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"),
configuration, "ljs");
System.out.println(fs);
fs.close();
}
//上传文件系统
//@Test
public void putFileTohdfs() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"),
conf, "ljs");
fs.copyFromLocalFile(true, new Path("d:/text"), new Path("/user/ljs/"));
fs.close();
}
public void getFileHDFS() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
fs.copyToLocalFile(false,new Path("/user/ljs/w.txt"), new Path("d:/Demo/w.txt"),true);
}
@Test
public void mkdirAtHDFs() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
fs.mkdirs(new Path("user/ljs/output"));
fs.close();
}
@Test
public void deleteAtHDFS() throws Exception{
//获取文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
//执行删除
fs.delete(new Path("/user/ljs/text"),true);
fs.close();
}
@Test
public void renameAtHDFS() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
fs.rename(new Path("/user/ljs/user"), new Path("/user/ljs/SB"));
fs.close();
}