UNPKG

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