/
BigQueryClient.php
1025 lines (981 loc) · 36.1 KB
/
BigQueryClient.php
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright 2016 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.
*/
namespace Google\Cloud\BigQuery;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\BigQuery\Connection\Rest;
use Google\Cloud\BigQuery\Exception\JobException;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Iterator\PageIterator;
use Google\Cloud\Core\RetryDeciderTrait;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\StreamInterface;
/**
* Google Cloud BigQuery allows you to create, manage, share and query data.
* Find more information at the
* [Google Cloud BigQuery Docs](https://cloud.google.com/bigquery/docs).
*
* Example:
* ```
* use Google\Cloud\BigQuery\BigQueryClient;
*
* $bigQuery = new BigQueryClient();
* ```
*/
class BigQueryClient
{
use ArrayTrait;
use ClientTrait, RetryDeciderTrait {
ClientTrait::jsonEncode insteadof RetryDeciderTrait;
ClientTrait::jsonDecode insteadof RetryDeciderTrait;
}
const VERSION = '1.30.2';
const MAX_DELAY_MICROSECONDS = 32000000;
const SCOPE = 'https://www.googleapis.com/auth/bigquery';
const INSERT_SCOPE = 'https://www.googleapis.com/auth/bigquery.insertdata';
/**
* @var ConnectionInterface Represents a connection to BigQuery.
* @internal
*/
protected $connection;
/**
* @var string The default geographic location.
*/
private $location;
/**
* @var ValueMapper Maps values between PHP and BigQuery.
*/
private $mapper;
/**
* Create a BigQuery client.
*
* @param array $config [optional] {
* Configuration options.
*
* @type string $apiEndpoint The hostname with optional port to use in
* place of the default service endpoint. Example:
* `foobar.com` or `foobar.com:1234`.
* @type string $projectId The project ID from the Google Developer's
* Console.
* @type CacheItemPoolInterface $authCache A cache for storing access
* tokens. **Defaults to** a simple in memory implementation.
* @type array $authCacheOptions Cache configuration options.
* @type callable $authHttpHandler A handler used to deliver Psr7
* requests specifically for authentication.
* @type FetchAuthTokenInterface $credentialsFetcher A credentials
* fetcher instance.
* @type callable $httpHandler A handler used to deliver Psr7 requests.
* Only valid for requests sent over REST.
* @type array $keyFile The contents of the service account credentials
* .json file retrieved from the Google Developer's Console.
* Ex: `json_decode(file_get_contents($path), true)`.
* @type string $keyFilePath The full path to your service account
* credentials .json file retrieved from the Google Developers
* Console.
* @type float $requestTimeout Seconds to wait before timing out the
* request. **Defaults to** `0` with REST and `60` with gRPC.
* @type int $retries Number of retries for a failed request. **Defaults
* to** `3`.
* @type array $scopes Scopes to be used for the request.
* @type string $quotaProject Specifies a user project to bill for
* access charges associated with the request.
* @type bool $returnInt64AsObject If true, 64 bit integers will be
* returned as a {@see Int64} object for 32 bit
* platform compatibility. **Defaults to** false.
* @type string $location If provided, determines the default geographic
* location used when creating datasets and managing jobs. Please
* note: This is only required for jobs started outside of the US
* and EU regions. Also, if location metadata has already been
* fetched over the network it will take precedent over this
* setting (by calling
* {@see Table::reload()}, for example).
* }
*/
public function __construct(array $config = [])
{
$this->location = $this->pluck('location', $config, false);
$this->setHttpRetryCodes([502]);
$this->setHttpRetryMessages([
'rateLimitExceeded',
'backendError'
]);
$config += [
'scopes' => [self::SCOPE],
'projectIdRequired' => true,
'returnInt64AsObject' => false,
'restRetryFunction' => $this->getRetryFunction(),
//@codeCoverageIgnoreStart
'restCalcDelayFunction' => function ($attempt) {
return min(
mt_rand(0, 1000000) + (pow(2, $attempt) * 1000000),
self::MAX_DELAY_MICROSECONDS
);
}
//@codeCoverageIgnoreEnd
];
$this->connection = new Rest($this->configureAuthentication($config));
$this->mapper = new ValueMapper($config['returnInt64AsObject']);
}
/**
* Returns a BigQuery job configuration.
*
* The job configuration is passed to either
* {@see BigQueryClient::runQuery()} or
* {@see BigQueryClient::startQuery()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
*
* Unless otherwise specified, all configuration options will default based on the
* [query job configuration](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationquery)
* except for `configuration.query.useLegacySql`, which defaults to `false`
* in this client.
*
* Example:
* ```
* $queryJobConfig = $bigQuery->query(
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'
* );
* ```
*
* ```
* // Set create disposition using fluent setters.
* $queryJobConfig = $bigQuery->query(
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'
* )->createDisposition('CREATE_NEVER');
* ```
*
* ```
* // This is equivalent to the above example, using array configuration
* // instead of fluent setters.
* $queryJobConfig = $bigQuery->query(
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100',
* [
* 'configuration' => [
* 'query' => [
* 'createDisposition' => 'CREATE_NEVER'
* ]
* ]
* ]
* );
* ```
*
* ```
* // Set a region to run the job in.
* $queryJobConfig = $bigQuery->query(
* 'SELECT name FROM `my_project.users_dataset.users` LIMIT 100'
* )->location('asia-northeast1');
* ```
*
* @param string $query A BigQuery SQL query.
* @param array $options [optional] {
* Configuration options.
*
* @type array $configuration Job configuration. Please see the
* [API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfiguration)
* for the available options.
* @type array $configuration.query Query job configuration. Please see the
* [documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationquery)
* for the available options.
* }
* @return QueryJobConfiguration
*/
public function query($query, array $options = [])
{
return (new QueryJobConfiguration(
$this->mapper,
$this->projectId,
$options,
$this->location
))->query($query);
}
/**
* Returns a BigQuery job configuration.
*
* The job configuration is passed to either
* {@see BigQueryClient::runQuery()} or
* {@see BigQueryClient::startQuery()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
*
* Unless otherwise specified, all configuration options will default based on the
* [query job configuration](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationquery)
* except for `configuration.query.useLegacySql`, which defaults to `false`
* in this client.
*
* As this method is an alias, please see
* {@see BigQueryClient::query()} for usage examples.
*
* @param string $query A BigQuery SQL query.
* @param array $options [optional] {
* Configuration options.
*
* @type array $configuration Job configuration. Please see the
* [API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfiguration)
* for the available options.
* @type array $configuration.query Query job configuration. Please see the
* [documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationquery)
* for the available options.
* }
* @return QueryJobConfiguration
*/
public function queryConfig($query, array $options = [])
{
return $this->query($query, $options);
}
/**
* Runs a BigQuery SQL query in a synchronous fashion.
*
* This method is ideal for queries which return results quickly - otherwise
* we highly recommend utilizing {@see BigQueryClient::startQuery()}
* as it provides better mechanisms for fine grained control over result polling.
*
* Unless `$options.maxRetries` is specified, this method will block until
* the query completes, at which time the result set will be returned.
*
* Queries constructed using
* [standard SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
* can take advantage of parameterization.
*
* Refer to the table below for a guide on how parameter types are mapped to
* their BigQuery equivalents.
*
* | **PHP Type** | **BigQuery Data Type** |
* |--------------------------------------------|--------------------------------------|
* | `\DateTimeInterface` | `DATETIME` |
* | {@see Bytes} | `BYTES` |
* | {@see Date} | `DATE` |
* | {@see Int64} | `INT64` |
* | {@see Time} | `TIME` |
* | {@see Timestamp} | `TIMESTAMP` |
* | Associative Array | `STRUCT` |
* | Non-Associative Array | `ARRAY` |
* | `float` | `FLOAT64` |
* | `int` | `INT64` |
* | `string` | `STRING` |
* | `resource` | `BYTES` |
* | `bool` | `BOOL` |
* | `object` (Outside types specified above) | **ERROR** `InvalidArgumentException` |
*
* Example:
* ```
* $queryJobConfig = $bigQuery->query(
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'
* );
* $queryResults = $bigQuery->runQuery($queryJobConfig);
*
* foreach ($queryResults as $row) {
* echo $row['commit'];
* }
* ```
*
* ```
* // Construct a query utilizing named parameters.
* $query = 'SELECT commit FROM `bigquery-public-data.github_repos.commits`' .
* 'WHERE author.date < @date AND message = @message LIMIT 100';
* $queryJobConfig = $bigQuery->query($query)
* ->parameters([
* 'date' => $bigQuery->timestamp(new \DateTime('1980-01-01 12:15:00Z')),
* 'message' => 'A commit message.'
* ]);
* $queryResults = $bigQuery->runQuery($queryJobConfig);
*
* foreach ($queryResults as $row) {
* echo $row['commit'];
* }
* ```
*
* ```
* // Construct a query utilizing positional parameters.
* $query = 'SELECT commit FROM `bigquery-public-data.github_repos.commits` WHERE message = ? LIMIT 100';
* $queryJobConfig = $bigQuery->query($query)
* ->parameters(['A commit message.']);
* $queryResults = $bigQuery->runQuery($queryJobConfig);
*
* foreach ($queryResults as $row) {
* echo $row['commit'];
* }
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert Jobs insert API Documentation.
*
* @param QueryJobConfiguration $query A BigQuery SQL query configuration.
* @param array $options [optional] {
* Configuration options.
*
* @type int $maxResults The maximum number of rows to return per page
* of results. Setting this flag to a small value such as 1000 and
* then paging through results might improve reliability when the
* query result set is large.
* @type int $startIndex Zero-based index of the starting row.
* @type int $timeoutMs How long, in milliseconds, each API call will
* wait for query results to become available before timing out.
* Depending on whether the $maxRetries has been exceeded, the
* results will be polled again after the timeout has been reached.
* **Defaults to** `10000` milliseconds (10 seconds).
* @type int $maxRetries The number of times to poll the Job status,
* until the job is complete. By default, will poll indefinitely.
* @type bool $returnRawResults Returns the raw data types returned from
* BigQuery without converting their values into native PHP types or
* the custom type classes supported by this library.
* }
* @return QueryResults
* @throws JobException If the maximum number of retries while waiting for
* query completion has been exceeded.
*/
public function runQuery(JobConfigurationInterface $query, array $options = [])
{
$queryResultsOptions = $this->pluckArray([
'maxResults',
'startIndex',
'timeoutMs',
'maxRetries',
'returnRawResults',
], $options);
$queryResultsOptions['initialTimeoutMs'] = 10000;
$queryResults = $this->startQuery(
$query,
$options
)->queryResults($queryResultsOptions + $options);
$queryResults->waitUntilComplete();
return $queryResults;
}
/**
* Runs a BigQuery SQL query in an asynchronous fashion.
*
* Queries constructed using
* [standard SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
* can take advantage of parameterization. For more details and examples
* please see {@see BigQueryClient::runQuery()}.
*
* Example:
* ```
* $queryJobConfig = $bigQuery->query(
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'
* );
* $job = $bigQuery->startQuery($queryJobConfig);
* $queryResults = $job->queryResults();
*
* foreach ($queryResults as $row) {
* echo $row['commit'];
* }
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert Jobs insert API documentation.
*
* @param QueryJobConfiguration $query A BigQuery SQL query configuration.
* @param array $options [optional] Configuration options.
* @return Job
*/
public function startQuery(JobConfigurationInterface $query, array $options = [])
{
return $this->startJob($query, $options);
}
/**
* Lazily instantiates a job.
*
* There are no network requests made at this
* point. To see the operations that can be performed on a job please
* see {@see Job}.
*
* Example:
* ```
* $job = $bigQuery->job('myJobId');
* ```
*
* @param string $id The id of the already run or running job to request.
* @param array $options [optional] {
* Configuration options.
*
* @type string $location The geographic location of the job. Required
* for jobs started outside of the US and EU regions.
* **Defaults to** a location specified in the client
* configuration.
* }
* @return Job
*/
public function job($id, array $options = [])
{
return new Job(
$this->connection,
$id,
$this->projectId,
$this->mapper,
[],
isset($options['location'])
? $options['location']
: $this->location
);
}
/**
* Fetches jobs in the project.
*
* Example:
* ```
* // Get all jobs with the state of 'done'
* $jobs = $bigQuery->jobs([
* 'stateFilter' => 'done'
* ]);
*
* foreach ($jobs as $job) {
* echo $job->id() . PHP_EOL;
* }
* ```
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/list Jobs list API documentation.
*
* @param array $options [optional] {
* Configuration options.
*
* @type bool $allUsers Whether to display jobs owned by all users in
* the project. **Defaults to** `false`.
* @type int $maxResults Maximum number of results to return per page.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* @type string $stateFilter Filter for job state. Maybe be either
* `done`, `pending`, or `running`.
* @type int $maxCreationTime Milliseconds since the POSIX epoch. If set, only jobs created
* before or at this timestamp are returned.
* @type int $minCreationTime Milliseconds since the POSIX epoch. If set, only jobs created
* after or at this timestamp are returned.
* @type string $parentJobId If set, show only child jobs of the
* specified parent. Otherwise, show all top-level jobs.
* }
* @return ItemIterator<Job>
*/
public function jobs(array $options = [])
{
$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $job) {
return new Job(
$this->connection,
$job['jobReference']['jobId'],
$this->projectId,
$this->mapper,
$job
);
},
[$this->connection, 'listJobs'],
$options + ['projectId' => $this->projectId],
[
'itemsKey' => 'jobs',
'resultLimit' => $resultLimit
]
)
);
}
/**
* Lazily instantiates a dataset.
*
* There are no network requests made at this
* point. To see the operations that can be performed on a dataset please
* see {@see Dataset}.
*
* If the dataset is owned by a different project than the project used to authenticate the client,
* provide the project ID as the second argument.
*
* Example:
* ```
* $dataset = $bigQuery->dataset('myDatasetId');
* ```
*
* ```
* // Reference a dataset from other project.
* $dataset = $bigQuery->dataset('samples', 'bigquery-public-data');
* ```
*
* @param string $id The id of the dataset to request.
* @param string|null $projectId The id of the project. **Defaults to** current project id.
* @return Dataset
*/
public function dataset($id, $projectId = null)
{
return new Dataset(
$this->connection,
$id,
$projectId ?: $this->projectId,
$this->mapper,
[],
$this->location
);
}
/**
* Fetches datasets in the project.
*
* Example:
* ```
* $datasets = $bigQuery->datasets();
*
* foreach ($datasets as $dataset) {
* echo $dataset->id() . PHP_EOL;
* }
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/list Datasets list API documentation.
*
* @codingStandardsIgnoreStart
* @param array $options [optional] {
* Configuration options.
*
* @type bool $all Whether to list all datasets, including hidden ones.
* **Defaults to** `false`.
* @type int $maxResults Maximum number of results to return per page.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* @type string $filter An expression for filtering the results of the
* request by label. The syntax is "labels.<name>[:<value>]".
* Multiple filters can be ANDed together by connecting with a
* space. Example: "labels.department:receiving labels.active".
* See [Filtering datasets using labels](https://cloud.google.com/bigquery/docs/filtering-labels#filtering_datasets_using_labels)
* for details.
* }
* @codingStandardsIgnoreEnd
* @return ItemIterator<Dataset>
*/
public function datasets(array $options = [])
{
$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $dataset) {
return new Dataset(
$this->connection,
$dataset['datasetReference']['datasetId'],
$this->projectId,
$this->mapper,
$dataset
);
},
[$this->connection, 'listDatasets'],
$options + ['projectId' => $this->projectId],
[
'itemsKey' => 'datasets',
'resultLimit' => $resultLimit
]
)
);
}
/**
* Creates a dataset.
*
* Please note that by default the library will not attempt to retry this
* call on your behalf. Additionally, if a default location is provided in
* the client configuration it will be used when creating the dataset.
*
* Example:
* ```
* $dataset = $bigQuery->createDataset('aDataset');
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/insert Datasets insert API documentation.
*
* @param string $id The id of the dataset to create.
* @param array $options [optional] {
* Configuration options.
*
* @type array $metadata The available options for metadata are outlined
* at the [Dataset Resource API docs](
* https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets)
* }
* @return Dataset
*/
public function createDataset($id, array $options = [])
{
if (isset($options['metadata'])) {
$options += $options['metadata'];
unset($options['metadata']);
}
if ($this->location && !isset($options['location'])) {
$options['location'] = $this->location;
}
$response = $this->connection->insertDataset(
[
'projectId' => $this->projectId,
'datasetReference' => [
'datasetId' => $id
]
]
+ $options
+ ['retries' => 0]
);
return new Dataset(
$this->connection,
$id,
$this->projectId,
$this->mapper,
$response
);
}
/**
* Starts a job in a synchronous fashion, waiting for the job to complete
* before returning.
*
* Example:
* ```
* $job = $bigQuery->runJob($jobConfig);
* echo $job->isComplete(); // true
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert Jobs insert API Documentation.
*
* @param JobConfigurationInterface $config The job configuration.
* @param array $options [optional] {
* Configuration options.
*
* @type int $maxRetries The number of times to retry, checking if the
* job has completed. **Defaults to** `100`.
* }
* @return Job
*/
public function runJob(JobConfigurationInterface $config, array $options = [])
{
$maxRetries = $this->pluck('maxRetries', $options, false);
$job = $this->startJob($config, $options);
$job->waitUntilComplete(['maxRetries' => $maxRetries]);
return $job;
}
/**
* Starts a job in an asynchronous fashion. In this case, it will be
* required to manually trigger a call to wait for job completion.
*
* Example:
* ```
* $job = $bigQuery->startJob($jobConfig);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert Jobs insert API Documentation.
*
* @param JobConfigurationInterface $config The job configuration.
* @param array $options [optional] Configuration options.
* @return Job
*/
public function startJob(JobConfigurationInterface $config, array $options = [])
{
$response = null;
$config = $config->toArray() + $options;
if (isset($config['data'])) {
$response = $this->connection->insertJobUpload($config)->upload();
} else {
$response = $this->connection->insertJob($config);
}
return new Job(
$this->connection,
$config['jobReference']['jobId'],
$this->projectId,
$this->mapper,
$response
);
}
/**
* Create a Bytes object.
*
* Example:
* ```
* $bytes = $bigQuery->bytes('hello world');
* ```
*
* @param string|resource|StreamInterface $value The bytes value.
* @return Bytes
*/
public function bytes($value)
{
return new Bytes($value);
}
/**
* Create a Date object.
*
* Example:
* ```
* $date = $bigQuery->date(new \DateTime('1995-02-04'));
* ```
*
* @param \DateTimeInterface $value The date value.
* @return Date
*/
public function date(\DateTimeInterface $value)
{
return new Date($value);
}
/**
* Create an Int64 object. This can be used to work with 64 bit integers as
* a string value while on a 32 bit platform.
*
* Example:
* ```
* $int64 = $bigQuery->int64('9223372036854775807');
* ```
*
* @param string $value The 64 bit integer value in string format.
* @return Int64
*/
public function int64($value)
{
return new Int64($value);
}
/**
* Create a Time object.
*
* Example:
* ```
* $time = $bigQuery->time(new \DateTime('12:15:00.482172'));
* ```
*
* @param \DateTimeInterface $value The time value.
* @return Time
*/
public function time(\DateTimeInterface $value)
{
return new Time($value);
}
/**
* Create a Timestamp object.
*
* Example:
* ```
* $timestamp = $bigQuery->timestamp(new \DateTime('2003-02-05 11:15:02.421827Z'));
* ```
*
* @param \DateTimeInterface $value The timestamp value.
* @return Timestamp
*/
public function timestamp(\DateTimeInterface $value)
{
return new Timestamp($value);
}
/**
* Create a Numeric object.
*
* Numeric represents a value with a data type of
* [Numeric](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#numeric_type).
*
* It supports a fixed 38 decimal digits of precision and 9 decimal digits of scale, and values
* are in the range of -99999999999999999999999999999.999999999 to
* 99999999999999999999999999999.999999999.
*
* Example:
* ```
* $numeric = $bigQuery->numeric('99999999999999999999999999999999999999.999999999');
* ```
*
* @param string|int|float $value The Numeric value.
* @return Numeric
* @throws \InvalidArgumentException
*/
public function numeric($value)
{
return new Numeric($value);
}
/**
* Create a BigNumeric object.
*
* Numeric represents a value with a data type of
* [BIGNUMERIC](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#numeric_type).
*
* It supports 76.76 (the 77th digit is partial) decimal digits of precision
* and 38 decimal digits of scale. Values are in the range of
* -5.7896044618658097711785492504343953926634992332820282019728792003956564819968E+38
* to 5.7896044618658097711785492504343953926634992332820282019728792003956564819967E+38.
*
* Example:
* ```
* $bigNumeric = $bigQuery->bigNumeric('999999999999999999999999999999999999999999999.99999999999999');
* ```
*
* @param string|int|float $value The Numeric value.
* @return BigNumeric
* @throws \InvalidArgumentException
*/
public function bigNumeric($value)
{
return new BigNumeric($value);
}
/**
* Create a Geography object.
*
* Example:
* ```
* $geography = $bigQuery->geography('POINT(10 20)');
* ```
*
* @param string $value The geography data in WKT format.
* @return Geography
*/
public function geography($value)
{
return new Geography($value);
}
/**
* Create a BigQuery Json object.
*
* Json represents a value with a data type of
* [JSON](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#json_type)
*
* Example:
* ```
* use Google\Cloud\BigQuery\BigQueryClient;
*
* $bigQuery = new BigQueryClient();
* $json = $bigQuery->json('{"key":"value"}');
* ```
*
* @param string|null $value The JSON string value.
* @return Json
* @throws InvalidArgumentException If the given $value is not string|null.
*/
public function json($value)
{
return new Json($value);
}
/**
* Get a service account for the KMS integration.
*
* Example:
* ```
* $serviceAccount = $bigQuery->getServiceAccount();
* ```
*
* @param array $options [optional] Configuration options.
*
* @return string
*/
public function getServiceAccount(array $options = [])
{
$resp = $this->connection->getServiceAccount($options + ['projectId' => $this->projectId]);
return $resp['email'];
}
/**
* Returns a BigQuery copy job configuration.
*
* The copy job configuration is passed to either
* {@see BigQueryClient::runJob()} or
* {@see BigQueryClient::startJob()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
*
* Example:
* ```
* $copyJobConfig = $bigQuery->copy()
* ->sourceTable($otherTable)
* ->destinationTable($myTable);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert Jobs insert API Documentation.
*
* @param array $options [optional] {
* Configuration options.
*
* @type array $configuration Job configuration. Please see the
* [API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfiguration)
* for the available options.
* @type array $configuration.copy Copy job configuration. Please see the
* [documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationtablecopy)
* for the available options.
* }
* @return CopyJobConfiguration
*/
public function copy(array $options = [])
{
return new CopyJobConfiguration(
$this->projectId,
$options,
$this->location
);
}
/**
* Returns a BigQuery extract job configuration.
*
* The extract job configuration is passed to either
* {@see BigQueryClient::runJob()} or
* {@see BigQueryClient::startJob()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
*
* Example:
* ```
* $extractJobConfig = $bigQuery->extract()
* ->sourceTable($table)
* ->destinationUris(['gs://my-bucket/table.csv']);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert Jobs insert API Documentation.
*
* @param array $options [optional] {
* Configuration options.
*
* @type array $configuration Job configuration. Please see the
* [API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfiguration)
* for the available options.
* @type array $configuration.extract Extract job configuration. Please see the
* [documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#jobconfigurationextract)
* for the available options.
* }
* @return ExtractJobConfiguration
*/
public function extract(array $options = [])
{
return new ExtractJobConfiguration(
$this->projectId,
$options,
$this->location
);
}
/**
* Returns a BigQuery load job configuration.
*
* The load job configuration is passed to either
* {@see BigQueryClient::runJob()} or
* {@see BigQueryClient::startJob()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
*
* Example:
* ```
* $loadJobConfig = $bigQuery->load()
* ->destinationTable($table)
* ->sourceUris(['gs://my-bucket/table.csv']);