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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

IO流操作HDFS

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

寫在前面:

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

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

上傳文件Demo

@Test public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {//1.獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//2.獲取輸入流FileInputStream fis = new FileInputStream(new File("e:/hello.txt"));//3.獲取輸出流FSDataOutputStream fos = fs.create(new Path("/hello3.txt"));//4.流對拷IOUtils.copyBytes(fis, fos, configuration);//5.關閉資源IOUtils.closeStream(fis);IOUtils.closeStream(fos);if(fs != null) {try {fs.close();}catch (IOException ex) {System.out.println("文件關閉失敗");}} }

下載文件Demo

@Test public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException {//1.獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//2.獲取輸入流FSDataInputStream fis = fs.open(new Path("/hello3.txt"));//3.獲取輸出流FileOutputStream fos = new FileOutputStream(new File("e:/hello1.txt"));//4.流對拷IOUtils.copyBytes(fis, fos, configuration);//5.關閉資源IOUtils.closeStream(fis);IOUtils.closeStream(fos);if(fs != null) {try {fs.close();}catch(IOException ex){System.out.println(ex.getMessage());}} }

定位文件讀取

本Demo中,在/user/atguigu中有文件hadoop-2.7.2.tar.gz,其大小為188.5M,由于HDFS文件塊大小為128M,所以該文件在HDFS系統中分兩塊存。
正常下載時,HDFS會將整個文件下載至本地。本Demo通過將文件分開定位的方式,按照塊的大小來分兩次讀取文件。

//下載第一部分 @Test public void getHDFSSeek1() throws IOException, InterruptedException, URISyntaxException {//1.獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//2.獲取輸入流FSDataInputStream fis = fs.open(new Path("/user/atguigu/hadoop-2.7.2.tar.gz"));//3.獲取輸出流FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part1"));//4.文件定位對拷byte[] buf = new byte[1024];for(int i = 0; i < 1024 * 128; i++) {fis.read(buf);fos.write(buf);}//5.關閉資源IOUtils.closeStream(fis);IOUtils.closeStream(fos);IOUtils.closeStream(fs); }//下載第二部分 @Test public void getHDFSSeek2() throws IOException, InterruptedException, URISyntaxException {// 1.獲取文件系統Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");// 2.獲取輸入流FSDataInputStream fis = fs.open(new Path("/user/atguigu/hadoop-2.7.2.tar.gz"));// 3.獲取輸出流FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part2"));// 4.文件定位對拷fis.seek(1024 * 1024 * 128);IOUtils.copyBytes(fis, fos, configuration);// 5.關閉資源IOUtils.closeStream(fis);IOUtils.closeStream(fos);IOUtils.closeStream(fs); } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

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

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