Google Cloud数据流:从具有动态文件名的文件中读取

时间:2017-09-21 13:48:44

标签: google-cloud-platform google-cloud-storage google-cloud-dataflow google-cloud-pubsub apache-beam

我正在尝试在Google Cloud Dataflow上构建一个管道,该管道将执行以下操作:

  • 收听Pubsub订阅上的活动
  • 从事件文本中提取文件名
  • 阅读文件(来自Google云端存储分区)
  • 将记录存储在BigQuery中

以下是代码:

Pipeline pipeline = //create pipeline
pipeline.apply("read events", PubsubIO.readStrings().fromSubscription("sub"))
        .apply("Deserialise events", //Code that produces ParDo.SingleOutput<String, KV<String, byte[]>>)
        .apply(TextIO.read().from(""))???

我正在努力进行第3步,不太确定如何访问第二步的输出并在第3步中使用它。我尝试编写产生以下内容的代码:

private ParDo.SingleOutput<KV<String, byte[]>, TextIO.Read> readFile(){
    //A class that extends DoFn<KV<String, byte[]>, TextIO.Read> and has TextIO.read wrapped into processElement method
}

但是,我无法在后续步骤中阅读文件内容。

任何人都可以请我知道在第3步和第4步中需要写什么才能让我逐行使用文件并将输出存储到BigQuery(或者只记录它)。

2 个答案:

答案 0 :(得分:3)

表达阅读的自然方式是使用TextIO.readAll()方法,该方法从文件名的输入PCollection读取文本文件。此方法已在Beam代码库中引入,但目前尚未发布。它将包含在Beam 2.2.0版本和相应的Dataflow 2.2.0版本中。

答案 1 :(得分:-1)

您可以使用SerializableFunction完成此操作。

你可以做到

pipeline.apply(TextIO.read().from(new FileNameFn()));

public class FileNameFn implements SerializableFunction<inputFileNameString, outputQualifiedFileNameStringWithBucket>

显然,您可以通过构造函数参数创建此类实例时静态传递存储桶名称和其他参数。

希望这会有所帮助。