2017-03-14 30 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