Skip to content

Commit 172cbca

Browse files
alixhamibshaffer
authored andcommitted
Refactor BigQuery sample tests (GoogleCloudPlatform#708)
1 parent 1188ebb commit 172cbca

File tree

5 files changed

+152
-91
lines changed

5 files changed

+152
-91
lines changed

bigquery/api/snippets/import_from_file.php renamed to bigquery/api/snippets/import_from_local_csv.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) != 5) {
28-
return print("Usage: php snippets/import_from_file.php PROJECT_ID DATASET_ID TABLE_ID SOURCE\n");
28+
return print("Usage: php snippets/import_from_local_csv.php PROJECT_ID DATASET_ID TABLE_ID SOURCE\n");
2929
}
3030

3131
list($_, $projectId, $datasetId, $tableId, $source) = $argv;
@@ -38,7 +38,7 @@
3838
// $projectId = 'The Google project ID';
3939
// $datasetId = 'The BigQuery dataset ID';
4040
// $tableId = 'The BigQuery table ID';
41-
// $source = 'The path to the source file to import';
41+
// $source = 'The path to the CSV source file to import';
4242

4343
// instantiate the bigquery table service
4444
$bigQuery = new BigQueryClient([
@@ -47,16 +47,8 @@
4747
$dataset = $bigQuery->dataset($datasetId);
4848
$table = $dataset->table($tableId);
4949
// create the import job
50-
$loadConfig = $table->load(fopen($source, 'r'));
51-
// determine the source format from the object name
52-
$pathInfo = pathinfo($source) + ['extension' => null];
53-
if ('csv' === $pathInfo['extension']) {
54-
$loadConfig->sourceFormat('CSV');
55-
} elseif ('json' === $pathInfo['extension']) {
56-
$loadConfig->sourceFormat('NEWLINE_DELIMITED_JSON');
57-
} else {
58-
throw new InvalidArgumentException('Source format unknown. Must be JSON or CSV');
59-
}
50+
$loadConfig = $table->load(fopen($source, 'r'))->sourceFormat('CSV');
51+
6052
$job = $table->runJob($loadConfig);
6153
// poll the job until it is complete
6254
$backoff = new ExponentialBackoff(10);

bigquery/api/snippets/import_from_storage.php renamed to bigquery/api/snippets/import_from_storage_csv.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,35 @@
2323

2424
// Include Google Cloud dependendencies using Composer
2525
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 6) {
28-
return print("Usage: php snippets/import_from_storage.php PROJECT_ID DATASET_ID TABLE_ID BUCKET_NAME OBJECT_NAME\n");
26+
if (count($argv) != 3) {
27+
return print("Usage: php snippets/import_from_storage.php PROJECT_ID DATASET_ID\n");
2928
}
3029

31-
list($_, $projectId, $datasetId, $tableId, $bucketName, $objectName) = $argv;
32-
30+
list($_, $projectId, $datasetId) = $argv;
3331
# [START bigquery_load_table_gcs_csv]
3432
use Google\Cloud\BigQuery\BigQueryClient;
35-
use Google\Cloud\Storage\StorageClient;
3633
use Google\Cloud\Core\ExponentialBackoff;
3734

3835
/** Uncomment and populate these variables in your code */
3936
// $projectId = 'The Google project ID';
4037
// $datasetId = 'The BigQuery dataset ID';
41-
// $tableId = 'The BigQuery table ID';
42-
// $bucketName = 'The Cloud Storage bucket Name';
43-
// $objectName = 'The Cloud Storage object Name';
4438

4539
// instantiate the bigquery table service
4640
$bigQuery = new BigQueryClient([
4741
'projectId' => $projectId,
4842
]);
4943
$dataset = $bigQuery->dataset($datasetId);
50-
$table = $dataset->table($tableId);
51-
// load the storage object
52-
$storage = new StorageClient([
53-
'projectId' => $projectId,
54-
]);
55-
$object = $storage->bucket($bucketName)->object($objectName);
44+
$table = $dataset->table('us_states');
45+
5646
// create the import job
57-
$loadConfig = $table->loadFromStorage($object);
58-
// determine the source format from the object name
59-
if ('.backup_info' === substr($objectName, -12)) {
60-
$loadConfig->sourceFormat('DATASTORE_BACKUP');
61-
} elseif ('.json' === substr($objectName, -5)) {
62-
$loadConfig->sourceFormat('NEWLINE_DELIMITED_JSON');
63-
}
47+
$gcsUri = 'gs://cloud-samples-data/bigquery/us-states/us-states.csv';
48+
$schema = [
49+
'fields' => [
50+
['name' => 'name', 'type' => 'string'],
51+
['name' => 'post_abbr', 'type' => 'string']
52+
]
53+
];
54+
$loadConfig = $table->loadFromStorage($gcsUri)->schema($schema)->skipLeadingRows(1);
6455
$job = $table->runJob($loadConfig);
6556
// poll the job until it is complete
6657
$backoff = new ExponentialBackoff(10);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/**
3+
* Copyright 2018 Google LLC.
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigquery/api/README.md
22+
*/
23+
24+
// Include Google Cloud dependendencies using Composer
25+
require_once __DIR__ . '/../vendor/autoload.php';
26+
if (count($argv) != 3) {
27+
return print("Usage: php snippets/import_from_storage.php PROJECT_ID DATASET_ID\n");
28+
}
29+
30+
list($_, $projectId, $datasetId) = $argv;
31+
# [START bigquery_load_table_gcs_json]
32+
use Google\Cloud\BigQuery\BigQueryClient;
33+
use Google\Cloud\Core\ExponentialBackoff;
34+
35+
/** Uncomment and populate these variables in your code */
36+
// $projectId = 'The Google project ID';
37+
// $datasetId = 'The BigQuery dataset ID';
38+
39+
// instantiate the bigquery table service
40+
$bigQuery = new BigQueryClient([
41+
'projectId' => $projectId,
42+
]);
43+
$dataset = $bigQuery->dataset($datasetId);
44+
$table = $dataset->table('us_states');
45+
46+
// create the import job
47+
$gcsUri = 'gs://cloud-samples-data/bigquery/us-states/us-states.json';
48+
$schema = [
49+
'fields' => [
50+
['name' => 'name', 'type' => 'string'],
51+
['name' => 'post_abbr', 'type' => 'string']
52+
]
53+
];
54+
$loadConfig = $table->loadFromStorage($gcsUri)->schema($schema)->sourceFormat('NEWLINE_DELIMITED_JSON');
55+
$job = $table->runJob($loadConfig);
56+
// poll the job until it is complete
57+
$backoff = new ExponentialBackoff(10);
58+
$backoff->execute(function () use ($job) {
59+
print('Waiting for job to complete' . PHP_EOL);
60+
$job->reload();
61+
if (!$job->isComplete()) {
62+
throw new Exception('Job has not yet completed', 500);
63+
}
64+
});
65+
// check if the job has errors
66+
if (isset($job->info()['status']['errorResult'])) {
67+
$error = $job->info()['status']['errorResult']['message'];
68+
printf('Error running job: %s' . PHP_EOL, $error);
69+
} else {
70+
print('Data imported successfully' . PHP_EOL);
71+
}
72+
# [END bigquery_load_table_gcs_json]

0 commit comments

Comments
 (0)