spatialhadoop2.1源码阅读(三) 自定义InputFormat(SpatialInputFormat & ShapeInputFormat)


SpatialInputFormat实现了InputFormat两大功能中的其中一个:getRecordReader。另外一个功能getSplits默认继承了FileInputFormat类的实现。

SpatialInputFormat的getRecordReader具体实现如下:

@SuppressWarnings("rawtypes")
  protected Class<? extends RecordReader> rrClass;
  
  @SuppressWarnings("unchecked")
  @Override
  public RecordReader<K, V> getRecordReader(InputSplit split, JobConf job,
      Reporter reporter) throws IOException {
    // Create compressionCodecs to be used by isSplitable method
    if (compressionCodecs == null)
      compressionCodecs = new CompressionCodecFactory(job);
    if (split instanceof FileSplit) {
      FileSplit fsplit = (FileSplit) split;
      if (fsplit.getPath().getName().toLowerCase().endsWith(".hdf")) {
        // HDF File. Create HDFRecordReader
        return (RecordReader<K, V>) new HDFRecordReader(job, fsplit,
            job.get(HDFRecordReader.DatasetName),
            job.getBoolean(HDFRecordReader.SkipFillValue, true));
      }
      try {
        @SuppressWarnings("rawtypes")
        Constructor<? extends RecordReader> rrConstructor;
        rrConstructor = rrClass.getDeclaredConstructor(constructorSignature);
        rrConstructor.setAccessible(true);
        return rrConstructor.newInstance(new Object [] {job, fsplit});
      } catch (SecurityException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (InstantiationException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
      throw new RuntimeException("Cannot generate a record reader");
    } else {
      throw new RuntimeException("Cannot handle splits of type "+split.getClass());
    }
  }
其中,rrclass为实际的RecordReader类,采用反射的方式生成,子类只需要继承SpatialInputFormat类,并指定rrclass。再调用父类的getRecordReader方法,即可产生 RecordReader类。


ShapeInputFormat类继承了SpatialInputFormat类,其覆盖了父类的getRecordReader方法,具体实现如下:

public class ShapeInputFormat<S extends Shape> extends SpatialInputFormat<Rectangle, S> {
  
  @Override
  public RecordReader<Rectangle, S> getRecordReader(InputSplit split,
      JobConf job, Reporter reporter) throws IOException {
    reporter.setStatus(split.toString());
    this.rrClass = ShapeRecordReader.class;
    return super.getRecordReader(split, job, reporter);
  }
}
最终产生了ShapeRecordReader类。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值