简体   繁体   中英

How to know when a BigQuery job insert is complete

I'm using BigQuery.Jobs.insert to upload a csv blob to a BigQuery table (from a Google Sheets spreadsheet if that matters), but I need to know when the upload is complete so that I can run a query to process it. Here's the code I have...

  csv_blob = Utilities.newBlob(csv,"application/octet-stream");
  
  //Copy the csv to BigQuery
  job = BigQuery.Jobs.insert(job, projectId, csv_blob );

  var jobId=job.jobReference.jobId;

  //Give it a half second to complete
  var sleepTimeMs = 500;
  Utilities.sleep(sleepTimeMs);

  //see if it needs longer
  var jobstatus = job.status;
  while(jobstatus.state!="COMPLETE") {
    Utilities.sleep(sleepTimeMs);
    sleepTimeMs *= 2;
    jobstatus = job.status;
  }

The upload is working, but the job.status.state never changes from "RUNNING", no matter how long I wait. Even after I manually verify the uploaded table on the server, the job.status.state is still "RUNNING". What is the right way to know when the job is complete? Thanks

You need to fetch the current status of the job with BigQuery.Jobs.Get ( REST reference ), it appears that you're just rechecking the response you received from the initial job insertion.

An quick and dirty example of invoking this:

function myFunction() {
  projectID = "my-project-id"
  jobID = "some-job-id"
  location = "us-central1"

  job = BigQuery.Jobs.get(projectID, jobID, {location: location});
  console.log("result state: " + job.status.state + " end time:" + job.statistics.endTime);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM