2017-06-12 50 views
1

我在Google Cloud Storage上有Google Cloud Datastore備份,我想將其自動導入到BigQuery中。通過BigQuery創建數據存儲備份表Java API a

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將此表創建爲外部表?

回答

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"); 
      } 
     } 
    }