从 Dataflow 作业创建云 sql 表

Creating cloud sql tables from Dataflow job

有一个管道可以将 table 数据从 BigQuery 复制到 CloudSql。

云 SQL table 创建发生在数据流之外。

现在我们需要在 Dataflow 中创建 table。

我要在 GCS 存储桶中创建 table 作为 .sql 文件。

下面是将 table 从 BQ 复制到 sql 的代码片段。

        p.apply(BigQueryIO.readTableRows()
                    .from(source_table)
                    .withTemplateCompatibility()
                    .withoutValidation())
           
            .apply(JdbcIO.<TableRow>write()
                    .withDataSourceConfiguration(
                            JdbcIO.DataSourceConfiguration.create(
                                    "org.postgresql.Driver",
                                    base_url
                            )
                    )
                    .withStatement("INSERT INTO " + target_table.split("\.")[1] + " VALUES " + insert_query)
                    .withPreparedStatementSetter(new StatementSetter(some_map)));
    p.run();

有什么方法可以使用 JDBCIO 执行 .sql 文件?

Apache Beam 中的

JdbcIO 只能用于读取和写入 JDBC 数据源。不幸的是,无法使用 JdbcIO 执行 .sql 文件。因为,JdbcIO 需要在写入数据之前创建数据库和 tables。这同样适用于 CREATE.

等 DDL 命令

自动创建 table 的解决方法是使用 Cloud Build with Dataflow. The end-to-end process could be created as a Cloud Build job that creates the Table by using the cloud client, and then the Dataflow job could be triggered. Please refer to this Medium article 示例 Cloud Build-Dataflow 设置。

如果此功能是您管道中的重要元素,请在 Beam 项目下 JIRA 向 Apache Beam 团队提出功能请求。为此,JIRA 需要登录。