在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    HDFS_CRUD

    package com.hdfsdemo;

    import java.io.FileNotFoundException;

    import java.io.IOException;

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.fs.BlockLocation;

    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.Before;

    import org.junit.Test;

    public class HDFS_CRUD {

    FileSystem fs = null;

    @Before

    public void init() throws Exception {

    // 構造一個配置參數對象,設置一個參數:我們要訪問的hdfs的URI

    Configuration conf = new Configuration();

    // 這里指定使用的是HDFS文件系統(tǒng)

    conf.set(“fs.defaultFS”, “hdfs://hadoop01:9000”);

    // 通過如下的方式進行客戶端身份的設置

    System.setProperty(“HADOOP_USER_NAME”, “root”);

    // 通過FileSystem的靜態(tài)方法獲取文件系統(tǒng)客戶端對象

    fs = FileSystem.get(conf);

    }

    @Test

    public void testAddFileToHdfs() throws IOException {

    // 要上傳的文件所在本地路徑

    Path src = new Path(“D:/test.txt”);

    // 要上傳到hdfs的目標路徑

    Path dst = new Path(“/testFile”);

    // 上傳文件方法

    fs.copyFromLocalFile(src, dst);

    // 關閉資源

    fs.close();

    }

    // 從hdfs中復制文件到本地文件系統(tǒng)

    @Test

    public void testDownloadFileToLocal() throws IllegalArgumentException, IOException {

    // 下載文件

    fs.copyToLocalFile(new Path(“/testFile”), new Path(“D:/”));

    }

    // 創(chuàng)建,刪除,重命名文件

    @Test

    public void testMkdirAndDeleteAndRename() throws Exception {

    // 創(chuàng)建目錄

    fs.mkdirs(new Path(“/a/b/c”));

    fs.mkdirs(new Path(“/a2/b2/c2”));

    // 重命名文件或文件夾

    fs.rename(new Path(“/a”), new Path(“/a3”));

    // 刪除文件夾,如果是非空文件夾,參數2必須給值true

    fs.delete(new Path(“/a2”), true);

    }

    // 查看目錄信息,只顯示文件

    @Test

    public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {

    // 獲取迭代器對象

    RemoteIterator listFiles = fs.listFiles(new Path(“/”), true);

    while (listFiles.hasNext()) {

    LocatedFileStatus fileStatus = listFiles.next();

    // 打印當前文件名

    System.out.println(fileStatus.getPath().getName());

    // 打印當前文件塊大小

    System.out.println(fileStatus.getBlockSize());

    // 打印當前文件權限

    System.out.println(fileStatus.getPermission());

    // 打印當前文件內容長度

    System.out.println(fileStatus.getLen());

    // 獲取該文件塊信息(包含長度,數據塊,datanode的信息)

    BlockLocation[] blockLocations = fileStatus.getBlockLocations();

    for (BlockLocation bl : blockLocations) {

    System.out.println(“block-length:” + bl.getLength() + “–” + “block-offset:” + bl.getOffset());

    String[] hosts = bl.getHosts();

    for (String host : hosts) {

    System.out.println(host);

    }

    }

    System.out.println(“—————————-“);

    }

    }

    // 查看文件及文件夾信息

    @Test

    public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {

    // 獲取HDFS系統(tǒng)中文件和目錄的元數據等信息

    FileStatus[] listStatus = fs.listStatus(new Path(“/”));

    String flag = “d– “;

    for (FileStatus fstatus : listStatus) {

    // 判斷是文件還是文件夾

    if (fstatus.isFile())

    flag = “f– “;

    System.out.println(flag + fstatus.getPath().getName());

    }

    }

    }

    鄭重聲明:本文內容及圖片均整理自互聯(lián)網,不代表本站立場,版權歸原作者所有,如有侵權請聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月16日 12:03
    下一篇 2022年6月16日 12:04

    相關推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息