Java Google Cloud Postgresql 使用 SSL 的连接字符串 - 数据流

Java connection String for Google Cloud Postgresql with SSL - Dataflow

需要使用数据流作业(Java 代码)将数据写入 Google Cloud PostgreSQL。此 PostgreSQL 实例启用了 SSL。所以想知道启用了 SSL 的这个 PostgreSQL 数据库的连接字符串(客户端认证、密钥和服务器认证)。在没有 SSL 的情况下,以下连接字符串在数据流作业中工作正常。但是想知道带 SSL 的连接字符串。

JdbcIO.<KV<Integer,Integer>>write()
.withDataSourceConfiguration( DataSourceConfiguration.create("org.postgresql.Driver","jdbc:postgresql://<PrivateIP>:5432/db")
               .withUsername("***")
               .withPassword("password"))

您可以使用以下 driver properties 配置与 PostgreSQL 的连接:sslcertsslkeysslrootcert 以指向不同的证书。

您也可以使用 Cloud SQL JDBC Socket Factory,它会创建临时证书并自动使用它们。

下面的代码适合我。

String conUrl="jdbc:postgresql://google/<dbName>?cloudSqlInstance=<InstanceName>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<UserName>&password=<PWD>";

p.apply(JdbcIO.<KV<Integer,Integer>>write()
    .withDataSourceConfiguration( DataSourceConfiguration.create("org.postgresql.Driver",conUrl)
            .withUsername(<UserName>)
            .withPassword(<PWD>)
            )
    .withStatement("insert into public.testTable(id,order_number) values(?, ?)")
    .withPreparedStatementSetter(new JdbcIO.PreparedStatementSetter<KV<Integer,Integer>>() {
        public void setParameters(KV<Integer,Integer> element, PreparedStatement query)
                throws SQLException {
            query.setInt(1, element.getKey());
            query.setInt(2, element.getValue());
        }
    })
);

这适用于我与 jdbcIO 建立连接

String conUrl="jdbc:postgresql://google/dbName?cloudSqlInstance=projectId:us-central1:databaseId&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=postgres&password=*****";