Hadoop-common之WritableFactories

      WritableFactories是个工厂类,通过它可以创建实例对象。原文中对此对象的注释是Factories for non-public writables,我的理解是处理那些不确定类型的对象进行实例化。例如在ObjectWritable的readObject方法中就调用了WritableFactories     

 public static Object readObject(DataInput in, ObjectWritable objectWritable, Configuration conf)
    throws IOException {
	.......
                  // Writable
      Class instanceClass = null;
      String str = UTF8.readString(in);
      instanceClass = loadClass(conf, str);
      
      Writable writable = WritableFactories.newInstance(instanceClass, conf);
      writable.readFields(in);
      instance = writable;

     .....

    return instance;
      
  }

     WritableFactories提供了注册机制,用户可以对某种类型自定义一个WritableFactory,然后调用WritableFactories的setFactory方法进行注册。WritableFactories.setFactory() 需要两个参数,分别是 

注册类对应的类对象和能够构造注册类的WritableFactory 接口的实现     

 private static final Map<Class, WritableFactory> CLASS_TO_FACTORY =
    new ConcurrentHashMap<Class, WritableFactory>();

  private WritableFactories() {}                  // singleton

  /** Define a factory for a class. */
  public static void setFactory(Class c, WritableFactory factory) {
    CLASS_TO_FACTORY.put(c, factory);
  }

    在HDFS的Block类中,就调用了此方法    

public class Block implements Writable, Comparable<Block> {
  public static final String BLOCK_FILE_PREFIX = "blk_";
  public static final String METADATA_EXTENSION = ".meta";
  static {                                      // register a ctor
    WritableFactories.setFactory
      (Block.class,
       new WritableFactory() {
         @Override
         public Writable newInstance() { return new Block(); }
       });
  }

    WritableFactories的核心方法当然是创建对接咯:newInstance。它的处理逻辑很简单,首先通过类型找到对应的工厂类,然后用工厂类创建实例对象,如果没有工厂类,则用Java的反射机制创建   

/** Create a new instance of a class with a defined factory. */
  public static Writable newInstance(Class<? extends Writable> c, Configuration conf) {
	//根据类型获取对应的工厂实现类
    WritableFactory factory = WritableFactories.getFactory(c);
    if (factory != null) {
      //通过实现工厂创建实例
      Writable result = factory.newInstance();
      //如果实例是可配置的,注入Configuration对象
      if (result instanceof Configurable) {
        ((Configurable) result).setConf(conf);
      }
      return result;
    } else {
      //如果没有对应实现类,则用反射创建实例
      return ReflectionUtils.newInstance(c, conf);
    }
  }

 

你可能感兴趣的:(hadoop,common,factory)