Hadoop框架:HDFS讀寫機制與API詳解

本文源碼:GitHub·點這裡 || GitEE·點這裡

一、讀寫機制

1、數據寫入

  • 客戶端訪問NameNode請求上傳文件;
  • NameNode檢查目標文件和目錄是否已經存在;
  • NameNode響應客戶端是否可以上傳;
  • 客戶端請求NameNode文件塊Block01上傳服務位置;
  • NameNode響應返回3個DataNode節點;
  • 客戶端通過輸入流建立DataNode01傳輸通道;
  • DataNode01調用DataNode02,DataNode02調用DataNode03,通信管道建立完成;
  • DataNode01、DataNode02、DataNode03逐級應答客戶端。
  • 客戶端向DataNode01上傳第一個文件塊Block;
  • DataNode01接收後傳給DataNode02,DataNode02傳給DataNode03;
  • Block01傳輸完成之後,客戶端再次請求NameNode上傳第二個文件塊;

2、數據讀取

  • 客戶端通過向NameNode請求下載文件;
  • NameNode查詢獲取文件元數據並返回;
  • 客戶端通過元數據信息獲取文件DataNode地址;
  • 就近原則選擇一台DataNode服務器,請求讀取數據;
  • DataNode傳輸數據返回給客戶端;
  • 客戶端以本地處理目標文件;

二、基礎API案例

1、基礎演示接口

public interface HdfsFileService {

    // 創建文件夾
    void mkdirs(String path) throws Exception ;

    // 文件判斷
    void isFile(String path) throws Exception ;

    // 修改文件名
    void reName(String oldFile, String newFile) throws Exception ;

    // 文件詳情
    void fileDetail(String path) throws Exception ;

    // 文件上傳
    void copyFromLocalFile(String local, String path) throws Exception ;

    // 拷貝到本地:下載
    void copyToLocalFile(String src, String dst) throws Exception ;

    // 刪除文件夾
    void delete(String path) throws Exception ;

    // IO流上傳
    void ioUpload(String path, String local) throws Exception ;

    // IO流下載
    void ioDown(String path, String local) throws Exception ;

    // 分塊下載
    void blockDown(String path, String local1, String local2) throws Exception ;
}

2、命令API用法

@Service
public class HdfsFileServiceImpl implements HdfsFileService {

    @Resource
    private HdfsConfig hdfsConfig ;

