2017-03-14 33 views
0

我有兩個的BigQuery連接:的BigQuery數據遷移上的Java

  1. 客戶BiqQuery連接(使用只讀權限)
  2. 應用BigQuery的連接。

兩個連接(BiqQuery服務)都有很多數據集。 因此,我創建了兩個擁有不同憑據(客戶端和應用程序)的bean(存儲庫)。

我需要客戶BigQuery服務執行查詢,得到的查詢結果(我在得到它GetQueryResultsResponse),並將其轉移到應用BigQuery服務如新表。

我試圖從GetQueryResultsResponse使用表方案應用BigQuery服務創建新表,我已經執行的查詢後,我加入新行到表中。但在這種情況下,我有問題 - 在BigQuery中插入不會立即完成。數據已添加一段時間,我需要在插入後立即使用數據。

問題: 也許有沒有辦法將這些數據保存到文件中,並在將來使用它? 有沒有辦法將查詢結果中的數據從一個Google BigQuery服務轉移到另一個服務?

我的代碼:

//get data from client service 
GetQueryResultsResponse resultsResponse = executeQuery(query); 
TableSchema schema = resultsResponse.getSchema(); 
Table table = new Table(); 
table.setSchema(schema); 

TableReference tableRef = new TableReference(); 
tableRef.setDatasetId(applicationDataSetId); 
tableRef.setProjectId(projectId); 
tableRef.setTableId(tableId); 
//create new table in Application BigQuery environment 
try { 
Bigquery.Tables.Insert insert = getApplicationBigQueryService().tables().insert(projectId, applicationDataSetId, table); 
insert.execute(); 
} catch (IOException e) { 
} 

TableDataInsertAllRequest content = new TableDataInsertAllRequest(); 
List<TableDataInsertAllRequest.Rows> bigQueryRows = new ArrayList<>(); 

List<Map<String, Object>> rows2 = new ArrayList<>(); 

Map<String, Object> tableCell = new TableCell(); 
tableCell.put("customer_master_id", 1); 
tableCell.put("formulary_name", "FOR"); 
tableCell.put("quarter", "2014"); 
tableCell.put("lives", 1234213); 
tableCell.put("source", "BOT"); 


TableDataInsertAllRequest.Rows row = new TableDataInsertAllRequest.Rows(); 
row.setJson(tableCell); 
bigQueryRows.add(row); 

TableDataInsertAllRequest.Rows insertRows = new TableDataInsertAllRequest.Rows(); 

insertRows.setJson(tableCell); 
bigQueryRows.add(insertRows); 

content.setRows(bigQueryRows); 
// send insert request to BigQuery 
Bigquery.Tabledata.InsertAll request = getBentoBigQueryService().tabledata().insertAll(projectId, bentoDataSetId,tableId, content); 

//insert data to table 
TableDataInsertAllResponse response = request.execute(); 
log.info(response.toString()); 
if (response.containsKey(INSERT_ERRORS)) { 
throw new JobException(String.format(ERROR_SYNC_MSG, response)); 
} 
} catch (IOException ex) { 
log.warn(ERROR_SYNC_MSG, ex); 
throw new JobException(String.format(ERROR_SYNC_MSG, ex.getMessage())); 
} 

感謝。

回答

0

這似乎是在BigQuery內移動數據的一種奇怪方式。如果您查看可以爲查詢設置的許多選項,則可以將結果顯式化爲已命名的目標表,並且設置適當的創建/寫入處置將決定數據是否在目標中添加或替換。

假設您對源(讀取)和目標(寫入)擁有適當的權限,則可以在數據集和項目之間完成此操作。

如果您沒有使用查詢操作模式或結果並簡單地複製數據,那麼您可能還需要查看錶複製作業。

+0

我知道如何做到這一點,讓我們說一個創建連接的BigQuery憑證。主要問題是,這些「憑證」中的任何一個都沒有其他服務的許可。我無法使用客戶憑證執行查詢並將結果插入到應用程序BigQuery中,因爲客戶服務沒有權限執行此操作。它看起來像:Bigquery.Jobs.Query bigQuery = getBigQueryService()。jobs()。query(projectId,queryRequest); getBigQueryService() - 客戶端大查詢連接對象 getApplicationBigQueryService() - 應用程序大查詢連接對象。可以舉個例子嗎? – yakutcv