0

これでジョブ構成が正しく設定されていると思いますが、ジョブを実行する方法がわかりません。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());

}
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 に答える