Skip to content

Commit 458cc69

Browse files
authored
adds create_table, create_dataset, and copy_table to bigquery samples (GoogleCloudPlatform#232)
1 parent 6320a96 commit 458cc69

File tree

9 files changed

+423
-39
lines changed

9 files changed

+423
-39
lines changed

bigquery/api/bigquery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require __DIR__ . '/vendor/autoload.php';
44

55
use Google\Cloud\Samples\BigQuery\BrowseTableCommand;
6+
use Google\Cloud\Samples\BigQuery\CopyTableCommand;
67
use Google\Cloud\Samples\BigQuery\DatasetsCommand;
78
use Google\Cloud\Samples\BigQuery\ExportCommand;
89
use Google\Cloud\Samples\BigQuery\ImportCommand;
@@ -14,6 +15,7 @@
1415

1516
$application = new Application();
1617
$application->add(new BrowseTableCommand());
18+
$application->add(new CopyTableCommand());
1719
$application->add(new DatasetsCommand());
1820
$application->add(new ExportCommand());
1921
$application->add(new ImportCommand());

bigquery/api/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud": "0.10",
3+
"google/cloud": "^0.11",
44
"symfony/console": "^3.0"
55
},
66
"require-dev": {
@@ -13,6 +13,9 @@
1313
},
1414
"files": [
1515
"src/functions/browse_table.php",
16+
"src/functions/copy_table.php",
17+
"src/functions/create_dataset.php",
18+
"src/functions/create_table.php",
1619
"src/functions/delete_table.php",
1720
"src/functions/export_table.php",
1821
"src/functions/import_from_file.php",

bigquery/api/composer.lock

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bigquery/api/src/CopyTableCommand.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
/**
3+
* Copyright 2015 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\Samples\BigQuery;
19+
20+
use Symfony\Component\Console\Command\Command;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
use Google\Cloud\ServiceBuilder;
26+
use InvalidArgumentException;
27+
28+
/**
29+
* Command line utility to copy a BigQuery table.
30+
*
31+
* Usage: php bigquery.php copy-table
32+
*/
33+
class CopyTableCommand extends Command
34+
{
35+
use ProjectIdTrait;
36+
37+
protected function configure()
38+
{
39+
$this
40+
->setName('copy-table')
41+
->setDescription('Copy a BigQuery table into another BigQuery table')
42+
->setHelp(<<
43+
The %command.name% command copies your data and schema from a BigQuery
44+
table into another BigQuery Table.
45+
46+
php %command.full_name% DATASET SOURCE_TABLE DESTINATION_TABLE
47+
48+
49+
EOF
50+
)
51+
->addArgument(
52+
'dataset',
53+
InputArgument::REQUIRED,
54+
'The dataset for the copy'
55+
)
56+
->addArgument(
57+
'source-table',
58+
InputArgument::REQUIRED,
59+
'The BigQuery table to copy from'
60+
)
61+
->addArgument(
62+
'destination-table',
63+
InputArgument::REQUIRED,
64+
'The BigQuery table to copy to'
65+
)
66+
->addOption(
67+
'project',
68+
null,
69+
InputOption::VALUE_REQUIRED,
70+
'The Google Cloud Platform project name to use for this invocation. ' .
71+
'If omitted then the current gcloud project is assumed. '
72+
)
73+
;
74+
}
75+
76+
protected function execute(InputInterface $input, OutputInterface $output)
77+
{
78+
if (!$projectId = $input->getOption('project')) {
79+
$projectId = $this->getProjectIdFromGcloud();
80+
}
81+
$datasetId = $input->getArgument('dataset');
82+
$sourceTableId = $input->getArgument('source-table');
83+
$builder = new ServiceBuilder([
84+
'projectId' => $projectId,
85+
]);
86+
$bigQuery = $builder->bigQuery();
87+
$dataset = $bigQuery->dataset($datasetId);
88+
$sourceTable = $dataset->table($sourceTableId);
89+
$destinationTableId = $input->getArgument('destination-table');
90+
if (!$dataset->exists()) {
91+
throw new InvalidArgumentException('The supplied dataset does not exist for this project');
92+
}
93+
if (!$sourceTable->exists()) {
94+
throw new InvalidArgumentException('The supplied source table does not exist for this project. ');
95+
}
96+
$message = sprintf('Copying table for project %s', $projectId);
97+
$output->writeln($message);
98+
99+
copy_table($projectId, $datasetId, $sourceTableId, $destinationTableId);
100+
}
101+
}

bigquery/api/src/SchemaCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
126126
return $output->writeln('Task cancelled by user.');
127127
}
128128
}
129-
$dataset = $bigQuery->createDataset($datasetId);
129+
$dataset = create_dataset($projectId, $datasetId);
130130
}
131131

132132
if ($input->getOption('delete')) {
@@ -169,8 +169,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
169169
}
170170
}
171171
try {
172-
$options = ['schema' => ['fields' => $fields]];
173-
$table = $dataset->createTable($tableId, $options);
172+
$schema = ['fields' => $fields];
173+
create_table($projectId, $datasetId, $tableId, $schema);
174174
} catch (BadRequestException $e) {
175175
$response = $e->getServiceException()->getResponse();
176176
$errorJson = json_decode((string) $response->getBody(), true);

0 commit comments

Comments
 (0)