Skip to content

Commit bbc6c7c

Browse files
committed
update tests/docs and add copy/load
1 parent beba45d commit bbc6c7c

26 files changed

+2616
-486
lines changed

src/BigQuery/BigQueryClient.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Google\Cloud\BigQuery\Connection\Rest;
2222
use Google\Cloud\BigQuery\Exception\JobException;
2323
use Google\Cloud\BigQuery\Job;
24+
use Google\Cloud\Core\ArrayTrait;
2425
use Google\Cloud\Core\ClientTrait;
2526
use Google\Cloud\Core\Int64;
2627
use Google\Cloud\Core\Iterator\ItemIterator;
@@ -42,6 +43,7 @@
4243
*/
4344
class BigQueryClient
4445
{
46+
use ArrayTrait;
4547
use ClientTrait;
4648

4749
const VERSION = '0.2.2';
@@ -111,10 +113,10 @@ public function __construct(array $config = [])
111113
* set of options at once.
112114
*
113115
* Unless otherwise specified, all configuration options will default based
114-
* on the
115-
* [Jobs configuration API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration)
116-
* except for `configuration.query.useLegacy`, which defaults to `false` in
117-
* this client.
116+
* on the [Jobs configuration API documentation]
117+
* (https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration)
118+
* except for `configuration.query.useLegacySql`, which defaults to `false`
119+
* in this client.
118120
*
119121
* Example:
120122
* ```
@@ -127,7 +129,7 @@ public function __construct(array $config = [])
127129
* // Set create disposition using fluent setters.
128130
* $queryJobConfig = $bigQuery->query(
129131
* 'SELECT commit FROM `bigquery-public-data.github_repos.commits` LIMIT 100'
130-
* )->createDisposition(QueryJobConfiguration::CREATE_DISPOSITION_CREATE_NEVER);
132+
* )->createDisposition('CREATE_NEVER');
131133
* ```
132134
*
133135
* ```
@@ -138,7 +140,7 @@ public function __construct(array $config = [])
138140
* [
139141
* 'configuration' => [
140142
* 'query' => [
141-
* 'createDisposition' => QueryJobConfiguration::CREATE_DISPOSITION_CREATE_NEVER
143+
* 'createDisposition' => 'CREATE_NEVER'
142144
* ]
143145
* ]
144146
* ]
@@ -233,7 +235,7 @@ public function query($query, array $options = [])
233235
*
234236
* @see https://cloud.google.com/bigquery/docs/reference/v2/jobs/query Query API documentation.
235237
*
236-
* @param QueryJobConfiguration $query A BigQuery SQL query.
238+
* @param QueryJobConfiguration $query A BigQuery SQL query configuration.
237239
* @param array $options [optional] {
238240
* Configuration options.
239241
*
@@ -251,7 +253,7 @@ public function query($query, array $options = [])
251253
* @throws JobException If the maximum number of retries while waiting for
252254
* query completion has been exceeded.
253255
*/
254-
public function runQuery(QueryJobConfiguration $query, array $options = [])
256+
public function runQuery(JobConfigurationInterface $query, array $options = [])
255257
{
256258
$queryResultsOptions = $this->pluckArray([
257259
'maxResults',
@@ -289,11 +291,11 @@ public function runQuery(QueryJobConfiguration $query, array $options = [])
289291
*
290292
* @see https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert Jobs insert API documentation.
291293
*
292-
* @param QueryJobConfiguration $query A BigQuery SQL query.
294+
* @param QueryJobConfiguration $query A BigQuery SQL query configuration.
293295
* @param array $options [optional] Configuration options.
294296
* @return Job
295297
*/
296-
public function startQuery(QueryJobConfiguration $query, array $options = [])
298+
public function startQuery(JobConfigurationInterface $query, array $options = [])
297299
{
298300
$config = $query->toArray();
299301
$response = $this->connection->insertJob($config + $options);

src/BigQuery/Connection/Rest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,19 @@ private function resolveUploadOptions(array $args)
246246
$args += [
247247
'projectId' => null,
248248
'data' => null,
249-
'configuration' => []
249+
'configuration' => [],
250+
'labels' => [],
251+
'dryRun' => false,
252+
'jobReference' => []
250253
];
251254

252255
$args['data'] = Psr7\stream_for($args['data']);
253-
$args['metadata']['configuration'] = $args['configuration'];
254-
unset($args['configuration']);
256+
$args['metadata'] = $this->pluckArray([
257+
'labels',
258+
'dryRun',
259+
'jobReference',
260+
'configuration'
261+
], $args);
255262

256263
$uploaderOptionKeys = [
257264
'restOptions',
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Copyright 2017 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\BigQuery;
19+
20+
/**
21+
* Represents a configuration for a copy job. For more information on the
22+
* available settings please see the
23+
* [Jobs configuration API documentation](https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration).
24+
*
25+
* Example:
26+
* ```
27+
* use Google\Cloud\BigQuery\BigQueryClient;
28+
*
29+
* $bigQuery = new BigQueryClient();
30+
* $sourceTable = $bigQuery->dataset('my_dataset')
31+
* ->table('my_source_table');
32+
* $destinationTable = $bigQuery->dataset('my_dataset')
33+
* ->table('my_destination_table');
34+
*
35+
* $copyJobConfig = $sourceTable->copy($destinationTable);
36+
* ```
37+
*/
38+
class CopyJobConfiguration implements JobConfigurationInterface
39+
{
40+
use JobConfigurationTrait;
41+
42+
/**
43+
* @param string $projectId The project's ID.
44+
* @param array $config A set of configuration options for a job.
45+
*/
46+
public function __construct($projectId, array $config)
47+
{
48+
$this->jobConfigurationProperties($projectId, $config);
49+
}
50+
51+
/**
52+
* Set whether the job is allowed to create new tables. Creation, truncation
53+
* and append actions occur as one atomic update upon job completion.
54+
*
55+
* Example:
56+
* ```
57+
* $copyJobConfig->createDisposition('CREATE_NEVER');
58+
* ```
59+
*
60+
* @param string $createDisposition The create disposition. Acceptable
61+
* values include `"CREATED_IF_NEEDED"`, `"CREATE_NEVER"`. **Defaults
62+
* to** `"CREATE_IF_NEEDED"`.
63+
* @return CopyJobConfiguration
64+
*/
65+
public function createDisposition($createDisposition)
66+
{
67+
$this->config['configuration']['copy']['createDisposition'] = $createDisposition;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Sets the custom encryption configuration (e.g., Cloud KMS keys).
74+
*
75+
* Example:
76+
* ```
77+
* $copyJobConfig->destinationEncryptionConfiguration([
78+
* 'kmsKeyName' => 'my_key'
79+
* ]);
80+
* ```
81+
*
82+
* @param array $configuration Custom encryption configuration.
83+
* @return CopyJobConfiguration
84+
*/
85+
public function destinationEncryptionConfiguration(array $configuration)
86+
{
87+
$this->config['configuration']['copy']['destinationEncryptionConfiguration'] = $configuration;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Sets the destination table.
94+
*
95+
* Example:
96+
* ```
97+
* $table = $bigQuery->dataset('my_dataset')
98+
* ->table('my_table');
99+
* $copyJobConfig->destinationTable($table);
100+
* ```
101+
*
102+
* @param Table $destinationTable The destination table.
103+
* @return CopyJobConfiguration
104+
*/
105+
public function destinationTable(Table $destinationTable)
106+
{
107+
$this->config['configuration']['copy']['destinationTable'] = $destinationTable->identity();
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* Sets the source table to copy.
114+
*
115+
* Example:
116+
* ```
117+
* $table = $bigQuery->dataset('my_dataset')
118+
* ->table('source_table');
119+
* $copyJobConfig->sourceTable($table);
120+
* ```
121+
*
122+
* @param Table $sourceTable The destination table.
123+
* @return CopyJobConfiguration
124+
*/
125+
public function sourceTable(Table $sourceTable)
126+
{
127+
$this->config['configuration']['copy']['sourceTable'] = $sourceTable->identity();
128+
129+
return $this;
130+
}
131+
132+
/**
133+
* Sets the action that occurs if the destination table already exists. Each
134+
* action is atomic and only occurs if BigQuery is able to complete the job
135+
* successfully. Creation, truncation and append actions occur as one atomic
136+
* update upon job completion.
137+
*
138+
* Example:
139+
* ```
140+
* $copyJobConfig->writeDisposition('WRITE_TRUNCATE');
141+
* ```
142+
*
143+
* @param string $writeDisposition The write disposition. Acceptable values
144+
* include `"WRITE_TRUNCATE"`, `"WRITE_APPEND"`, `"WRITE_EMPTY"`.
145+
* **Defaults to** `"WRITE_EMPTY"`.
146+
* @return CopyJobConfiguration
147+
*/
148+
public function writeDisposition($writeDisposition)
149+
{
150+
$this->config['configuration']['copy']['writeDisposition'] = $writeDisposition;
151+
152+
return $this;
153+
}
154+
}

0 commit comments

Comments
 (0)