小编Mic*_*ael的帖子

Google Dataflow (Apache beam) JdbcIO 批量插入 mysql 数据库

我正在使用 Dataflow SDK 2.X Java API(Apache Beam SDK)将数据写入 mysql。我创建了基于Apache Beam SDK 文档的管道,以使用数据流将数据写入 mysql。它一次插入单行,因为我需要实现批量插入。我在官方文档中找不到任何启用批量插入模式的选项。

想知道是否可以在数据流管道中设置批量插入模式?如果是,请让我知道我需要在下面的代码中更改什么。

 .apply(JdbcIO.<KV<Integer, String>>write()
      .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create(
            "com.mysql.jdbc.Driver", "jdbc:mysql://hostname:3306/mydb")
          .withUsername("username")
          .withPassword("password"))
      .withStatement("insert into Person values(?, ?)")
      .withPreparedStatementSetter(new JdbcIO.PreparedStatementSetter<KV<Integer, String>>() {
        public void setParameters(KV<Integer, String> element, PreparedStatement query) {
          query.setInt(1, kv.getKey());
          query.setString(2, kv.getValue());
        }
      })
Run Code Online (Sandbox Code Playgroud)

mysql google-cloud-dataflow apache-beam apache-beam-io

5
推荐指数
1
解决办法
5433
查看次数