Hadoop MetaData(元数据) 介绍. 和Block File 信息获得

19 篇文章 0 订阅

hadoop 管理数据的机制

     hadoop 用来存储文件是很好,但是要去对存储好的文件进行update,delete,操作,相对就不是那么好操作了,但是非要
做这样的操作,该如何办呐 ?
     a. 先去看hadoop 存文件是怎么存的,是怎么读|写的,
     b.根据a 的结论,找到读|写的方式也就找到了如何去delete,update 的方式了.
     hadoop 的数据都是被 namenode 管理在内存和文件系统中的,用hadoop metadta来标记数据存储的位置,名字,目录等信息.
    hadoop 写数据的实例是:https://sites.google.com/site/hadoopandhive/home/how-to-write-a-file-in-hdfs-using-hadoop 
    hadoop 读数据的实例是:https://sites.google.com/site/hadoopandhive/home/hadoop-how-to-read-a-file-from-hdfs
    hadoop 元数据介绍:
     http://www.dummies.com/how-to/content/hadoop-distributed-file-system-hdfs-for-big-data-p.html
     http://zh.hortonworks.com/blog/hdfs-metadata-directories-explained/

    以下是在 hadoop hdfs 上 输出某个文件目录下的所有的Block File 的信息

      FileSystem myFs = FileSystem.get(“hdfs://localhost:9000”, new Configure());

         //get all files in the directory
         RemoteIterator<LocatedFileStatus> localFiles = myFs.listFiles(myPath, false);
         while (localFiles.hasNext()) {
            LocatedFileStatus localFile = (LocatedFileStatus) localFiles.next();
            String tagPath = localFile.getPath().toString();
            int index = tagPath.lastIndexOf("/");
            String blockFileName  = tagPath.substring(index+1, tagPath.length());
            String blockPath = tagPath.substring(0,index);
            System.err.println(blockPath );
            System.err.println(blockFileName);
         }

      如此就可以将所有的Block File 的信息罗列出来,还能知道每个Block的Size 。

    分析:hdfs 其实和 windows 的文件系统是一个道理,因为都是文件系统,

    理论都是一样的。不管怎么变化,万变不离其宗...



以下为转载整理内容

1、元数据(Metadata)

维护HDFS文件系统中文件和目录的信息,分为内存元数据和元数据文件两种。NameNode维护整个元数据。
HDFS实现时,没有采用定期导出元数据的方法,而是采用元数据镜像文件(FSImage)+日子文件(edits)的备份机制。

2、Block:文件内容而言。

寻路径流程:
          路径信息                         bocks[]                                   triplets[]          
Client ------------》INode---------------------》BlockInfo --------------------------》DataNode。
INode:文件的基本元素:文件和目录
BlockInfo: 文件内容对象
DatanodeDescriptor:具体存储对象。

3 、 FSImage和edits的checkPoint。

FSImage有2个状态,分别是FsImage和FsImage.ckpt,后者表示正在checkpoint的过程中,上传后将会修改为FSImage文件,同理edits也有两个状态,edits和edits.new。

4、NameNode format情景分析:

  • 遍历元数据存储目录,提示用户是否格式化?(NameNode.java里format函数)
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private static boolean format( Configuration conf ,  
  2.                                 boolean isConfirmationNeeded )  
  3.       throws IOException {  
  4.     Collection<URI > dirsToFormat = FSNamesystem. getNamespaceDirs(conf );  
  5.     Collection<URI > editDirsToFormat =  
  6.                  FSNamesystem .getNamespaceEditsDirs (conf );  
  7.     for( Iterator< URI> it = dirsToFormat.iterator (); it. hasNext() ;) {  
  8.       File curDir = new File (it .next (). getPath()) ;  
  9.       if (! curDir. exists())  
  10.         continue;  
  11.       if (isConfirmationNeeded ) {  
  12.         System .err .print ("Re-format filesystem in " + curDir + " ? (Y or N) ");  
  13.         if (! (System .in .read () == 'Y')) {  
  14.           System .err .println ("Format aborted in " + curDir );  
  15.           return true ;  
  16.         }  
  17.         while(System .in .read () != '\n') ; // discard the enter-key  
  18.       }  
  19.     }  
  20.   
  21.     FSNamesystem nsys = new FSNamesystem (new FSImage(dirsToFormat ,  
  22.                                          editDirsToFormat ), conf) ;  
  23.     nsys.dir.fsImage .format ();  
  24.     return false;  
  25.   }  
  • 创建元数据内存镜像,包括类FSNamesystem实例化对象,类FSDirectory实例化对象,类FSImage对象,类Edits对象。创建FsNameSystem对象主要完成:BlockManager,FSDirectory对象以及初始化成员变量。FSImage对象主要完成对layoutVersion、namespaceID,CTime赋值为0,实例化FSEditLog。在类FSDirectory,创建了HDFS根目录节点rootDir。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. FSNamesystem( FSImage fsImage, Configuration conf ) throws IOException {  
  2.     this. blockManager = new BlockManager (this, conf) ;  
  3.     setConfigurationParameters (conf );  
  4.     this. dir = new FSDirectory(fsImage , this, conf );  
  5.     dtSecretManager = createDelegationTokenSecretManager (conf );  
  6.   }  
  7.   
  8.   FSImage( Collection< URI> fsDirs , Collection< URI> fsEditsDirs )  
  9.       throws IOException {  
  10.     this() ;  
  11.     setStorageDirectories( fsDirs, fsEditsDirs );  
  12.   }  
  13.   
  14.  void setStorageDirectories(Collection <URI > fsNameDirs,  
  15.                              Collection< URI> fsEditsDirs ) throws IOException {  
  16.     this. storageDirs = new ArrayList <StorageDirectory >() ;  
  17.     this. removedStorageDirs = new ArrayList <StorageDirectory >() ;  
  18.      
  19.    // Add all name dirs with appropriate NameNodeDirType  
  20.     for (URI dirName : fsNameDirs ) {  
  21.       checkSchemeConsistency (dirName );  
  22.       boolean isAlsoEdits = false;  
  23.       for (URI editsDirName : fsEditsDirs) {  
  24.         if (editsDirName .compareTo (dirName ) == 0) {  
  25.           isAlsoEdits = true;  
  26.           fsEditsDirs .remove (editsDirName );  
  27.           break;  
  28.         }  
  29.       }  
  30.       NameNodeDirType dirType = (isAlsoEdits ) ?  
  31.                           NameNodeDirType .IMAGE_AND_EDITS :  
  32.                           NameNodeDirType .IMAGE ;  
  33.       // Add to the list of storage directories, only if the  
  34.       // URI is of type file://  
  35.       if(dirName .getScheme (). compareTo( JournalType.FILE .name (). toLowerCase())  
  36.           == 0){  
  37.         this.addStorageDir (new StorageDirectory(new File(dirName. getPath()) ,  
  38.             dirType ));  
  39.       }  
  40.     }  
  41.      
  42.     // Add edits dirs if they are different from name dirs  
  43.     for (URI dirName : fsEditsDirs ) {  
  44.       checkSchemeConsistency (dirName );  
  45.       // Add to the list of storage directories, only if the  
  46.       // URI is of type file://  
  47.       if(dirName .getScheme (). compareTo( JournalType.FILE .name (). toLowerCase())  
  48.           == 0)  
  49.         this.addStorageDir (new StorageDirectory(new File(dirName. getPath()) ,  
  50.                     NameNodeDirType .EDITS ));  
  51.     }  
  52.   }  
  • 对内存镜像数据中的数据结构进行初始化:主要有FSImage的format函数完成,layoutVersion:软件所处的版本。namespaceID:在Format时候产生,当data node注册到Name Node后,会获得该NameNode的NameSpaceID,并作为后续与NameNode通讯的身份标识。对于未知身份的Data Node,NameNode拒绝通信。CTime:表示FSimage产生的时间。checkpointTime:表示NameSpace第一次checkpoint的时间。  
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public void format () throws IOException {  
  2.    this. layoutVersion = FSConstants .LAYOUT_VERSION ;  
  3.    this. namespaceID = newNamespaceID ();  
  4.    this. cTime = 0L ;  
  5.    this. checkpointTime = FSNamesystem .now ();  
  6.    for (Iterator <StorageDirectory > it =  
  7.                           dirIterator (); it. hasNext() ;) {  
  8.      StorageDirectory sd = it .next ();  
  9.      format (sd );  
  10.    }  
  11.  }  
  • 对内存镜像写入元数据备份目录。FSImage的format方法会遍历所有的目录进行备份。如果是FSImage的文件目录,则调用saveFSImage保存FSImage,如果是Edits,则调用editLog.createEditLogFile,最后调用sd.write方法创建fstime和VERSION文件。VERSION文件通常最后写入。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. void format(StorageDirectory sd ) throws IOException {  
  2.     sd.clearDirectory (); // create currrent dir  
  3.     sd.lock ();  
  4.     try {  
  5.       saveCurrent (sd );  
  6.     } finally {  
  7.       sd .unlock ();  
  8.     }  
  9.     LOG.info ("Storage directory " + sd. getRoot()  
  10.              + " has been successfully formatted.");  
  11.   }  
最后分析一下元数据应用的场景:
1、格式化时。
2、Hadoop启动时。
3、元数据更新操作时。
4、如果NameNode与Secondary NameNode、Backup Node或checkpoint Node配合使用时,会进行checkPoint操作。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值