Google Cloud Dataflow中FileBasedSource使用示例

时间:2016-01-07 03:44:53

标签: google-cloud-dataflow

有人可以发布一个子类化FileBasedSource的简单示例吗?我是Google Dataflow的新手,对Java非常缺乏经验。我的目标是在包含行号作为键的同时读取文件,或者根据行号跳过行。

1 个答案:

答案 0 :(得分:1)

XMLSource的实现是理解FileBasedSource如何工作的良好起点。你的读者可能想要这样的东西(readNextLine()读到行的末尾并更新偏移量):

protected void startReading(ReadableByteChannel channel) throws IOException {
  if (getCurrentSource().getMode() == FileBasedSource.Mode.SINGLE_FILE_OR_SUBRANGE) {
    // If we are not at the beginning of a line, we should ignore the current line.
    if (getCurrentSource().getStartOffset() > 0) {
      SeekableByteChannel seekChannel = (SeekableByteChannel) channel;
      // Start from one character back and read till we find a new line.
      seekChannel.position(seekChannel.position() - 1);
      nextOffset = seekChannel.position() + readNextLine(new ByteArrayOutputStream());
    }
  }
}

我已经用完整的LineIO示例创建了一个要点,它可能比XMLSource更简单。