11.12

hdfs基本操作1

1|1创建目录

//创建目录 public static void mkdir(String filePath) throws URISyntaxException, IOException, InterruptedException{ FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path path= new Path(filePath); fs.mkdirs(path); System.out.println("目录创建成功:"+filePath); fs.close(); }
 

1|2创建文件

 

 
//创建文件 public static void createFile(String remoteFilePath)throws URISyntaxException, IOException, InterruptedException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path remotePath = new Path(remoteFilePath); FSDataOutputStream outputStream = fs.create(remotePath); outputStream.close(); System.out.println("文件创建成功!!"+remoteFilePath); }
 

1|3删除文件


 
//删除文件 public static void deleteFile(String filePath) throws URISyntaxException, IOException, InterruptedException{ FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path path= new Path(filePath); if(fs.deleteOnExit(path)) { System.out.println("文件删除成功:"+filePath); } else { System.out.println("文件删除失败:"+filePath); } fs.close(); }
 

1|4移动文件到本地


 
// 移动文件到本地 public static void moveToLocalFile(String remoteFilePath, String localFilePath) throws IOException, InterruptedException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path remotePath = new Path(remoteFilePath); Path localPath = new Path(localFilePath); fs.moveToLocalFile(remotePath, localPath); }
posted @ 2021-11-12 13:58  韩佳龙  阅读(74)  评论(0编辑  收藏  举报