    @Override
    public void mkdirs(String path) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 2、創建目錄
        fileSystem.mkdirs(new Path(path));
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void isFile(String path) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 2、判斷文件和文件夾
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path(path));
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isFile()) {
                System.out.println("文件:"+fileStatus.getPath().getName());
            }else {
                System.out.println("文件夾:"+fileStatus.getPath().getName());
            }
        }
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void reName(String oldFile, String newFile) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                configuration, "root");
        // 2、修改文件名
        fileSystem.rename(new Path(oldFile), new Path(newFile));
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void fileDetail(String path) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 2、讀取文件詳情
        RemoteIterator<LocatedFileStatus> listFiles =
                                    fileSystem.listFiles(new Path(path), true);
        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();
            System.out.println("文件名:"+status.getPath().getName());
            System.out.println("文件長度:"+status.getLen());
            System.out.println("文件權限:"+status.getPermission());
            System.out.println("所屬分組:"+status.getGroup());
            // 存儲塊信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                // 塊存儲的主機節點
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.print(host+";");
                }
            }
            System.out.println("==============Next==============");
        }
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void copyFromLocalFile(String local, String path) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                configuration, "root");
        // 2、執行上傳操作
        fileSystem.copyFromLocalFile(new Path(local), new Path(path));
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void copyToLocalFile(String src,String dst) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 2、執行下載操作
        // src 服務器文件路徑 ; dst 文件下載到的路徑
        fileSystem.copyToLocalFile(false, new Path(src), new Path(dst), true);
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void delete(String path) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                configuration, "root");
        // 2、刪除文件或目錄 是否遞歸
        fileSystem.delete(new Path(path), true);
        // 3、關閉資源
        fileSystem.close();
    }

    @Override
    public void ioUpload(String path, String local) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                configuration, "root");
        // 2、輸入輸出流
        FileInputStream fis = new FileInputStream(new File(local));
        FSDataOutputStream fos = fileSystem.create(new Path(path));
        // 3、流對拷
        IOUtils.copyBytes(fis, fos, configuration);
        // 4、關閉資源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }

    @Override
    public void ioDown(String path, String local) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                configuration, "root");
        // 2、輸入輸出流
        FSDataInputStream fis = fileSystem.open(new Path(path));
        FileOutputStream fos = new FileOutputStream(new File(local));
        // 3、流對拷
        IOUtils.copyBytes(fis, fos, configuration);
        // 4、關閉資源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }

    @Override
    public void blockDown(String path,String local1,String local2) throws Exception {
        readFileSeek01(path,local1);
        readFileSeek02(path,local2);
    }

    private void readFileSeek01(String path,String local) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                configuration, "root");
        // 2、輸入輸出流
        FSDataInputStream fis = fileSystem.open(new Path(path));
        FileOutputStream fos = new FileOutputStream(new File(local));
        // 3、部分拷貝
        byte[] buf = new byte[1024];
        for(int i =0 ; i < 1024 * 128; i++){
            fis.read(buf);
            fos.write(buf);
        }
        // 4、關閉資源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }

    private void readFileSeek02(String path,String local) throws Exception {
        // 1、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                configuration, "root");
        // 2、輸入輸出流
        FSDataInputStream fis = fileSystem.open(new Path(path));
        // 定位輸入數據位置
        fis.seek(1024*1024*128);
        FileOutputStream fos = new FileOutputStream(new File(local));
        // 3、流拷貝
        IOUtils.copyBytes(fis, fos, configuration);
        // 4、關閉資源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }
}

3、合併切割文件

cat hadoop-2.7.2.zip.block1 hadoop-2.7.2.zip.block2 > hadoop.zip

三、機架感知

Hadoop2.7的文檔說明

第一個副本和client在一個節點裏,如果client不在集群範圍內,則這第一個node是隨機選取的;第二個副本和第一個副本放在相同的機架上隨機選擇;第三個副本在不同的機架上隨機選擇,減少了機架間的寫流量,通常可以提高寫性能,機架故障的概率遠小於節點故障的概率,因此該策略不會影響數據的穩定性。

四、網絡拓撲

HDFS寫數據的過程中,NameNode會選擇距離待上傳數據最近距離的DataNode接收數據,基於機架感知,NameNode就可以畫出上圖所示的datanode網絡拓撲圖。D1,R1都是交換機,最底層是datanode。

Distance(/D1/R1/N1,/D1/R1/N1)=0  相同的節點
Distance(/D1/R1/N1,/D1/R1/N2)=2  同一機架下的不同節點
Distance(/D1/R1/N1,/D1/R2/N1)=4  同一IDC下的不同datanode
Distance(/D1/R1/N1,/D2/R3/N1)=6  不同IDC下的datanode

五、源代碼地址

GitHub·地址
//github.com/cicadasmile/big-data-parent
GitEE·地址
//gitee.com/cicadasmile/big-data-parent

推薦閱讀:編程體系整理

序號 項目名稱 GitHub地址 GitEE地址 推薦指數
01 Java描述設計模式,算法,數據結構 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆☆
02 Java基礎、並發、面向對象、Web開發 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆
03 SpringCloud微服務基礎組件案例詳解 GitHub·點這裡 GitEE·點這裡 ☆☆☆
04 SpringCloud微服務架構實戰綜合案例 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆☆
05 SpringBoot框架基礎應用入門到進階 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆
06 SpringBoot框架整合開發常用中間件 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆☆
07 數據管理、分佈式、架構設計基礎案例 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆☆
08 大數據系列、存儲、組件、計算等框架 GitHub·點這裡 GitEE·點這裡 ☆☆☆☆☆