0

我相信这是正确设置作业配置,但我无法弄清楚如何让作业运行。有没有人成功使用 Apps 脚本将表加载到 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());

}
4

1 回答 1

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();
}
于 2013-08-07T01:24:52.597 回答