使用 Beam Java SDK 在 Google Dataflow 上安装 apt-get 依赖项

Install apt-get dependencies on Google Dataflow with Beam Java SDK

我们目前正在尝试在 Google Cloud Dataflow 上的 Java 作业中获取 OpenCV 运行。不幸的是,我们无法将 Dataflow 使用的 Docker 容器替换为安装了 OpenCV 的容器。 () 如果我们使用 Python SDK,则有一个选项可以指定可用于调用 apt-getsetup.py 文件。使用 Java SDK 创建的工作是否有类似的东西?

感谢您的帮助!

我想出了一个解决方案,但可能还有更优雅的方法。

@Setup
public void setupDoFn() {
    ProcessBuilder pb = new ProcessBuilder("apt-get", "install", "-y", "libopencv-dev");
    try {
        Process p = pb.start();
        String line;
        BufferedReader input =
                new BufferedReader
                        (new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            logger.debug("Apt-get: " + line);
        }
        input.close();
        // Initialize the OpenCV Libarary
        nu.pattern.OpenCV.loadLibrary();
    } catch (IOException e) {
        e.printStackTrace();
        // If we could not install OpenCV, we have to terminate the stream
        System.exit(-1);
    }

}

Cloud Dataflow 现在支持自定义容器,可以安装其他库(例如 OpenCV)。 See this page for more information.