读取HDFS相关的数据源时,大量使用mapreduce封装的读取数据源的方式,而一个mapreduce job会依赖InputFormat对读取的数据进行格式校验、输入切分等操作。读取HBase数据源,则使用了TableInputFormat。先来看看InputFormat。
InputFormat
InputFormat是mapreduce提供的数据源格式接口,也就是说,通过该接口可以支持读取各种各样的数据源(文件系统,数据库等),从而进行mapreduce计算。
看下InputFormat接口定义:
public abstract class InputFormat<K, V> {
/**
* Logically split the set of input files for the job.
*
* @param context job configuration.
* @return an array of {@link InputSplit}s for the job.
*/
public abstract
List<InputSplit> getSplits(JobContext context
) throws IOException, InterruptedException;
/**
* Create a record reader for a given split. The framework will call
* {@link RecordReader#initialize(InputSplit, TaskAttemptContext)} before
* the split is used.
*/
public abstract
RecordReader<K,V> createRecordReader(InputSplit split,
TaskAttemptContext context
) throws IOException,
InterruptedException;
}
getSplits决定逻辑分区的策略,createRecordReader提供了获取切分后分区记录的迭代器。
TableInputFormat
TalbeInputFormat是HBase提供的接口,看看他的分区策略:
RegionSizeCalculator sizeCalculator =
new RegionSizeCalculator(getRegionLocator(), getAdmin());
T