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ù)
        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.關閉
        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.關閉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.關閉
        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.關閉
        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:復制一行
                //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     是否關閉流
        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     是否關閉流
        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.關閉流
        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.關閉流
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
    }

3刊苍、合并文件

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

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末正什,一起剝皮案震驚了整個濱河市啥纸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌婴氮,老刑警劉巖斯棒,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異主经,居然都是意外死亡名船,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門旨怠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來渠驼,“玉大人,你說我怎么就攤上這事鉴腻∶陨龋” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵爽哎,是天一觀的道長蜓席。 經(jīng)常有香客問我,道長课锌,這世上最難降的妖魔是什么厨内? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任祈秕,我火速辦了婚禮,結(jié)果婚禮上雏胃,老公的妹妹穿的比我還像新娘请毛。我一直安慰自己,他們只是感情好瞭亮,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布方仿。 她就那樣靜靜地躺著,像睡著了一般统翩。 火紅的嫁衣襯著肌膚如雪仙蚜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天厂汗,我揣著相機與錄音委粉,去河邊找鬼。 笑死娶桦,一個胖子當著我的面吹牛贾节,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播趟紊,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼碰酝!你這毒婦竟也來了霎匈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤送爸,失蹤者是張志新(化名)和其女友劉穎铛嘱,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體袭厂,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡墨吓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了纹磺。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帖烘。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖橄杨,靈堂內(nèi)的尸體忽然破棺而出秘症,到底是詐尸還是另有隱情,我是刑警寧澤式矫,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布乡摹,位于F島的核電站,受9級特大地震影響采转,放射性物質(zhì)發(fā)生泄漏聪廉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望板熊。 院中可真熱鬧框全,春花似錦、人聲如沸邻邮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽筒严。三九已至丹泉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鸭蛙,已是汗流浹背摹恨。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留娶视,地道東北人晒哄。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像肪获,于是被迫代替她去往敵國和親寝凌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容

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