通过BigQuery Java api创建数据存储备份表

时间:2017-06-12 11:09:07

标签: java google-cloud-datastore google-bigquery

我在Google云端存储上有Google Cloud Datastore备份,我想将其自动导入BigQuery。

TableDefinition tableDefinition = ExternalTableDefinition
        .newBuilder(tableUri, Schema.of(), FormatOptions.datastoreBackup())
        .setAutodetect(true)
        .build();
return bigQuery.create(TableInfo.of(TableId.of("ds", tableName), tableDefinition));

这会产生以下异常;

  

com.google.cloud.bigquery.BigQueryException:指定架构是   不允许使用STORAGE_FORMAT_DATASTORE_BACKUP

如果我将Schema.of()更改为null,则抛出一个空指针。并且所有工厂方法都具有需要方案的方法签名。如何通过Java API将此表创建为外部表?

1 个答案:

答案 0 :(得分:0)

试试这个

public class DatastoreBackupImport {

        private String datasetName = "...";
        private String uri = "gs://xxx/xxx/.xxx.backup_info";
        private String tableName = "xxx";

        private void importBackup(){
            BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
            TableId tableId = TableId.of(datasetName, tableName);
            TableDefinition tableDefinition = StandardTableDefinition.newBuilder().build();
            TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
            Table table = bigquery.create(tableInfo);
            Job job = table.load(FormatOptions.datastoreBackup(), uri);
    // Wait for the job to complete
            try {
                Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS),
                    WaitForOption.timeout(3, TimeUnit.MINUTES));
                if (completedJob != null && completedJob.getStatus().getError() == null) {
                    // Job completed successfully
                    System.out.println("Job completed successfully");
                } else {
                    // Handle error case
                    System.out.printf("There was an error");
                    System.out.println(completedJob.getStatus().getError().getMessage());
                }
            } catch (InterruptedException | TimeoutException e) {
                // Handle interrupted wait
                System.out.println("There was an interruption");
            }
        }
    }