提前致谢...
- 我想使用 JDBC API 连接/写入以编程方式使用云 SQL 运行的 Postgres SQL 实例。我用过以下罐子:
- Postgresql
- Postgres-socket-factory
- postgres-socket-factory-1.0.11-jar-with-dependencies.jar
我正在运行 Dataproc,它将尝试使用步骤 #1 进行连接,但我收到以下异常:
2019-04-01 11:05:03.998 IST
Something unusual has occurred to cause the driver to fail. Please report this exception.
at org.postgresql.Driver.connect(Driver.java:277)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at rdsConnector$.getConnection(rdsConnector.scala:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$4.run(ApplicationMaster.scala:721)
Caused by: java.lang.RuntimeException: Unable to retrieve information about Cloud SQL instance [projectID:us-east1:dB]
at com.google.cloud.sql.core.SslSocketFactory.obtainInstanceMetadata(SslSocketFactory.java:459)
at com.google.cloud.sql.core.SslSocketFactory.fetchInstanceSslInfo(SslSocketFactory.java:333)
at com.google.cloud.sql.core.SslSocketFactory.getInstanceSslInfo(SslSocketFactory.java:313)
at com.google.cloud.sql.core.SslSocketFactory.createAndConfigureSocket(SslSocketFactory.java:194)
at com.google.cloud.sql.core.SslSocketFactory.create(SslSocketFactory.java:160)
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:96)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
... 11 more
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
2019-04-01 11:05:03.000 IST
User class threw exception: org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
我理解这是一个权限问题,但由于我正在使用 Dataproc 连接到 Postgres,缺少什么权限? 如果我从本地笔记本电脑运行,那么我必须GOOGLE_APPLICATION_CREDENTIALS
设置为 JSON 文件。但是在Dataproc的情况下,过程是什么?
JDBC 套接字工厂使用应用程序默认凭证策略来访问帐户凭证。
对于云数据处理器,在 [project-number]-compute@developer.gserviceaccount.com
中为您提供默认服务帐户。您授予此账户"Cloud SQL 客户端"IAM 角色,JDBC 套接字工厂将使用它进行身份验证,因此您将能够连接到您的应用程序。
只需为未来的访客留下进一步的注释:
如果您收到的错误如下:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Request had insufficient authentication scopes.",
"status" : "PERMISSION_DENIED"
}
则可能是由 VM 实例的范围(云 API 访问范围(问题引起的。解决此问题的一种方法是在创建 Dataproc 群集时添加sql-admin
范围。例如:
gcloud dataproc clusters create <your-cluster-name>
--region=<your-region>
--zone=<your-zone>
--scopes=https://www.googleapis.com/auth/sqlservice.admin
...
另一种方法是在 VM 实例中编辑。
有关详细信息,请参阅gcloud
文档