0

我发现 RawLocalFileSystem 的输入流中的 getPos 可以在其底层流关闭时抛出空指针异常。

我在玩自定义记录阅读器时发现了这一点。

为了修补它,我只需检查对“stream.available()”的调用是否引发异常,如果是,我在 getPos() 函数中返回 0。

现有的 getPos() 实现在这里找到:

https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.20/src/examples/org/apache/hadoop/examples/MultiFileWordCount.java

RecordReader 中 getPos() 的正确行为应该是什么?

4

1 回答 1

0

RecordReader 中的“getPos”随着时间而改变。

在旧的 mapred RecordReader 实现中,它用于计算读取的字节数。

  /** 
   * Returns the current position in the input.
   * 
   * @return the current position in the input.
   * @throws IOException
   */
  long getPos() throws IOException;

在较新的 mapreduce RecordReader 实现中,此信息不是由 RR 类提供的,而是 FSInputStream 实现的一部分:

class LocalFSFileInputStream extends FSInputStream implements HasFileDescriptor {
private FileInputStream fis;
private long position;

public LocalFSFileInputStream(Path f) throws IOException {
  this.fis = new TrackingFileInputStream(pathToFile(f));
}

@Override
public void seek(long pos) throws IOException {
  fis.getChannel().position(pos);
  this.position = pos;
}

@Override
public long getPos() throws IOException {
  return this.position;
}

因此,使用新的 mapreduce API,RecordReader 被抽象为不一定返回 getPos()。可能想要使用此底层实现的 RecordReader 的较新实现可以重写为直接使用 FSInputStream 对象,这些对象确实提供了一个 getPos()。

于 2013-12-20T16:14:31.447 回答