【发布时间】:2020-11-03 04:12:19
【问题描述】:
有没有办法通过从 cloud-sql 发出特定查询来将多个 SQL 表导出为 csv。
以下是我目前拥有的代码。当我背靠背调用多个表的 exportTables 时,我看到 409 错误。这可能是因为 cloud-sql 实例正忙于导出,并且不允许后续导出请求。
我怎样才能让它工作?这里的理想解决方案是什么。
private void exportTables(String table_name, String query)
throws IOException, InterruptedException {
HttpClient httpclient = new HttpClient();
PostMethod httppost =
new PostMethod(
"https://www.googleapis.com/sql/v1beta4/projects/"
+ "abc"
+ "/instances/"
+ "zxy"
+ "/export");
String destination_bucket =
String.join(
"/",
"gs://" + "test",
table_name,
DateTimeUtil.getCurrentDate() + ".csv");
GoogleCredentials credentials =
GoogleCredentials.getApplicationDefault().createScoped(SQLAdminScopes.all());
AccessToken access_token = credentials.refreshAccessToken();
access_token.getTokenValue();
httppost.addRequestHeader("Content-Type", "application/json");
httppost.addRequestHeader("Authorization", "Bearer " + access_token.getTokenValue());
String request =
"{"
+ " \"exportContext\": {"
+ " \"fileType\": \"CSV\","
+ " \"uri\":\""
+ destination_bucket
+ "\","
+ " \"databases\": [\""
+ "xyz"
+ "\"],"
+ " \"csvExportOptions\": {"
+ " \"selectQuery\": \""
+ query
+ "\""
+ " }\n"
+ " }"
+ "}";
httppost.setRequestEntity(new StringRequestEntity(request, "application/json", "UTF-8"));
httpclient.executeMethod(httppost);
if (httppost.getStatusCode() > 200) {
String response = new String(httppost.getResponseBody(), StandardCharsets.UTF_8);
if (httppost.getStatusCode() != 409) {
throw new RuntimeException(
"Exception occurred while exporting the table: " + table_name + " Error " + response);
} else {
throw new IOException("SQL instance seems to be busy at the moment. Please retry");
}
}
httppost.releaseConnection();
logger.info("Finished exporting table {} to {}", table_name, destination_bucket);
}
【问题讨论】:
-
您想按顺序运行多个导出,对吗?当你同时有 2 个时,你就有冲突(409),对吗?您是否尝试将
offload: true值添加到请求正文中的exportContext对象?能解决问题吗? -
@guillaumeblaquiere 我没试过。让我试试添加这个。
-
@guillaumeblaquiere
offload:true对我不起作用 -
你的意思还是一样的409错误吗?而且您想按顺序运行多个导出,对吗?
-
@guillaumeblaquiere。是的,我遇到了同样的错误。是的,我想按顺序运行多个导出。
标签: google-cloud-platform google-cloud-sql