UNPKG

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