UNPKG

9.31 kBTypeScriptView Raw
1/*!
2 * Copyright 2014 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/*!
17 * @module bigquery/job
18 */
19import { MetadataCallback, Operation } from '@google-cloud/common';
20import { ResourceStream } from '@google-cloud/paginator';
21import { BigQuery, IntegerTypeCastOptions, JobRequest, PagedRequest, QueryRowsCallback, QueryRowsResponse, RequestCallback } from './bigquery';
22import { RowMetadata } from './table';
23import bigquery from './types';
24export type JobMetadata = bigquery.IJob;
25export type JobOptions = JobRequest<JobMetadata>;
26export type CancelCallback = RequestCallback<bigquery.IJobCancelResponse>;
27export type CancelResponse = [bigquery.IJobCancelResponse];
28export type QueryResultsOptions = {
29 job?: Job;
30 wrapIntegers?: boolean | IntegerTypeCastOptions;
31 parseJSON?: boolean;
32} & PagedRequest<bigquery.jobs.IGetQueryResultsParams>;
33/**
34 * @callback QueryResultsCallback
35 * @param {?Error} err An error returned while making this request.
36 * @param {array} rows The results of the job.
37 */
38/**
39 * @callback ManualQueryResultsCallback
40 * @param {?Error} err An error returned while making this request.
41 * @param {array} rows The results of the job.
42 * @param {?object} nextQuery A pre-made configuration object for your next
43 * request. This will be `null` if no additional results are available.
44 * If the query is not yet complete, you may get empty `rows` and
45 * non-`null` `nextQuery` that you should use for your next request.
46 * @param {object} apiResponse The full API response.
47 */
48/**
49 * Job objects are returned from various places in the BigQuery API:
50 *
51 * - {@link BigQuery#getJobs}
52 * - {@link BigQuery#job}
53 * - {@link BigQuery#query}
54 * - {@link BigQuery#createJob}
55 * - {@link Table#copy}
56 * - {@link Table#createWriteStream}
57 * - {@link Table#extract}
58 * - {@link Table#load}
59 *
60 * They can be used to check the status of a running job or fetching the results
61 * of a previously-executed one.
62 *
63 * @class
64 * @param {BigQuery} bigQuery {@link BigQuery} instance.
65 * @param {string} id The ID of the job.
66 * @param {object} [options] Configuration object.
67 * @param {string} [options.location] The geographic location of the job.
68 * Required except for US and EU.
69 *
70 * @example
71 * ```
72 * const {BigQuery} = require('@google-cloud/bigquery');
73 * const bigquery = new BigQuery();
74 *
75 * const job = bigquery.job('job-id');
76 *
77 * //-
78 * // All jobs are event emitters. The status of each job is polled
79 * // continuously, starting only after you register a "complete" listener.
80 * //-
81 * job.on('complete', (metadata) => {
82 * // The job is complete.
83 * });
84 *
85 * //-
86 * // Be sure to register an error handler as well to catch any issues which
87 * // impeded the job.
88 * //-
89 * job.on('error', (err) => {
90 * // An error occurred during the job.
91 * });
92 *
93 * //-
94 * // To force the Job object to stop polling for updates, simply remove any
95 * // "complete" listeners you've registered.
96 * //
97 * // The easiest way to do this is with `removeAllListeners()`.
98 * //-
99 * job.removeAllListeners();
100 * ```
101 */
102declare class Job extends Operation {
103 bigQuery: BigQuery;
104 location?: string;
105 projectId?: string;
106 getQueryResultsStream(options?: QueryResultsOptions): ResourceStream<RowMetadata>;
107 constructor(bigQuery: BigQuery, id: string, options?: JobOptions);
108 /**
109 * @callback CancelCallback
110 * @param {?Error} err Request error, if any.
111 * @param {object} metadata The job metadata.
112 * @param {object} apiResponse The full API response.
113 */
114 /**
115 * @typedef {array} CancelResponse
116 * @property {object} 0 The job metadata.
117 * @property {object} 1 The full API response.
118 */
119 /**
120 * Cancel a job. Use {@link Job#getMetadata} to see if the cancel
121 * completes successfully. See an example implementation below.
122 *
123 * See {@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/cancel| Jobs: get API Documentation}
124 *
125 * @param {CancelCallback} [callback] The callback function.
126 * @param {?error} callback.err An error returned while making this request.
127 * @param {object} callback.apiResponse The full API response.
128 * @returns {Promise<CancelResponse>}
129 *
130 * @example
131 * ```
132 * const {BigQuery} = require('@google-cloud/bigquery');
133 * const bigquery = new BigQuery();
134 *
135 * const job = bigquery.job('job-id');
136 *
137 * job.cancel((err, apiResponse) =>{
138 * // Check to see if the job completes successfully.
139 * job.on('error', (err) => {});
140 * job.on('complete', (metadata) => {});
141 * });
142 *
143 * //-
144 * // If the callback is omitted, we'll return a Promise.
145 * //-
146 * job.cancel().then((data) => {
147 * const apiResponse = data[0];
148 * });
149 * ```
150 */
151 cancel(): Promise<CancelResponse>;
152 cancel(callback: CancelCallback): void;
153 /**
154 * Get the results of a job.
155 *
156 * See {@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults| Jobs: getQueryResults API Documentation}
157 *
158 * @param {object} [options] Configuration object.
159 * @param {boolean} [options.autoPaginate=true] Have pagination handled
160 * automatically.
161 * @param {number} [options.maxApiCalls] Maximum number of API calls to make.
162 * @param {number} [options.maxResults] Maximum number of results to read.
163 * @param {string} [options.pageToken] Page token, returned by a previous call,
164 * to request the next page of results. Note: This is automatically added
165 * to the `nextQuery` argument of your callback.
166 * @param {number} [options.startIndex] Zero-based index of the starting row.
167 * @param {number} [options.timeoutMs] How long to wait for the query to
168 * complete, in milliseconds, before returning. Default is 10 seconds.
169 * If the timeout passes before the job completes, an error will be returned
170 * and the 'jobComplete' field in the response will be false.
171 * @param {boolean|IntegerTypeCastOptions} [options.wrapIntegers=false] Wrap values
172 * of 'INT64' type in {@link BigQueryInt} objects.
173 * If a `boolean`, this will wrap values in {@link BigQueryInt} objects.
174 * If an `object`, this will return a value returned by
175 * `wrapIntegers.integerTypeCastFunction`.
176 * @param {QueryResultsCallback|ManualQueryResultsCallback} [callback] The
177 * callback function. If `autoPaginate` is set to false a
178 * {@link ManualQueryResultsCallback} should be used.
179 * @returns {Promise<QueryResultsCallback>}
180 *
181 * @example
182 * ```
183 * const {BigQuery} = require('@google-cloud/bigquery');
184 * const bigquery = new BigQuery();
185 *
186 * const job = bigquery.job('job-id');
187 *
188 * //-
189 * // Get all of the results of a query.
190 * //-
191 * job.getQueryResults((err, rows) => {
192 * if (!err) {
193 * // rows is an array of results.
194 * }
195 * });
196 *
197 * //-
198 * // Customize the results you want to fetch.
199 * //-
200 * job.getQueryResults({
201 * maxResults: 100
202 * }, (err, rows) => {});
203 *
204 * //-
205 * // To control how many API requests are made and page through the results
206 * // manually, set `autoPaginate` to `false`.
207 * //-
208 * function manualPaginationCallback(err, rows, nextQuery, apiResponse) {
209 * if (nextQuery) {
210 * // More results exist.
211 * job.getQueryResults(nextQuery, manualPaginationCallback);
212 * }
213 * }
214 *
215 * job.getQueryResults({
216 * autoPaginate: false
217 * }, manualPaginationCallback);
218 *
219 * //-
220 * // If the callback is omitted, we'll return a Promise.
221 * //-
222 * job.getQueryResults().then((data) => {
223 * const rows = data[0];
224 * });
225 * ```
226 */
227 getQueryResults(options?: QueryResultsOptions): Promise<QueryRowsResponse>;
228 getQueryResults(options: QueryResultsOptions, callback: QueryRowsCallback): void;
229 getQueryResults(callback: QueryRowsCallback): void;
230 /**
231 * This method will be called by `getQueryResultsStream()`. It is required to
232 * properly set the `autoPaginate` option value.
233 *
234 * @private
235 */
236 getQueryResultsAsStream_(options: QueryResultsOptions, callback: QueryRowsCallback): void;
237 /**
238 * Poll for a status update. Execute the callback:
239 *
240 * - callback(err): Job failed
241 * - callback(): Job incomplete
242 * - callback(null, metadata): Job complete
243 *
244 * @private
245 *
246 * @param {function} callback
247 */
248 poll_(callback: MetadataCallback): void;
249}
250/**
251 * Reference to the {@link Job} class.
252 * @name module:@google-cloud/bigquery.Job
253 * @see Job
254 */
255export { Job };