/
job.ts
506 lines (472 loc) · 14.3 KB
/
job.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*!
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module bigquery/job
*/
import {
Metadata,
MetadataCallback,
Operation,
util,
} from '@google-cloud/common';
import {paginator, ResourceStream} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {
BigQuery,
JobRequest,
PagedRequest,
QueryRowsCallback,
QueryRowsResponse,
RequestCallback,
} from './bigquery';
import {RowMetadata} from './table';
import bigquery from './types';
export type JobMetadata = bigquery.IJob;
export type JobOptions = JobRequest<JobMetadata>;
export type CancelCallback = RequestCallback<bigquery.IJobCancelResponse>;
export type CancelResponse = [bigquery.IJobCancelResponse];
export type QueryResultsOptions = {job?: Job} & PagedRequest<
bigquery.jobs.IGetQueryResultsParams
>;
/**
* @callback QueryResultsCallback
* @param {?Error} err An error returned while making this request.
* @param {array} rows The results of the job.
*/
/**
* @callback ManualQueryResultsCallback
* @param {?Error} err An error returned while making this request.
* @param {array} rows The results of the job.
* @param {?object} nextQuery A pre-made configuration object for your next
* request. This will be `null` if no additional results are available.
* If the query is not yet complete, you may get empty `rows` and
* non-`null` `nextQuery` that you should use for your next request.
* @param {object} apiResponse The full API response.
*/
/**
* Job objects are returned from various places in the BigQuery API:
*
* - {@link BigQuery#getJobs}
* - {@link BigQuery#job}
* - {@link BigQuery#query}
* - {@link BigQuery#createJob}
* - {@link Table#copy}
* - {@link Table#createWriteStream}
* - {@link Table#extract}
* - {@link Table#load}
*
* They can be used to check the status of a running job or fetching the results
* of a previously-executed one.
*
* @class
* @param {BigQuery} bigQuery {@link BigQuery} instance.
* @param {string} id The ID of the job.
* @param {object} [options] Configuration object.
* @param {string} [options.location] The geographic location of the job.
* Required except for US and EU.
*
* @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();
*/
class Job extends Operation {
bigQuery: BigQuery;
location?: string;
getQueryResultsStream: (
options?: QueryResultsOptions
) => ResourceStream<RowMetadata>;
constructor(bigQuery: BigQuery, id: string, options?: JobOptions) {
let location;
if (options && options.location) {
location = options.location;
}
const methods = {
/**
* Check if the job exists.
*
* @method Job#exists
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {boolean} callback.exists Whether the job exists or not.
* @returns {Promise}
*
* @example
* const {BigQuery} = require('@google-cloud/bigquery');
* const bigquery = new BigQuery();
*
* const job = bigquery.job('job-id');
*
* job.exists((err, exists) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* job.exists().then((data) => {
* const exists = data[0];
* });
*/
exists: true,
/**
* Get a job if it exists.
*
* @method Job#get
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {Job} callback.job The job.
* @returns {Promise}
*
* @example
* const {BigQuery} = require('@google-cloud/bigquery');
* const bigquery = new BigQuery();
*
* const job = bigquery.job('job-id');
*
* job.get((err, job, apiResponse) => {
* if (!err) {
* // `job.metadata` has been populated.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* job.get().then((data) => {
* const job = data[0];
* const apiResponse = data[1];
* });
*/
get: true,
/**
* Get the metadata of the job. This will mostly be useful for checking
* the status of a previously-run job.
*
* @see [Jobs: get API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/get}
*
* @method Job#getMetadata
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this
* request.
* @param {object} callback.metadata The metadata of the job.
* @param {object} callback.apiResponse The full API response.
* @returns {Promise}
*
* @example
* const {BigQuery} = require('@google-cloud/bigquery');
* const bigquery = new BigQuery();
*
* const job = bigquery.job('id');
* job.getMetadata((err, metadata, apiResponse) => {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* job.getMetadata().then((data) => {
* const metadata = data[0];
* const apiResponse = data[1];
* });
*/
getMetadata: {
reqOpts: {
qs: {location},
},
},
};
super({
parent: bigQuery,
baseUrl: '/jobs',
id,
methods,
});
this.bigQuery = bigQuery;
if (options && options.location) {
this.location = options.location;
}
/**
* Get the results of a job as a readable object stream.
*
* @param {object} options Configuration object. See
* {@link Job#getQueryResults} for a complete list of options.
* @return {stream}
*
* @example
* const through2 = require('through2');
* const fs = require('fs');
* const {BigQuery} = require('@google-cloud/bigquery');
* const bigquery = new BigQuery();
*
* const job = bigquery.job('job-id');
*
* job.getQueryResultsStream()
* .pipe(through2.obj(function (row, enc, next) {
* this.push(JSON.stringify(row) + '\n');
* next();
* }))
* .pipe(fs.createWriteStream('./test/testdata/testfile.json'));
*/
this.getQueryResultsStream = paginator.streamify<RowMetadata>(
'getQueryResultsAsStream_'
);
}
cancel(): Promise<CancelResponse>;
cancel(callback: CancelCallback): void;
/**
* Cancel a job. Use {@link Job#getMetadata} to see if the cancel
* completes successfully. See an example implementation below.
*
* @see [Jobs: get API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/cancel}
*
* @param {function} [callback] The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {object} callback.apiResponse The full API response.
* @returns {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?: CancelCallback): void | Promise<CancelResponse> {
let qs;
if (this.location) {
qs = {location: this.location};
}
this.request(
{
method: 'POST',
uri: '/cancel',
qs,
},
callback!
);
}
getQueryResults(options?: QueryResultsOptions): Promise<QueryRowsResponse>;
getQueryResults(
options: QueryResultsOptions,
callback: QueryRowsCallback
): void;
getQueryResults(callback: QueryRowsCallback): void;
/**
* Get the results of a job.
*
* @see [Jobs: getQueryResults API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults}
*
* @param {object} [options] Configuration object.
* @param {boolean} [options.autoPaginate=true] Have pagination handled
* automatically.
* @param {number} [options.maxApiCalls] Maximum number of API calls to make.
* @param {number} [options.maxResults] Maximum number of results to read.
* @param {string} [options.pageToken] Page token, returned by a previous call,
* to request the next page of results. Note: This is automatically added
* to the `nextQuery` argument of your callback.
* @param {number} [options.startIndex] Zero-based index of the starting row.
* @param {number} [options.timeoutMs] How long to wait for the query to
* complete, in milliseconds, before returning. Default is to return
* immediately. If the timeout passes before the job completes, the
* request will fail with a `TIMEOUT` error.
* @param {QueryResultsCallback|ManualQueryResultsCallback} [callback] The
* callback function. If `autoPaginate` is set to false a
* {@link ManualQueryResultsCallback} should be used.
* @returns {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(
optionsOrCallback?: QueryResultsOptions | QueryRowsCallback,
cb?: QueryRowsCallback
): void | Promise<QueryRowsResponse> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
const callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
const qs = extend(
{
location: this.location,
},
options
);
delete qs.job;
this.bigQuery.request(
{
uri: '/queries/' + this.id,
qs,
},
(err, resp) => {
if (err) {
callback!(err, null, null, resp);
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rows: any = [];
if (resp.schema && resp.rows) {
rows = BigQuery.mergeSchemaWithRows_(resp.schema, resp.rows);
}
let nextQuery: {} | null = null;
if (resp.jobComplete === false) {
// Query is still running.
nextQuery = Object.assign({}, options);
} else if (resp.pageToken) {
// More results exist.
nextQuery = Object.assign({}, options, {
pageToken: resp.pageToken,
});
}
callback!(null, rows, nextQuery, resp);
}
);
}
/**
* This method will be called by `getQueryResultsStream()`. It is required to
* properly set the `autoPaginate` option value.
*
* @private
*/
getQueryResultsAsStream_(
options: QueryResultsOptions,
callback: QueryRowsCallback
): void {
options = extend({autoPaginate: false}, options);
this.getQueryResults(options, callback);
}
/**
* Poll for a status update. Execute the callback:
*
* - callback(err): Job failed
* - callback(): Job incomplete
* - callback(null, metadata): Job complete
*
* @private
*
* @param {function} callback
*/
poll_(callback: MetadataCallback): void {
this.getMetadata((err: Error, metadata: Metadata) => {
if (!err && metadata.status && metadata.status.errorResult) {
err = new util.ApiError(metadata.status);
}
if (err) {
callback(err);
return;
}
if (metadata.status.state !== 'DONE') {
callback(null);
return;
}
callback(null, metadata);
});
}
}
/*! Developer Documentation
*
* These methods can be auto-paginated.
*/
paginator.extend(Job, ['getQueryResults']);
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisifyAll(Job);
/**
* Reference to the {@link Job} class.
* @name module:@google-cloud/bigquery.Job
* @see Job
*/
export {Job};