081-BigData-09HDFS上傳與下載

上一篇:080-BigData-08HDFS

一、HDFS獲取文件系統(tǒng)

image.png
image.png
/**
     * 打印本地hadoop地址值
     * IO的方式寫代碼
     */
    @Test
    public void intiHDFS() throws IOException {
        //F2 可以快速的定位錯誤
        // alt + enter自動找錯誤
        //1.創(chuàng)建配信信息對象 ctrl + alt + v  后推前  ctrl + shitl + enter 補全
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(conf);

        //3.打印文件系統(tǒng)
        System.out.println(fs.toString());
    }
image.png

跑一下:

image.png

二、HDFS文件上傳

/**
     * 上傳代碼
     * 注意:如果上傳的內(nèi)容大于128MB,則是2塊
     */
    @Test
    public void putFileToHDFS() throws Exception {
        //注:import org.apache.hadoop.conf.Configuration;
        //ctrl + alt + v 推動出對象
        //1.創(chuàng)建配置信息對象
        Configuration conf = new Configuration();

        //2.設(shè)置部分參數(shù)
        conf.set("dfs.replication","2");

        //3.找到HDFS的地址
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //4.上傳本地Windows文件的路徑
        Path src = new Path("F:\\hello2.txt");

        //5.要上傳到HDFS的路徑
        Path dst = new Path("hdfs://bigdata131:9000/");

        //6.以拷貝的方式上傳,從src -> dst
        fs.copyFromLocalFile(src,dst);

        //7.關(guān)閉
        fs.close();

        System.out.println("上傳成功");
}

需要的包為

import org.apache.hadoop.fs.Path;
import java.net.URI;
image.png

查看一下,上傳成功了:

image.png

三、HDFS文件下載

/**
     * hadoop fs -get /HDFS文件系統(tǒng)
     * @throws Exception
     */
    @Test
    public void getFileFromHDFS() throws Exception {
        //1.創(chuàng)建配置信息對象  Configuration:配置
        Configuration conf = new Configuration();

        //2.找到文件系統(tǒng)
        //final URI uri     :HDFS地址
        //final Configuration conf:配置信息
        // String user :Linux用戶名
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.下載文件
        //boolean delSrc:是否將原文件刪除
        //Path src :要下載的路徑
        //Path dst :要下載到哪
        //boolean useRawLocalFileSystem :是否校驗文件
       fs.copyToLocalFile(false,new Path("hdfs://bigdata131:9000/hello1.txt"),new Path("E:\\hello3.txt"),true);

        //4.關(guān)閉fs
        //alt + enter 找錯誤
        //ctrl + alt + o  可以快速的去除沒有用的導包
        fs.close();
        System.out.println("下載成功");
    }
image.png

可以在本地查看了:

image.png

注意點:

image.png

四、HDFS目錄創(chuàng)建

/**
     * hadoop fs -mkdir /xinshou
     */
    @Test
    public void mkmdirHDFS() throws Exception {
        //1.創(chuàng)新配置信息對象
        Configuration configuration = new Configuration();

        //2.鏈接文件系統(tǒng)
        //final URI uri  地址
        //final Configuration conf  配置
        //String user   Linux用戶
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), configuration, "root");

        //3.創(chuàng)建目錄
        fs.mkdirs(new Path("hdfs://bigdata131:9000/AncientMing"));

        //4.關(guān)閉
        fs.close();
        System.out.println("創(chuàng)建文件夾成功");
    }
image.png

查看一下:

image.png

五、HDFS文件夾刪除

 /**
     * hadoop fs -rm -r /文件
     */
    @Test
    public void deleteHDFS() throws Exception {
        //1.創(chuàng)建配置對象
        Configuration conf = new Configuration();

        //2.鏈接文件系統(tǒng)
        //final URI uri, final Configuration conf, String user
        //final URI uri  地址
        //final Configuration conf  配置
        //String user   Linux用戶
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.刪除文件
        //Path var1   : HDFS地址
        //boolean var2 : 是否遞歸刪除
        fs.delete(new Path("hdfs://bigdata131:9000/a"),false);

        //4.關(guān)閉
        fs.close();
        System.out.println("刪除成功啦");
    }
