日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows下使用Java API操作HDFS的常用方法

發布時間:2025/3/19 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。