您好,欢迎访问一九零五行业门户网

2.3 Configurable接口

2.3 configurable接口 configurable是一个很简单的接口,也位于org.apache.hadoop.conf包中,其类图如图2-3所示。 从字面理解,configurable的含义是可配置的,如果一个类实现了configurable接口,意味着这个类是可配置的。也就是说,可以通过为这个类的对象
2.3 configurable接口
configurable是一个很简单的接口,也位于org.apache.hadoop.conf包中,其类图如图2-3所示。
从字面理解,configurable的含义是可配置的,如果一个类实现了configurable接口,意味着这个类是可配置的。也就是说,可以通过为这个类的对象传入一个configuration实例,提供对象工作需要的一些配置信息。hadoop的代码中有大量的类实现了configurable接口,如org.apache.hadoop.mapred.sequencefileinputfilter.regexfilter。regexfilter对象工作时,需要提供一个正则表达式,用于过滤读取的记录。由于regexfilter的父类filter中实现的configurable接口,regexfilter可以在它的setconf()方法中,使用configuration.get()方法获取以字符串传入的正则表达式,并初始化成员变量p。相关代码如下:
public void setconf(configuration conf) {    //在conf中获取键为sequencefile.filter.regex(filter_regex)的配置项    string regex = conf.get(filter_regex);     if (regex==null)       throw new runtimeexception(filter_regex + not set);    this.p = pattern.compile(regex);    this.conf = conf;  }  
configurable.setconf()方法何时被调用呢?一般来说,对象创建以后,就应该使用setconf()方法,为对象提供进一步的初始化工作。为了简化对象创建和调用setconf()方法这两个连续的步骤,org.apache.hadoop.util.reflectionutils中提供了静态方法newinstance(),代码如下:
public static t> t newinstance(classt>theclass, configuration conf) 
方法newinstance()利用java反射机制,根据对象类型信息(参数theclass),创建一个新的相应类型的对象,然后调用reflectionutils中的另一个静态方法setconf()配置对象,代码如下:
public static void setconf(object theobject, configuration conf) {    if(conf != null) {       //传入的对象实现了configurable接口       if(theobject instanceof configurable) {          //调用对象的setconf方法,传入configuration对象          ((configurable) theobject).setconf(conf);       }       setjobconf(theobject, conf);    }  } 
在setconf()中,如果对象实现了configurable接口,那么对象的setconf()方法会被调用,并根据configuration类的实例conf进一步初始化对象。
其它类似信息

推荐信息