使用云服务的并行文件处理

Parallel file processing using cloud services

我有许多图像需要 运行 通过 java 程序来创建更多图像文件 -- 一个令人尴尬的并行案例。每个输入文件大约 500 MB,在处理过程中需要大约 4 GB 的内存,需要 30 秒到 2 分钟才能 运行。 java 程序是多线程的,但与使用更多线程相比,对输入文件进行并行处理可以获得更多收益。我需要每天多次启动流程(我不想手动打开 on/off 集群,也不想全天候 24/7 付费)。

我有点迷失在各种云选项中:

任何人都可以建议对此有什么合适的解决方案吗?

您应该能够使用 Dataflow 轻松完成此操作。管道可能类似于(假设您的文件位于 Google Cloud Storage,GCS):

class ImageProcessor {
    public static void process(GcsPath path) {
        // Open the image, do the processing you want, write
        // the output to where you want.
        // You can use GcsUtil.open() and GcsUtil.create() for
        // reading and writing paths on GCS.
    }
}

// This will work fine until a few tens of thousands of files.
// If you have more, let me know.
List<GcsPath> filesToProcess = GcsUtil.expand(GcsPath.fromUri("..."));
p.apply(Create.of(filesToProcess))
 .apply(MapElements.via(ImageProcessor::process)
                   .withOutputType(new TypeDescriptor<Void>() {}));
p.run();

这是 Dataflow 被用作令人尴尬的并行编排框架而不是数据处理框架的常见案例之一,但它应该可以工作。

您需要 Dataflow SDK 1.2.0 才能使用 MapElements 转换(对 Java 8 lambda 的支持是 1.2.0 中的新增功能)。