title: Bigquery: Extract Job does not create file
body: <p>I am working on a Java application which uses Bigquery as the analytics engine. Was able to run query jobs (and get results) using the code on <a href="http://developers.google.com/bigquery/articles/gettingstartedwithjava#insertqueryjob" rel="nofollow noreferrer">Insert a Query Job</a>. Had to modify the code to use service account using <a href="https://stackoverflow.com/a/10526246/1557399">this comment</a> on stackoverflow.</p>
  <p>Now, need to run an extract job to export a table to a bucket on GoogleStorage. Based on <a href="http://developers.google.com/bigquery/docs/reference/v2/jobs#exporting" rel="nofollow noreferrer">Exporting a Table</a>, was able to modify the Java code to insert extract jobs (code below). When run, the extract job's status changes from PENDING to RUNNING to DONE. The problem is that no file is actually uploaded to the specified bucket.</p>
  <p>Info that might be helpful:</p>
  <ul>
  <li>The <code>createAuthorizedClient</code> function returns a Bigquery instance and works for query jobs, so probably no issues with the service account, private key etc.</li>
  <li>Also tried creating and running the insert job manually on <a href="https://developers.google.com/apis-explorer/#s/bigquery/v2/bigquery.jobs.insert" rel="nofollow noreferrer">google's api-explorer</a> and the file is successfully created in the bucket. Using the same values for project, dataset, table and destination uri as in code so these should be correct.</li>
  </ul>
  <p>Here is the code (pasting the complete file in case somebody else finds this useful):</p>
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.security.GeneralSecurityException;
  import java.util.Arrays;
  import java.util.List;
  
  import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  import com.google.api.client.http.HttpTransport;
  import com.google.api.client.http.javanet.NetHttpTransport;
  import com.google.api.client.json.JsonFactory;
  import com.google.api.client.json.jackson.JacksonFactory;
  import com.google.api.services.bigquery.Bigquery;
  import com.google.api.services.bigquery.Bigquery.Jobs.Insert;
  import com.google.api.services.bigquery.BigqueryScopes;
  import com.google.api.services.bigquery.model.Job;
  import com.google.api.services.bigquery.model.JobConfiguration;
  import com.google.api.services.bigquery.model.JobConfigurationExtract;
  import com.google.api.services.bigquery.model.JobReference;
  import com.google.api.services.bigquery.model.TableReference;
  
  public class BigQueryJavaGettingStarted {
      
      private static final String PROJECT_ID = &quot;123456789012&quot;;
      private static final String DATASET_ID = &quot;MY_DATASET_NAME&quot;;
      private static final String TABLE_TO_EXPORT = &quot;MY_TABLE_NAME&quot;;
      private static final String SERVICE_ACCOUNT_ID = &quot;123456789012-...@developer.gserviceaccount.com&quot;;
      private static final File PRIVATE_KEY_FILE = new File(&quot;/path/to/privatekey.p12&quot;);
      private static final String DESTINATION_URI = &quot;gs://mybucket/file.csv&quot;;
  
      private static final List&lt;String&gt; SCOPES =  Arrays.asList(BigqueryScopes.BIGQUERY);
      private static final HttpTransport TRANSPORT = new NetHttpTransport();
      private static final JsonFactory JSON_FACTORY = new JacksonFactory();
      
      public static void main (String[] args) {
          try {
              executeExtractJob();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static final void executeExtractJob() throws IOException, InterruptedException, GeneralSecurityException {
          Bigquery bigquery = createAuthorizedClient();
          
          //Create a new Extract job
          Job job = new Job();
          JobConfiguration config = new JobConfiguration();
          JobConfigurationExtract extractConfig = new JobConfigurationExtract();
          TableReference sourceTable = new TableReference();
  
          sourceTable.setProjectId(PROJECT_ID).setDatasetId(DATASET_ID).setTableId(TABLE_TO_EXPORT);
          extractConfig.setSourceTable(sourceTable);
          extractConfig.setDestinationUri(DESTINATION_URI);
          config.setExtract(extractConfig);
          job.setConfiguration(config);
  
          //Insert/Execute the created extract job
          Insert insert = bigquery.jobs().insert(PROJECT_ID, job);
          insert.setProjectId(PROJECT_ID);
          JobReference jobId = insert.execute().getJobReference();
          
          //Now check to see if the job has successfuly completed (Optional for extract jobs?)
          long startTime = System.currentTimeMillis();
          long elapsedTime;
          while (true) {
              Job pollJob = bigquery.jobs().get(PROJECT_ID, jobId.getJobId()).execute();
              elapsedTime = System.currentTimeMillis() - startTime;
              System.out.format(&quot;Job status (%dms) %s: %s\n&quot;, elapsedTime, jobId.getJobId(), pollJob.getStatus().getState());
              if (pollJob.getStatus().getState().equals(&quot;DONE&quot;)) {
                  break;
              }
              //Wait a second before rechecking job status
              Thread.sleep(1000);
          }
          
      }
  
      private static Bigquery createAuthorizedClient() throws GeneralSecurityException, IOException {
          GoogleCredential credential = new GoogleCredential.Builder()
              .setTransport(TRANSPORT)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountScopes(SCOPES)
              .setServiceAccountId(SERVICE_ACCOUNT_ID)
              .setServiceAccountPrivateKeyFromP12File(PRIVATE_KEY_FILE)
              .build();
          
          return Bigquery.builder(TRANSPORT, JSON_FACTORY)
              .setApplicationName(&quot;My Reports&quot;)
              .setHttpRequestInitializer(credential)
              .build();
      }
  }
  </code></pre>
  <p>Here is the output:</p>
  <pre><code>Job status (337ms) job_dc08f7327e3d48cc9b5ba708efe5b6b5: PENDING
  ...
  Job status (9186ms) job_dc08f7327e3d48cc9b5ba708efe5b6b5: PENDING
  Job status (10798ms) job_dc08f7327e3d48cc9b5ba708efe5b6b5: RUNNING
  ...
  Job status (53952ms) job_dc08f7327e3d48cc9b5ba708efe5b6b5: RUNNING
  Job status (55531ms) job_dc08f7327e3d48cc9b5ba708efe5b6b5: DONE
  </code></pre>
  <p>It is a small table (about 4MB) so the job taking about a minute seems ok. Have no idea why no file is created in the bucket OR how to go about debugging this. Any help would be appreciated.</p>
  <p>As Craig pointed out, printed the status.errorResult() and status.errors() values.</p>
  <ul>
  <li>getErrorResults(): {&quot;message&quot;:&quot;Backend error. Job aborted.&quot;,&quot;reason&quot;:&quot;internalError&quot;}</li>
  <li>getErrors(): null</li>
  </ul>