MapFile即是已经排好序的SequenceFile,已加入用于搜索键的索引。
MapFile是排序后的SequenceFile,通过观察其目录结构可以看到MapFile由两部分组成,分别是data和index。即:
集群上文件:“hdfs://master:9000/mapfile/numbers.map/data”
“hdfs://master:9000/mapfile/numbers.map/index”
data即数据文件中包含所有需要存储的key-value对,按key的顺序排列。
index作为文件的数据索引,主要记录了每个Record的key值,以及该Record在文件中的偏移位置。在MapFile被访问的时候,索引文件会被加载到内存,通过索引映射关系可迅速定位到指定Record所在文件位置,因此,相对SequenceFile而言,MapFile的检索效率是高效的,缺点是会消耗一部分内存来存储index数据。
需注意的是,MapFile并不会把所有Record都记录到index中去,默认情况下每隔128条记录存储一个索引映射。当然,记录间隔可人为修改,通过MapFIle.Writer的setIndexInterval()方法,或修改io.map.index.interval属性;
另外,与SequenceFile不同的是,MapFile的KeyClass一定要实现WritableComparable接口,即Key值是可比较的。
下面给出MapFile的重建索引代码:
package com.tht.hadoopIO;
//cc MapFileFixer Re-creates the index for a MapFile
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MapFile;
import org.apache.hadoop.io.SequenceFile;
//vv MapFileFixer
public class MapFileFixer {
public static void main(String[] args) throws Exception {
// String mapUri = args[0];
String mapUri = "hdfs://master:9000/mapfile/numbers.map";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(mapUri), conf);
Path map = new Path(mapUri);
Path mapData = new Path(map, MapFile.DATA_FILE_NAME);
// Get key and value types from data sequence file
SequenceFile.Reader reader = new SequenceFile.Reader(fs, mapData, conf);
Class keyClass = reader.getKeyClass();
Class valueClass = reader.getValueClass();
reader.close();
// Create the map file index file
long entries = MapFile.fix(fs, map, keyClass, valueClass, false, conf);
System.out.printf("Created MapFile %s with %d entries\n", map, entries);
}
}
// ^^ MapFileFixer
在"hdfs://master:9000/mapfile/number.map/"下的"index"文件损坏,或没有时,可以用此方法来重建。
分享到:
相关推荐
`Hadoop: The Definitive Guide`中可能会讲解如何创建、读取和操作HDFS上的文件,以及如何配置HDFS参数以优化性能。 MapReduce是Hadoop处理大数据的主要计算模型,它将大规模数据处理任务分解为小的“映射”和...
With this digital Early Release edition of Hadoop: The Definitive Guide, you get the entire book bundle in its earliest form – the author’s raw and unedited content – so you can take advantage of ...
Hadoop: The Definitive Guide, 4th Edition Get ready to unlock the power of your data. With the fourth edition of this comprehensive guide, you’ll learn how to build and maintain reliable, scalable,...
《Hadoop:权威指南》是了解和掌握Apache Hadoop生态系统不可或缺的一本著作。这本书由Tom White撰写,全面深入地介绍了Hadoop的各个组件及其工作原理,对于初学者和专业人士来说都是一份宝贵的参考资料。 Hadoop是...
《Hadoop:The Definitive Guide》是O'REILLY出版社出版的一本关于Apache Hadoop的权威指南,目前流行的是第四版。这本书为读者提供了一个全面的Hadoop学习平台,内容涵盖了如何构建和维护一个既可靠又可扩展的...
- **书名**:《Hadoop:The Definitive Guide》(第二版) - **作者**:Tom White - **前言作者**:Doug Cutting - **出版社**:O'Reilly Media, Inc. - **出版日期**:2010年10月 - **版权**:版权所有 © 2011 Tom...
Hadoop是一个由Apache软件基金会开发的开源框架,它允许通过简单的编程模型跨分布式环境存储和处理大数据。其设计目标是可伸缩、高效以及能够从单个服务器的单个机架到数千台机器的大规模商用服务器集群实现容错。...
Hadoop- The Definitive Guide, 4th Edition
《Hadoop: The Definitive Guide, Third Edition》是Tom White对于Hadoop的深入剖析之作,涵盖了Hadoop的多个方面,包括它的历史、架构和应用。本书为读者展示了Hadoop的生态系统和核心概念,包括分布式文件系统...
The fourth edition covers Hadoop 2 exclusively. The Hadoop 2 release series is the current active release series and contains the most stable versions of Hadoop. There are new chapters covering YARN ...
《Hadoop权威指南》是大数据领域的一本经典著作,它详细介绍了Apache Hadoop生态系统的核心组件、工作原理以及实际应用。这本书分为中文版和英文版,为读者提供了双语学习的选择,且带有书签,便于查阅和学习,无需...
《Spark: The Definitive Guide: Big Data Processing Made Simple》是大数据处理领域的经典著作,由Databricks的创始人之一Michael Armbrust等专家撰写。这本书深入浅出地介绍了Apache Spark的核心概念、架构以及...
EPUB版本: Hadoop -The Definitive Guide, 4th edition.epub