HDFS的JAVA用戶端編寫(JAVA代碼實現對HDFS的操作)__JAVA

來源:互聯網
上載者:User
源碼如下:
package com.sfd.hdfs;import java.io.FileInputStream;import java.io.IOException;import org.apache.commons.compress.utils.IOUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.LocatedFileStatus;import org.apache.hadoop.fs.Path;import org.apache.hadoop.fs.RemoteIterator;import org.junit.BeforeClass;import org.junit.Test;public class HdfsUtils {    private static FileSystem fs;    /**     * 每次測試前都將進入此方法對fs進行初始化     * @throws Exception     */    @BeforeClass    public static void init()throws Exception{        Configuration conf=new Configuration();        /**            設定設定檔(hdfs-site.xml,core-site.xml檔案)的資訊,            也可以將工作目錄下的core-site.xml,hdfs-site.xml檔案            添加到src目錄下,如果不填加程式運行時會報錯,            因為程式的預設的檔案管理系統是本地的檔案管理系統而不是            hdfs。            下面的設定相當於告訴程式,我們的配置是作用在hdfs(Distributed File System)上的,            這個設定將會覆蓋scr目錄下hdfs-site.xml中的相應屬性的配置            通過這個配置產生的fs實際上繼承了抽象類別FileSystem的DistributedFileSystem子類。         */         conf.set("fs.defaultFS", "hdfs://localhost:9000");        fs = FileSystem.get(conf);     }    /**     * 上傳檔案到hdfs上(沒有被封裝)     * @throws Exception     */    @Test    public void upload() throws Exception{        Path path=new Path("hdfs://localhost:9000/sfd/sfd3.txt");        //從檔案系統中的到輸出資料流        FSDataOutputStream os=fs.create(path);        //得到用戶端本地的輸入資料流        FileInputStream in=new FileInputStream("/home/sfd/soft/download/sfd1.txt");        //將上面得到的輸入資料流中的資料通過工具類複製到輸出資料流中,從而實現檔案從本地上傳到hdfs的過程        IOUtils.copy(in, os);    }    /**     * 使用架構實現本地檔案的上傳     * @throws Exception     */    @Test    public void upload2() throws Exception{        fs.copyFromLocalFile(new Path("/home/sfd/soft/download/sfd1.txt"), new Path("hdfs://localhost:9000/aa/bb/sfd3.txt"));    }    /**     * 使用架構下載hdfs中的檔案到本地     * @throws Exception     */    @Test    public void download()throws Exception{         fs.copyToLocalFile(new Path("hdfs://localhost:9000/aa/bb/sfd3.txt"), new Path("/home/sfd/soft/download/sfd4.txt"));    }    /**     * 查詢檔案目錄中的所有檔案(包括子檔案夾下的檔案,不包括檔案夾)     * @throws Exception     */    @Test    public void listFile()throws Exception{        //得到hdfs指定(第一個參數)目錄下的檔案狀態的迭代器,第二個參數表示是否迭代的取出該目錄下檔案夾下的子檔案        RemoteIterator< LocatedFileStatus> files=fs.listFiles(new Path("/"), false);        //遍曆迭代器得到檔案名稱        while (files.hasNext()){            //分別得到每個檔案的狀態            LocatedFileStatus file=files.next();            //從狀態中得到檔案的路徑,進而得到檔案名稱            Path path=file.getPath();            String filename=path.getName();            //輸出檔案名            System.out.println(filename);        }    }    /**     * 查詢指定目錄下的檔案和檔案夾     * @throws Exception     */    @Test    public void listFindAndDir()throws Exception{            //得到檔案的狀態            FileStatus[] listStatus = fs.listStatus(new Path("/"));            //遍曆            for (FileStatus fileStatus:listStatus){                Path path = fileStatus.getPath();                String name = path.getName();                System.out.println(name);            }    }    /**     * 在hdfs中建立檔案夾     * @throws Exception     */    @Test    public void makedir()throws Exception{            //如果bb檔案夾沒有就先建立bb檔案夾,如果aa還沒有就先建立cc檔案夾            fs.mkdirs(new Path("/aa/bb/cc"));    }    /**     * 使用架構刪除檔案或非空檔案夾     * @throws Exception     */    @Test    public void remove()throws Exception{        //刪除目錄,第二個參數便是是否遞迴刪除,如果第一個參數表示的是非空的檔案夾,就會報錯        fs.delete(new Path("/aa/bb"), true);    }    /**     * 使用架構實現檔案的重新命名和移動     * @throws Exception     */    @Test    public void reName()throws Exception{        //1.當兩個參數所指向的檔案在一個檔案夾下的時候實現的是重新命名        //2.當兩個參數所指向的檔案在不同檔案夾下的時候實現的是移動檔案        fs.rename(new Path("/sfd/sfd1.txt"),new Path("/sfd/sfd4.txt"));    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.