大数据全系列 教程
1869个小节阅读:464.9k
目录
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
###5.4.4 mapper、inputFormat和input对象的创建
1. Mapper对象如何创建的?
xxxxxxxxxx
// make a mapper 创建Mapper对象
org.apache.hadoop.mapreduce.Mapper<INKEY,INVALUE,OUTKEY,OUTVALUE> mapper =
(org.apache.hadoop.mapreduce.Mapper<INKEY,INVALUE,OUTKEY,OUTVALUE>)
ReflectionUtils.newInstance(taskContext.getMapperClass(), job);
点击进入getMapperClass()方法中->JobContext接口中:
xxxxxxxxxx
public Class<? extends Mapper<?,?,?,?>> getMapperClass()
throws ClassNotFoundException;
选中getMapperClass()之后Ctrl+Alt+B->JobContextImpl:
xxxxxxxxxx
public Class<? extends Mapper<?,?,?,?>> getMapperClass()
throws ClassNotFoundException {
return (Class<? extends Mapper<?,?,?,?>>)
conf.getClass(MAP_CLASS_ATTR, Mapper.class);
}
去配置文件中查找MAP_CLASS_ATTR常量对应(mapreduce.job.map.class)的自定义的Mapper类,如果找到则使用自定义的Mapper类,如果找不到使用Mapper.class。
使用mapreduce.job.map.class去job.xml中查找对应的Mapper类:
xxxxxxxxxx
<property>
<name>mapreduce.job.map.class</name>
<value>com.itbaizhan.WCMapper</value>
<final>false</final>
<source>programmatically</source>
</property>
对应的自定义的Mapper类就是com.itbaizhan.WCMapper。对应的设置代码就是在WCDriver类中:
xxxxxxxxxx
job.setMapperClass(WCMapper.class);
//也就是:
/**
* Set the {@link Mapper} for the job.
* @param cls the <code>Mapper</code> to use
* @throws IllegalStateException if the job is submitted
*/
public void setMapperClass(Class<? extends Mapper> cls
) throws IllegalStateException {
ensureState(JobState.DEFINE);
conf.setClass(MAP_CLASS_ATTR, cls, Mapper.class);
}
思考:为什么在WCDriver类设置好Mapper类之后可以在MapTask中直接使用?设置和使用谁先谁后?
参考答案:WCDriver在客户端执行的,搞定一切配置信息之后,才会提交作业,只有提交作业之后才开始运行MapTask.
2. inputFormat对象如何创建的?
xxxxxxxxxx
// make the input format
org.apache.hadoop.mapreduce.InputFormat<INKEY,INVALUE> inputFormat =
(org.apache.hadoop.mapreduce.InputFormat<INKEY,INVALUE>)
ReflectionUtils.newInstance(taskContext.getInputFormatClass(), job);
taskContext.getInputFormatClass()->JobContext接口中:
xxxxxxxxxx
public Class<? extends InputFormat<?,?>> getInputFormatClass()
throws ClassNotFoundException;
选择getInputFormatClass() 后Ctrl+Alt+B->选择JobContextImpl
xxxxxxxxxx
public Class<? extends InputFormat<?,?>> getInputFormatClass()
throws ClassNotFoundException {
return (Class<? extends InputFormat<?,?>>)
conf.getClass(INPUT_FORMAT_CLASS_ATTR, TextInputFormat.class);
}
如果设置过使用自定义的InputFormat类,没有指定就使用TextInputFormat。可以通过如下方式设置:
xxxxxxxxxx
job.setInputFormatClass(KeyValueTextInputFormat.class);
默认使用的就是TextInputFormat类。