Class Job (6.2.1)

Job objects are returned from various places in the BigQuery API:


They can be used to check the status of a running job or fetching the results of a previously-executed one.

Inheritance

ServiceObject <T> > Operation > Job

Package

@google-cloud/bigquery

Example

  const 
  
 { 
 BigQuery 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery 
' 
 ); 
 const 
  
 bigquery 
  
 = 
  
 new 
  
  BigQuery 
 
 (); 
 const 
  
 job 
  
 = 
  
 bigquery 
 . 
  job 
 
 ( 
 'job-id' 
 ); 
 //- 
 // All jobs are event emitters. The status of each job is polled 
 // continuously, starting only after you register a "complete" listener. 
 //- 
  job 
 
 . 
 on 
 ( 
 'complete' 
 , 
  
 ( 
 metadata 
 ) 
  
 = 
>  
 { 
  
 // The job is complete. 
 }); 
 //- 
 // Be sure to register an error handler as well to catch any issues which 
 // impeded the job. 
 //- 
  job 
 
 . 
 on 
 ( 
 'error' 
 , 
  
 ( 
 err 
 ) 
  
 = 
>  
 { 
  
 // An error occurred during the job. 
 }); 
 //- 
 // To force the Job object to stop polling for updates, simply remove any 
 // "complete" listeners you've registered. 
 // 
 // The easiest way to do this is with `removeAllListeners()`. 
 //- 
  job 
 
 . 
 removeAllListeners 
 (); 
 

Constructors

(constructor)(bigQuery, id, options)

  constructor 
 ( 
 bigQuery 
 : 
  
 BigQuery 
 , 
  
 id 
 : 
  
 string 
 , 
  
 options 
 ?: 
  
 JobOptions 
 ); 
 

Constructs a new instance of the Job class

Parameters
Name
Description
bigQuery
id
string
options

Properties

bigQuery

  bigQuery 
 : 
  
 BigQuery 
 ; 
 

location

  location 
 ?: 
  
 string 
 ; 
 

projectId

  projectId 
 ?: 
  
 string 
 ; 
 

Methods

cancel()

  cancel 
 () 
 : 
  
 Promise<CancelResponse> 
 ; 
 

Cancel a job. Use to see if the cancel completes successfully. See an example implementation below.

See Jobs: get API Documentation

Returns
Type
Description
Promise < CancelResponse >

