Febian Shah Febian Shah - 1 month ago 11
Java Question

BigQuery job status is always "PENDING"

I'm trying to run the following code, but the status variable is always "PENDING". Could you please tell me what am I doing wrong?

Job execute = bigquery.jobs().insert(PROJECT_ID, runJob).execute();

String status;
while(status.equalsIgnoreCase("PENDING")) {
status = execute.getStatus().getState();
System.out.println("Status: " + status);
Thread.wait(1000);
}

Answer

Your code isn't making a request to BigQuery to get the updated state, it's just checking the state of the Job returned by the insert call.

Instead, you should poll for the state of the job by issuing a jobs.get request, and check that state, e.g.:

Job job = bigquery.jobs().insert(PROJECT_ID, runJob).execute();
String status = job.getStatus().getState();
while(!status.equalsIgnoreCase("DONE")) {
  status = bigquery.jobs().get(PROJECT_ID, job.getId()).execute().getStatus().getState();
  System.out.println("Status: " + status);
  Thread.wait(1000);
}

*Edited based on Jordan Tigani's comment.