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

本文源碼:GitHub·點這裏 || GitEE·點這裏java

1、讀寫機制

一、數據寫入

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

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

二、數據讀取

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

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

2、基礎API案例

一、基礎演示接口

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 ;
}

二、命令API用法

@Service
public class HdfsFileServiceImpl implements HdfsFileService {

    @Resource
    private HdfsConfig hdfsConfig ;

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

    @Override
    public void isFile(String path) throws Exception {
        // 一、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 二、判斷文件和文件夾
        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());
            }
        }
        // 三、關閉資源
        fileSystem.close();
    }

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

    @Override
    public void fileDetail(String path) throws Exception {
        // 一、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                               configuration, "root");
        // 二、讀取文件詳情
        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==============");
        }
        // 三、關閉資源
        fileSystem.close();
    }

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

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

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

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

    @Override
    public void ioDown(String path, String local) throws Exception {
        // 一、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                configuration, "root");
        // 二、輸入輸出流
        FSDataInputStream fis = fileSystem.open(new Path(path));
        FileOutputStream fos = new FileOutputStream(new File(local));
        // 三、流對拷
        IOUtils.copyBytes(fis, fos, configuration);
        // 四、關閉資源
        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 {
        // 一、獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI(hdfsConfig.getNameNode()),
                                configuration, "root");
        // 二、輸入輸出流
        FSDataInputStream fis = fileSystem.open(new Path(path));
        FileOutputStream fos = new FileOutputStream(new File(local));
        // 三、部分拷貝
        byte[] buf = new byte[1024];
        for(int i =0 ; i < 1024 * 128; i++){
            fis.read(buf);
            fos.write(buf);
        }
        // 四、關閉資源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fileSystem.close();
    }

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

三、合併切割文件

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

3、機架感知

Hadoop2.7的文檔說明node

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

第一個副本和client在一個節點裏,若是client不在集羣範圍內,則這第一個node是隨機選取的;第二個副本和第一個副本放在相同的機架上隨機選擇;第三個副本在不一樣的機架上隨機選擇,減小了機架間的寫流量,一般能夠提升寫性能,機架故障的機率遠小於節點故障的機率,所以該策略不會影響數據的穩定性。git

4、網絡拓撲

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

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

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

5、源代碼地址

GitHub·地址
https://github.com/cicadasmile/big-data-parent
GitEE·地址
https://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·點這裏 ☆☆☆☆☆
相關文章
相關標籤/搜索