{Promise

Example
  const 
  
 { 
 BigQuery 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery 
' 
 ); 
 const 
  
 bigquery 
  
 = 
  
 new 
  
  BigQuery 
 
 (); 
 const 
  
 job 
  
 = 
  
 bigquery 
 . 
  job 
 
 ( 
 'job-id' 
 ); 
  job 
 
 . 
  cancel 
 
 (( 
 err 
 , 
  
 apiResponse 
 ) 
  
 = 
> { 
  
 // Check to see if the job completes successfully. 
  
 job 
 . 
 on 
 ( 
 'error' 
 , 
  
 ( 
 err 
 ) 
  
 = 
>  
 {}); 
  
  job 
 
 . 
 on 
 ( 
 'complete' 
 , 
  
 ( 
 metadata 
 ) 
  
 = 
>  
 {}); 
 }); 
 //- 
 // If the callback is omitted, we'll return a Promise. 
 //- 
  job 
 
 . 
  cancel 
 
 (). 
 then 
 (( 
 data 
 ) 
  
 = 
>  
 { 
  
 const 
  
 apiResponse 
  
 = 
  
 data 
 [ 
 0 
 ]; 
 }); 
 

cancel(callback)

  cancel 
 ( 
 callback 
 : 
  
 CancelCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameter
Name
Description
callback
Returns
Type
Description
void

getQueryResults(options)

  getQueryResults 
 ( 
 options 
 ?: 
  
 QueryResultsOptions 
 ) 
 : 
  
 Promise<QueryRowsResponse> 
 ; 
 

Get the results of a job.

See Jobs: getQueryResults API Documentation

Parameter
Name
Description
options
QueryResultsOptions

Configuration object.

Returns
Type
Description
Promise < QueryRowsResponse >

{Promise

Example
  const 
  
 { 
 BigQuery 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery 
' 
 ); 
 const 
  
 bigquery 
  
 = 
  
 new 
  
  BigQuery 
 
 (); 
 const 
  
 job 
  
 = 
  
 bigquery 
 . 
  job 
 
 ( 
 'job-id' 
 ); 
 //- 
 // Get all of the results of a query. 
 //- 
  job 
 
 . 
  getQueryResults 
 
 (( 
 err 
 , 
  
 rows 
 ) 
  
 = 
>  
 { 
  
 if 
  
 ( 
 ! 
 err 
 ) 
  
 { 
  
 // rows is an array of results. 
  
 } 
 }); 
 //- 
 // Customize the results you want to fetch. 
 //- 
  job 
 
 . 
  getQueryResults 
 
 ({ 
  
 maxResults 
 : 
  
 100 
 }, 
  
 ( 
 err 
 , 
  
 rows 
 ) 
  
 = 
>  
 {}); 
 //- 
 // To control how many API requests are made and page through the results 
 // manually, set `autoPaginate` to `false`. 
 //- 
 function 
  
 manualPaginationCallback 
 ( 
 err 
 , 
  
 rows 
 , 
  
 nextQuery 
 , 
  
 apiResponse 
 ) 
  
 { 
  
 if 
  
 ( 
 nextQuery 
 ) 
  
 { 
  
 // More results exist. 
  
  job 
 
 . 
  getQueryResults 
 
 ( 
 nextQuery 
 , 
  
 manualPaginationCallback 
 ); 
  
 } 
 } 
  job 
 
 . 
  getQueryResults 
 
 ({ 
  
 autoPaginate 
 : 
  
 false 
 }, 
  
 manualPaginationCallback 
 ); 
 //- 
 // If the callback is omitted, we'll return a Promise. 
 //- 
  job 
 
 . 
  getQueryResults 
 
 (). 
 then 
 (( 
 data 
 ) 
  
 = 
>  
 { 
  
 const 
  
 rows 
  
 = 
  
 data 
 [ 
 0 
 ]; 
 }); 
 

getQueryResults(options, callback)

  getQueryResults 
 ( 
 options 
 : 
  
 QueryResultsOptions 
 , 
  
 callback 
 : 
  
 QueryRowsCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameters
Name
Description
Returns
Type
Description
void

getQueryResults(callback)

  getQueryResults 
 ( 
 callback 
 : 
  
 QueryRowsCallback 
 ) 
 : 
  
 void 
 ; 
 
Parameter
Name
Description
Returns
Type
Description
void

getQueryResultsAsStream_(options, callback)

  getQueryResultsAsStream_ 
 ( 
 options 
 : 
  
 QueryResultsOptions 
 , 
  
 callback 
 : 
  
 QueryRowsCallback 
 ) 
 : 
  
 void 
 ; 
 

This method will be called by getQueryResultsStream() . It is required to properly set the autoPaginate option value.

Parameters
Name
Description
Returns
Type
Description
void

getQueryResultsStream(options)

  getQueryResultsStream 
 ( 
 options 
 ?: 
  
 QueryResultsOptions 
 ) 
 : 
  
 ResourceStream<RowMetadata> 
 ; 
 
Parameter
Name
Description
Returns
Type
Description
ResourceStream < RowMetadata >

poll_(callback)

  poll_ 
 ( 
 callback 
 : 
  
 MetadataCallback 
 ) 
 : 
  
 void 
 ; 
 

Poll for a status update. Execute the callback:

  • callback(err): Job failed - callback(): Job incomplete - callback(null, metadata): Job complete
Parameter
Name
Description
callback
Returns
Type
Description
void
Design a Mobile Site
View Site in Mobile | Classic
Share by: