java - 将 csv 数据从存储导入 Cloud SQL 不起作用 - 状态始终为 "pending"

标签 java mysql google-cloud-sql

我是 java 新手(不过我有 C# 经验)

遗憾的是,我继承了一个糟糕的项目(代码很糟糕),我需要完成的是将一些 csv 文件导入 Cloud SQL

所以有一个 WS 运行这个任务,显然开发人员遵循 this guide导入数据。但它不起作用。这是代码(基本部分,实际上它更长而且更难看)

    InstancesImportRequest requestBody = new InstancesImportRequest();
        ImportContext ic = new  ImportContext();
        ic.setKind("sql#importContext");
        ic.setFileType("csv");
        ic.setUri(bucketPath);
        ic.setDatabase(CLOUD_SQL_DATABASE);
        CsvImportOptions csv = new CsvImportOptions();
        csv.setTable(tablename);
        List<String> list = new ArrayList<String>();
        // here there is some code that populates the list with the columns
        csv.setColumns(list);
        ic.setCsvImportOptions(csv);
        requestBody.setImportContext(ic);
SQLAdmin sqlAdminService = createSqlAdminService();
     SQLAdmin.Instances.SQLAdminImport request = sqlAdminService.instances().sqladminImport(project, instance, requestBody);
    Operation response = request.execute();
    System.out.println("Executed : Going to sleep.>"+response.getStatus());
        int c = 1;
        while(!response.getStatus().equalsIgnoreCase("Done")){
            Thread.sleep(10000);
            System.out.println("sleeped enough >"+response.getStatus());
            c++;
            if(c==50){
                System.out.println("timeout?");
                break;
            }
        }
public static SQLAdmin createSqlAdminService() throws IOException, GeneralSecurityException {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        if (credential.createScopedRequired()) {
          credential =
              credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
        }

        return new SQLAdmin.Builder(httpTransport, jsonFactory, credential)
            .setApplicationName("Google-SQLAdminSample/0.1")
            .build();
      }

我不太确定应该如何处理响应,它似乎是一个异步请求。不管怎样,我总是会得到状态Pending;似乎还没有开始执行。

当然超时了。这里出了什么问题,为什么请求永远不会开始?除了我上面给出的链接之外,我在互联网上找不到任何有关使用此 java sdk 导入文件的实际示例

最佳答案

问题是响应对象是静态的,因此它总是返回“Pending”作为初始状态,因为它是对象中的字符串 - 它实际上并未被更新。

要获取实际状态,您必须使用 sdk 向 google 请求。我做了这样的事情(最好使用较小的 sleep 时间,并随着尝试的次数增多而使其增长)

SQLAdmin.Instances.SQLAdminImport request = sqlAdminService.instances().sqladminImport(CLOUD_PROJECT, CLOUD_SQL_INSTANCE, requestBody);
        // execution of our import request
        Operation response = request.execute();
        int tried = 0;
        Operation statusOperation;
        do {
            // sleep one minute
            Thread.sleep(60000);
            // here we are requesting the status of our operation. Name is actually the unique identifier
            Get requestStatus = sqlAdminService.operations().get(CLOUD_PROJECT, response.getName());
            statusOperation = requestStatus.execute();
            tried++;
            System.out.println("status is: " + statusOperation.getStatus());
        } while(!statusOperation.getStatus().equalsIgnoreCase("DONE") && tried < 10);
        if (!statusOperation.getStatus().equalsIgnoreCase("DONE")) {
            throw new Exception("import failed: Timeout");
        }

关于java - 将 csv 数据从存储导入 Cloud SQL 不起作用 - 状态始终为 "pending",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49434219/

相关文章:

java - 使用包含重复键或值的 Map 中的值获取键

mysql - 无法输入 Unicode 数据、MySql、Express、M*EAN

java - 有什么方法可以强制执行 java 枚举值的合规性吗?

java - 如何覆盖子类中列的特征?

mysql - 多主(n >=3 个节点)范式

php - Prestashop 插入类别会破坏 UI

mysql - 使用云 SQL 从 MySQL 5.5 迁移到 GAE : Microtime support?

postgresql - 如何从 Go 中的 Cloud Function 连接到 Cloud-SQL?

google-cloud-sql - 更改 TCP 端口 :3306 for Google Cloud SQL instance

java - 无法解析NotificationManagerCompat - Android Wear