【问题标题】:Multiple cloud-sql table export as csv多个 cloud-sql 表导出为 csv
【发布时间】: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


【解决方案1】:

我没有建议直接在 Cloud SQL 上解决问题,但是借助新工具可以按顺序执行导出的解决方案:Workflow

  • 以 JSON 格式定义您想要的数据格式,以定义 ONE 导出。
  • 然后为您的工作流程提供一系列配置
  • 在此工作流程中,
    • 在配置数组上创建一个循环
    • 对 Cloud SQL 执行 API 调用以在每个配置上生成导出
    • 获取 API 调用的答案,你有 jobId
    • 睡一会儿
    • 检查导出是否结束(使用 jobId)。
      • 如果没有,请睡觉并再次检查
      • 如果是,则循环(并因此开始下一次导出)

它是无服务器的,免费层使这个用例免费。

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 2019-10-03
    • 2017-06-24
    • 2015-11-28
    • 2020-01-10
    • 2018-12-18
    • 2012-12-24
    • 2013-05-23
    相关资源
    最近更新 更多