Windows下使用Java API操作HDFS的常用方法
生活随笔
收集整理的這篇文章主要介紹了
Windows下使用Java API操作HDFS的常用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
Windows下配置Hadoop的Java開發環境以及用Java API操作HDFS:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119379055
在上面將Hadoop的開發環境搭建起來之后,使用Java API 簡單輸出了文件目錄。
那么對應HDFS的常用文件的操作還有哪些。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
1、獲取HDFS文件系統
??? /*** 獲取HDFS文件系統* @return* @throws IOException*/public static FileSystem getFileSystem() throws IOException {Configuration configuration = new Configuration();configuration.set("fs.defaultFS","hdfs://192.168.148.128:9000");configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");System.setProperty("HADOOP_USER_NAME","root");FileSystem fileSystem = FileSystem.get(configuration);return fileSystem;}2、獲取HDFS中所有的dataNodes
??? /*** 獲取HDFS中所有的dataNode* @return* @throws IOException*/public static void listDataNodeInfo() throws IOException {FileSystem fs = getFileSystem();DistributedFileSystem hdfs = (DistributedFileSystem) fs;DatanodeInfo[] dataNodeStats =hdfs.getDataNodeStats();String[] names = new String[dataNodeStats.length];System.out.println("HDFS中所有的dataNode:");for(int i=0;i<names.length;i++){names[i]=dataNodeStats[i].getHostName();System.out.println(names[i]);}}?
3、創建文件夾
??? /*** 創建文件夾* @return* @throws IOException*/public static void mkdir() throws IOException {FileSystem fs = getFileSystem();Path srcPath = new Path("/newDir");boolean isok = fs.mkdirs(srcPath);if(isok){System.out.printf("創建文件夾成功");}else{System.out.printf("創建文件夾失敗");}fs.close();}?
4、刪除文件夾
??? /*** 刪除文件夾* @return* @throws IOException*/public static void deletedir() throws IOException {FileSystem fs = getFileSystem();Path srcPath = new Path("/newDir");boolean isok = fs.deleteOnExit(srcPath);if(isok){System.out.printf("刪除文件夾成功");}else{System.out.printf("刪除文件夾失敗");}fs.close();}?
5、判斷文件是否存在
??? /*** 判斷文件是否存在* @return* @throws IOException*/public static void checkFileExists() throws IOException {FileSystem fs = getFileSystem();Path srcPath = new Path("/badao.txt");boolean isexist = fs.exists(srcPath);if(isexist){System.out.printf("文件存在");}else{System.out.printf("文件不存在");}}?
6、上傳文件
??? /*** 上傳文件* @return* @throws IOException*/public static void uploadFile() throws IOException {FileSystem fs = getFileSystem();//源路徑Path srcPath = new Path("D:\\windows.txt");//目標路徑Path targetPath = new Path("/");fs.copyFromLocalFile(false,srcPath,targetPath);fs.close();}?
7、下載文件
??? /*** 下載文件* @return* @throws IOException*/public static void downloadFile() throws IOException {FileSystem fs = getFileSystem();//源路徑Path targetPath = new Path("D:\\");//目標路徑Path srcPath = new Path("/user/1.txt");fs.copyToLocalFile(srcPath,targetPath);fs.close();}?
5、重命名文件
??? /*** 重命名文件* @return* @throws IOException*/public static void renameFile() throws IOException {FileSystem fs = getFileSystem();//源路徑Path oldPath = new Path("/windows.txt");//目標路徑Path newPath = new Path("/centos.txt");boolean isok = fs.rename(oldPath,newPath);if(isok){System.out.printf("重命名成功");}else{System.out.printf("重命名失敗");}fs.close();}?
6、遍歷目錄和文件
??? /*** 遍歷目錄和文件* @return* @throws IOException*/public static void showDir(Path path) throws IOException {FileSystem fs = getFileSystem();DistributedFileSystem hdfs = (DistributedFileSystem) fs;FileStatus[] fileStatuses = hdfs.listStatus(path);if(fileStatuses.length>0){for (FileStatus status:fileStatuses) {Path f = status.getPath();System.out.println(f.toString());if(status.isDirectory()){FileStatus[] files = hdfs.listStatus(f);if(files.length>0){for (FileStatus file:files) {showDir(file.getPath());}}}}}}?
總結
以上是生活随笔為你收集整理的Windows下使用Java API操作HDFS的常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows下配置Hadoop的Jav
- 下一篇: Three.js中使用requestAn