image.png

查看一下,果然沒了:

image.png

六、HDFS文件名更改

    @Test
    public void renameAtHDFS() throws Exception{
        // 1 創(chuàng)建配置信息對象
        Configuration configuration = new Configuration();
        
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"),configuration, "root");
        
        //2 重命名文件或文件夾
        fs.rename(new Path("hdfs://bigdata131:9000/AncientMing"), new Path("hdfs://bigdata131:9000/AncientMing3"));
        fs.rename(new Path("hdfs://bigdata131:9000/hello2.txt"), new Path("hdfs://bigdata131:9000/hello3.txt"));
        fs.close();

        System.out.println("重命名一下~");
    }
image.png

查看一下改了沒:

image.png

七、HDFS文件詳情查看

查看文件名稱、權(quán)限、長度、塊信息

 /**
     * 查看【文件】名稱、權(quán)限等
     */
    @Test
    public void readListFiles() throws Exception {
        //1.創(chuàng)建配置對象
        Configuration conf = new Configuration();

        //2.鏈接文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.迭代器
//List the statuses and block locations of the files in the given path. If the path is a directory, if recursive is false, returns files in the directory; if recursive is true, return files in the subtree rooted at the path. If the path is a file, return the file's status and block locations.

        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        //4.遍歷迭代器
        while (listFiles.hasNext()){
            //一個一個出
            LocatedFileStatus fileStatus = listFiles.next();

            //名字
            System.out.println("文件名:" + fileStatus.getPath().getName());
            //塊大小
            System.out.println("大?。? + fileStatus.getBlockSize());
            //權(quán)限
            System.out.println("權(quán)限:" + fileStatus.getPermission());
            System.out.println(fileStatus.getLen());


            BlockLocation[] locations = fileStatus.getBlockLocations();

            for (BlockLocation bl:locations){
                System.out.println("block-offset:" + bl.getOffset());
                String[] hosts = bl.getHosts();
                for (String host:hosts){
                    System.out.println(host);
                }
            }
        }
            System.out.println("------------------我就看看----------------");
        }

需要包:

import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.*;

image.png

八、HDFS文件和文件夾判斷

 /**
     * 判斷是否是個文件還是目錄,然后打印
     * @throws Exception
     */
    @Test
    public void judge() throws Exception {
        //1.創(chuàng)建配置文件信息
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.遍歷所有的文件
//List the statuses of the files/directories in the given path if the path is a directory.

        FileStatus[] liststatus = fs.listStatus(new Path("/"));
        for(FileStatus status :liststatus)
        {
            //判斷是否是文件
            if (status.isFile()){
                //ctrl + d:復(fù)制一行
                //ctrl + x 是剪切一行,可以用來當作是刪除一行
                System.out.println("文件:" + status.getPath().getName());
            } else {
                System.out.println("目錄:" + status.getPath().getName());
            }
        }
    }
image.png

九、HDFS文件上傳

  • 通過IO流操作HDFS
/**
     * IO流方式上傳
     *
     * @throws URISyntaxException
     * @throws FileNotFoundException
     * @throws InterruptedException
     */
    @Test
    public void putFileToHDFSIO() throws URISyntaxException, IOException, InterruptedException {
        //1.創(chuàng)建配置文件信息
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.創(chuàng)建輸入流
        FileInputStream fis = new FileInputStream(new File("F:\\hello2.txt"));

        //4.輸出路徑
        //注意:不能/Andy  記得后邊寫個名 比如:/Andy/Sogou.txt
        Path writePath = new Path("hdfs://bigdata131:9000/hello5.txt");
        FSDataOutputStream fos = fs.create(writePath);

        //5.流對接
        //InputStream in    輸入
        //OutputStream out  輸出
        //int buffSize      緩沖區(qū)
        //boolean close     是否關(guān)閉流
        try {
            IOUtils.copyBytes(fis,fos,4 * 1024,false);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeStream(fos);
            IOUtils.closeStream(fis);
            System.out.println("上傳成功啦");
        }
    }

十、HDFS文件下載

  • 通過IO流操作HDFS
/**
     * IO讀取HDFS到控制臺
     *
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void getFileToHDFSIO() throws URISyntaxException, IOException, InterruptedException {
        //1.創(chuàng)建配置文件信息
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.讀取路徑
        Path readPath = new Path("hdfs://bigdata131:9000/hello5.txt");

        //4.輸入
        FSDataInputStream fis = fs.open(readPath);

        //5.輸出到控制臺
        //InputStream in    輸入
        //OutputStream out  輸出
        //int buffSize      緩沖區(qū)
        //boolean close     是否關(guān)閉流
        IOUtils.copyBytes(fis,System.out,4 * 1024 ,true);
    }

十一、定位文件讀取

  • 通過IO流操作HDFS

1、下載第一塊

/**
     * IO讀取第一塊的內(nèi)容
     *
     * @throws Exception
     */
    @Test
    public void  readFlieSeek1() throws Exception {
        //1.創(chuàng)建配置文件信息
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.輸入
        Path path = new Path("hdfs://bigdata131:9000/hello5.txt");
        FSDataInputStream fis = fs.open(path);

        //4.輸出
        FileOutputStream fos = new FileOutputStream("E:\\hello5.txt");

        //5.流對接
        byte[] buf = new byte[1024];
        for (int i = 0; i < 128 * 1024; i++) {
            fis.read(buf);
            fos.write(buf);
        }

        //6.關(guān)閉流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
    }

2、下載第二塊

/**
     * IO讀取第二塊的內(nèi)容
     *
     * @throws Exception
     */
    @Test
    public void readFlieSeek2() throws Exception {
        //1.創(chuàng)建配置文件信息
        Configuration conf = new Configuration();

        //2.獲取文件系統(tǒng)
        FileSystem fs = FileSystem.get(new URI("hdfs://bigdata131:9000"), conf, "root");

        //3.輸入
        Path path = new Path("hdfs://bigdata131:9000/hello5.txt");
        FSDataInputStream fis = fs.open(path);

        //4.輸出
        FileOutputStream fos = new FileOutputStream("E:\\hello5.txt");

        //5.定位偏移量/offset/游標/讀取進度 (目的:找到第一塊的尾巴,第二塊的開頭)
        fis.seek(128 * 1024 * 1024);

        //6.流對接
        IOUtils.copyBytes(fis, fos, 1024);

        //7.關(guān)閉流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
    }

3、合并文件

在window命令窗口中執(zhí)行
type A2 >> A1 然后更改后綴為rar即可

注意:需要塊夠大,才好測試。

下一篇:082-BigData-10HDFS上傳與下載機制

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HDFS入門 hadoop架構(gòu) Hadoop 1.0中的資源管理方案 Hadoop 1.0指的是版本為Apache...
    依天立業(yè)閱讀 1,257評論 0 1
  • 通過API操作HDFS 今天的主要內(nèi)容 HDFS獲取文件系統(tǒng) HDFS文件上傳 HDFS文件下載 HDFS目錄創(chuàng)建...
    須臾之北閱讀 2,818評論 0 3
  • HDFS的設(shè)計目標 通過上一篇文章的介紹我們已經(jīng)了解到HDFS到底是怎樣的東西,以及它是怎樣通過多副本機制來提供高...
    陌上疏影涼閱讀 1,529評論 0 3
  • 翻譯: http://hadoop.apache.org/docs/stable/hadoop-project-d...
    金剛_30bf閱讀 544評論 0 0
  • 時間空間
    kimi_zhao閱讀 385評論 0 0

友情鏈接更多精彩內(nèi)容