|
| 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 | +} |
0 commit comments