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.
Package
@google-cloud/bigqueryExample
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
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.
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
;
void
getQueryResults(options)
getQueryResults
(
options
?:
QueryResultsOptions
)
:
Promise<QueryRowsResponse>
;
Get the results of a job.
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
;
void
getQueryResults(callback)
getQueryResults
(
callback
:
QueryRowsCallback
)
:
void
;
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.
void
getQueryResultsStream(options)
getQueryResultsStream
(
options
?:
QueryResultsOptions
)
:
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
void