【问题标题】:Can we rename cloud storage files using dataflow我们可以使用数据流重命名云存储文件吗
【发布时间】:2017-07-13 06:36:16
【问题描述】:

我正在尝试使用数据流程序重命名云存储文件。有可能这样做吗?如果是,那么如何。

【问题讨论】:

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


    【解决方案1】:

    虽然 Apache Beam SDK 不包含用于重命名文件的现成 PTransform,但没有什么可以阻止您自己进行 - 管道可以包含 DoFn 中的任意代码,您可以使用标准Google Cloud Storage Java APIs,或者更方便的是,使用 Beam 的 FileSystems API。例如:

    class RenameFn extends DoFn<KV<String, String>, Void> {
      @ProcessElement
      public void process(ProcessContext c) {
        ResourceId src = FileSystems.matchNewResource(c.element().getKey());
        ResourceId dest = FileSystems.matchNewResource(c.element().getValue());
        FileSystems.rename(Arrays.asList(src), Arrays.asList(dest));
      }
    }
    

    【讨论】:

    • 感谢您的回答,实际上我对数据流非常陌生,所以您能否解释一下关键和价值应该是什么。是文件名吗?
    • 键是源文件名,值是目标文件名。请记住,我没有测试过这段代码,我只是用它来说明如何使用 FileSystems.rename() API 以及如何从 String 转换为 FileSystems API 采用的 ResourceId。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多