基于Java的BigQuery数据迁移

基于Java的BigQuery数据迁移,java,google-bigquery,Java,Google Bigquery,我有两个BigQuery连接: 客户端BiqQuery连接(具有只读权限) 应用程序BigQuery连接 这两个连接(BiqQuery服务)都有大量的数据集。 因此,我创建了两个具有不同凭据(客户端和应用程序)的bean(存储库) 我需要在客户端BigQuery服务中执行查询,从查询中获取结果(我在GetQueryResultsResponse中获得),并将其传输到应用程序BigQuery服务,就像新表一样 我尝试在应用程序BigQuery服务中使用GetQueryResultsResponse

我有两个BigQuery连接:

  • 客户端BiqQuery连接(具有只读权限)
  • 应用程序BigQuery连接
  • 这两个连接(BiqQuery服务)都有大量的数据集。 因此,我创建了两个具有不同凭据(客户端和应用程序)的bean(存储库)

    我需要在客户端BigQuery服务中执行查询,从查询中获取结果(我在GetQueryResultsResponse中获得),并将其传输到应用程序BigQuery服务,就像新表一样

    我尝试在应用程序BigQuery服务中使用GetQueryResultsResponse中的表方案创建新表,执行查询后,我向表中添加了新行。但在本例中,我遇到了一个问题—在BigQuery中插入并不能立即完成。数据添加了一段时间,插入后我需要数据

    问题: 也许有办法将这些数据保存到一个文件中,并在将来使用它吗? 有没有一种方法可以将查询结果中的数据从一个Google BigQuery服务传输到另一个

    我的代码:

    //从客户端服务获取数据
    GetQueryResultsResponse resultsResponse=executeQuery(查询);
    TableSchema schema=resultsResponse.getSchema();
    Table Table=新表();
    表1.setSchema(模式);
    TableReference tableRef=新的TableReference();
    tableRef.setDatasetId(applicationDataSetId);
    表REF.setProjectId(projectId);
    tableRef.setTableId(tableId);
    //在应用程序BigQuery环境中创建新表
    试一试{
    Bigquery.Tables.Insert Insert=getApplicationBigQueryService().Tables().Insert(projectId,applicationDataSetId,table);
    insert.execute();
    }捕获(IOE异常){
    }
    TableDataInsertAllRequest内容=新建TableDataInsertAllRequest();
    List bigQueryRows=newArrayList();
    列表行2=新的ArrayList();
    Map tableCell=新tableCell();
    tableCell.put(“客户\主\ id”,1);
    tableCell.put(“公式集名称”,“FOR”);
    tableCell.put(“季度”、“2014年”);
    tableCell.put(“生命”,1234213);
    tableCell.put(“源”、“BOT”);
    TableDataInsertAllRequest.Rows行=新建TableDataInsertAllRequest.Rows();
    row.setJson(tableCell);
    添加(行);
    TableDataInsertAllRequest.Rows insertRows=新建TableDataInsertAllRequest.Rows();
    setJson(tableCell);
    添加(insertRows);
    content.setRows(bigQueryRows);
    //向BigQuery发送插入请求
    Bigquery.Tabledata.InsertAll请求=getBentoBigQueryService().Tabledata().InsertAll(projectId、bentoDataSetId、tableId、content);
    //向表中插入数据
    TableDataInsertAllResponse=request.execute();
    log.info(response.toString());
    if(response.containsKey(插入错误)){
    抛出新的JobException(String.format(ERROR_SYNC_MSG,response));
    }
    }捕获(IOEX异常){
    日志警告(错误同步消息,例如);
    抛出新的JobException(String.format(ERROR_SYNC_MSG,例如getMessage());
    }
    

    谢谢。

    这似乎是在BigQuery中移动数据的一种奇怪方式。如果查看可以为查询设置的许多选项,可以显式地将结果物化到指定的目标表中,并且设置适当的创建/写入处理将决定是否在目标表中追加或替换数据

    假设您对源(读取)和目标(写入)具有适当的权限,这可以在数据集和项目之间完成


    如果您不使用查询操作模式或结果,而只是复制数据,那么您可能还需要查看表复制作业。

    我知道使用创建连接的一个BigQuery凭据执行此操作的几种方法。主要问题是,这些“凭据”中的任何一个都没有访问其他服务的权限。我无法使用客户凭据执行查询并将结果插入应用程序BigQuery,因为客户服务无权执行此操作。它看起来像:Bigquery.Jobs.Query Bigquery=getBigQueryService().Jobs().Query(projectId,queryRequest);getBigQueryService()-客户端大查询连接对象getApplicationBigQueryService()-应用程序大查询连接对象。你能举个例子吗?
    //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()));
    }