使用Google App Scripts将Google桌面中的表格加载到BigQuery

时间:2013-07-03 00:51:19

标签: google-apps-script google-bigquery

我认为这是正确设置作业配置,但我无法弄清楚如何让作业运行。有没有人使用Apps Script将表加载到BigQuery?

function testLoad(){

  var fields = [
    {'name': 'FirstName', 'type':'STRING'},
    {'name': 'LastName', 'type':'STRING'}
  ];

  var schema = BigQuery.newTableSchema()
    schema.setFields(fields)

  var tableReference = BigQuery.newTableReference()
    tableReference.setProjectId(PROJECT_ID);
    tableReference.setDatasetId('Test_Dataset');
    tableReference.setTableId('TestTable1');

  var load = BigQuery.newJobConfigurationLoad();
    load.setDestinationTable(tableReference);
    load.setSkipLeadingRows(1);
    load.setSourceUris([SOURCE]);
    load.setSourceFormat('CSV');
    load.setSchema(schema)

  var configuration = BigQuery.newJobConfiguration();
    configuration.setLoad(load);

  var newJob = BigQuery.newJob();
    newJob.setConfiguration(configuration);

  var insert = BigQuery.Jobs.insert(newJob)

  Logger.log(insert.getId());

}

1 个答案:

答案 0 :(得分:1)

最后回到了这个问题。缺少的部分需要将项目ID包含在插入作业以及表引用中。

我还包括一些用于轮询作业状态并在作业完成时返回。

function testLoad(){

  Logger.log(exampleLoad_());
}


function exampleLoad_(){

  try{

    var fields = [
      {'name': 'FirstName', 'type':'STRING'},
      {'name': 'LastName', 'type':'STRING'}
    ];

    var schema = BigQuery.newTableSchema();
    schema.setFields(fields);

    var tableReference = BigQuery.newTableReference();
    tableReference.setProjectId(PROJECT_ID);
    tableReference.setDatasetId('Test_Dataset');
    tableReference.setTableId('TestTable1');

    var load = BigQuery.newJobConfigurationLoad();
    load.setDestinationTable(tableReference);
    load.setSkipLeadingRows('1');
    load.setSourceUris([SOURCE]);
    load.setSourceFormat('CSV');
    load.setSchema(schema);
    load.setAllowJaggedRows(false);

    var configuration = BigQuery.newJobConfiguration();
    configuration.setLoad(load);

    var newJob = BigQuery.newJob();
    newJob.setConfiguration(configuration);

    var job = BigQuery.Jobs.insert(newJob, {projectId:PROJECT_ID});

    var jobId = job.getJobReference().getJobId();

    var status = job.getStatus();


    while (status.getState() != 'DONE'){

      if(status.getState() == 'PENDING'){
        Utilities.sleep(100);

      }

      if (status.getErrorResult() == true){     
        return status.getErrors();

      } 
      status = BigQuery.Jobs.get(PROJECT_ID, jobId).getStatus();
    }

  }catch(err){ 
    return err;

  }

  return status.getState();
}