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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA操作Hadoop

發布時間:2025/10/17 编程问答 83 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA操作Hadoop 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面:

我的博客已遷移至自建服務器:博客傳送門,CSDN博客暫時停止,如有機器學習方面的興趣,歡迎來看一看。

此外目前我在gitHub上準備一些李航的《統計學習方法》的實現算法,目標將書內算法全部手打實現,歡迎參觀并打星。GitHib傳送門

一、客戶端環境準備

1.jar 包及Eclipse 準備

該部分可網上查閱相關資料

2.相關Demo

1.獲取文件系統

//1.獲取文件系統 @Test public void initHDFS() throws IOException {// 1獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(configuration);//2.打印文件系統到控制臺System.out.println(fs.toString());fs.close(); }

1.首先需要獲取到Hadoop的文件系統,以便對Hadoop進行操作。
2.打印獲取到的文件系統
3.控制臺有信息輸出,說明獲取成功

2.文件上傳

//2.文件上傳 @Test public void CopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//上傳fs.copyFromLocalFile(false, new Path("e:/hello.txt"), new Path("/hello1.txt"));//釋放資源fs.close();}

3.文件下載

//3.文件下載 @Test public void CopyToLoaclFile() throws IOException, InterruptedException, URISyntaxException{//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//下載文件fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:\\hello1.txt"), true);fs.close(); }

4.創建目錄

//4.創建目錄 @Test public void Mkdirs() throws IOException, InterruptedException, URISyntaxException {Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//創建目錄fs.mkdirs(new Path("/test/haha/heihei"));fs.close(); }

5.刪除文件夾

//5.刪除文件夾 @Test public void Delete() throws IOException, InterruptedException, URISyntaxException {//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//刪除文件夾fs.delete(new Path("/test"), true);//釋放資源fs.close();}

6.更改文件名

//6.更改文件名 @Test public void ReName() throws IOException, InterruptedException, URISyntaxException {//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//更改文件名fs.rename(new Path("/hello.txt"), new Path("/hello2.txt"));//釋放資源fs.close(); }

7.查看文件詳情

//7.查看文件詳情 @Test public void ListFiles() throws IOException, InterruptedException, URISyntaxException {//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);while(remoteIterator.hasNext()) {LocatedFileStatus status = remoteIterator.next();//文件名System.out.println(status.getPath().getName());//文件長度System.out.println(status.getLen());//組System.out.println(status.getGroup());//副本數System.out.println(status.getReplication());//獲取塊位置信息BlockLocation[] blockLocations = status.getBlockLocations();for(BlockLocation bl: blockLocations) {//偏移量System.out.println(bl.getOffset());//副本所在主機System.out.println(bl.getHosts());System.out.println(bl.getNames());}System.out.println("-------------------------------------------");}fs.close(); }

8.判斷是否文件

//8.判斷是否文件 @Test public void IsFile() throws IOException, InterruptedException, URISyntaxException {//獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");FileStatus[] listStatus = fs.listStatus(new Path("/"));for(FileStatus status : listStatus) {if(status.isFile()) {System.out.println("f:" + status.getPath().getName());}else {System.out.println("d:" + status.getPath().getName());}}fs.close(); } } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的JAVA操作Hadoop的全部內容,希望文章能夠幫你解決所遇到的問題。

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