java如何使用IO流的方式實現hdfs數據的上傳和下載

這篇文章給大家分享的是有關java如何使用IO流的方式實現hdfs數據的上傳和下載的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

目前成都創(chuàng)新互聯已為上千余家的企業(yè)提供了網站建設、域名、網頁空間、網站托管運營、企業(yè)網站設計、白銀網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發(fā)展。

IO操作-文件上傳

1、把本地d盤下的hello.txt文件上傳到HDFS根目錄

2、代碼編寫

@Test    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {        // 1 獲取文件系統        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");        // 2 創(chuàng)建輸入流        FileInputStream fis = new FileInputStream(new File("d:/hello.txt"));        // 3 獲取輸出流        FSDataOutputStream fos = fs.create(new Path("/hello.txt"));        // 4 流對拷        IOUtils.copyBytes(fis, fos, configuration);        // 5 關閉資源        IOUtils.closeStream(fos);        IOUtils.closeStream(fis);        fs.close();    }

3、瀏覽器輸入http://hadoop100:50070/explorer.html#/查看是否上傳

IO操作-文件下載

1、從HDFS下載hello.txt文件到d盤根目錄下

2、代碼編寫

    @Test    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException {        // 1 獲取文件系統        Configuration configuration = new Configuration();        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");        // 2 獲取輸入流        FSDataInputStream fis = fs.open(new Path("/hello.txt"));        // 3 獲取輸出流        FileOutputStream fos = new FileOutputStream(new File("d:/hello.txt"));        // 4 流的對拷        IOUtils.copyBytes(fis, fos, configuration);        // 5 關閉資源        IOUtils.closeStream(fos);        IOUtils.closeStream(fis);        fs.close();    }

3、查看目錄下是否有文件

IO操作-分塊讀取

1、分塊讀取HDFS上的大文件,比如根目錄下的/hadoop-2.7.2.tar.gz

2、下載第一塊,代碼如下

@Testpublic void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
 // 1 獲取文件系統  Configuration configuration = new Configuration();  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");      // 2 獲取輸入流  FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));      // 3 創(chuàng)建輸出流  FileOutputStream fos = new FileOutputStream(new File("d:/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);fs.close();}

3、下載第二塊,代碼如下

@Testpublic void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
 // 1 獲取文件系統  Configuration configuration = new Configuration();  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop100:9000"), configuration, "root");      // 2 打開輸入流  FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz"));      // 3 定位輸入數據位置  fis.seek(1024*1024*128);      // 4 創(chuàng)建輸出流  FileOutputStream fos = new FileOutputStream(new File("d:/hadoop-2.7.2.tar.gz.part2"));      // 5 流的對拷  IOUtils.copyBytes(fis, fos, configuration);      // 6 關閉資源  IOUtils.closeStream(fis);  IOUtils.closeStream(fos);}

4、合并文件,把兩塊文件合并為一個

在Window命令窗口中進入到目錄E:\,然后執(zhí)行如下命令,對數據進行合并

type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1,合并完成后,將hadoop-2.7.2.tar.gz.part1重新命名為hadoop-2.7.2.tar.gz。解壓發(fā)現該tar包非常完整。

感謝各位的閱讀!關于“java如何使用IO流的方式實現hdfs數據的上傳和下載”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

本文標題:java如何使用IO流的方式實現hdfs數據的上傳和下載
網站網址:http://bm7419.com/article6/phopog.html

成都網站建設公司_創(chuàng)新互聯,為您提供用戶體驗、ChatGPTGoogle、軟件開發(fā)微信公眾號、定制網站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

h5響應式網站建設