Skip to content

Commit 1d884c1

Browse files
alixhamibshaffer
authored andcommitted
Add 8 BigQuery Load samples (GoogleCloudPlatform#711)
1 parent d0194dd commit 1d884c1

26 files changed

+598
-28
lines changed

bigquery/api/snippets/browse_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) < 4 || count($argv) > 5) {
28-
return print("Usage: php snippets/browse_table.php PROJECT_ID DATASET_ID TABLE_ID [NUM_RESULTS]\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID [NUM_RESULTS]\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId, $tableId) = $argv;
3131
$maxResults = isset($argv[4]) ? $argv[4] : 10;

bigquery/api/snippets/copy_table.php

Lines changed: 1 addition & 1 deletion
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/copy_table.php PROJECT_ID DATASET_ID SOURCE_TABLE_ID DESTINATION_TABLE_ID\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID SOURCE_TABLE_ID DESTINATION_TABLE_ID\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId, $sourceTableId, $destinationTableId) = $argv;
3131

bigquery/api/snippets/create_dataset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) != 3) {
28-
return print("Usage: php snippets/create_dataset.php PROJECT_ID DATASET_ID\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId) = $argv;
3131

bigquery/api/snippets/create_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) < 4 || count($argv) > 5) {
28-
return print("Usage: php snippets/create_table.php PROJECT_ID DATASET_ID TABLE_ID [FIELDS]\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID [FIELDS]\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId, $tableId) = $argv;
3131
$fields = isset($argv[4]) ? json_decode($argv[4]) : [['name' => 'field1', 'type' => 'string']];

bigquery/api/snippets/delete_dataset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) > 3) {
28-
return print("Usage: php snippets/delete_dataset.php PROJECT_ID DATASET_ID\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId) = $argv;
3131

bigquery/api/snippets/delete_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) != 4) {
28-
return print("Usage: php snippets/delete_table.php PROJECT_ID DATASET_ID TABLE_ID\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID\n", __FILE__);
2929
}
3030
list($_, $projectId, $datasetId, $tableId) = $argv;
3131

bigquery/api/snippets/extract_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

2727
if (count($argv) < 6 || count($argv) > 7) {
28-
return print("Usage: php snippets/extract_table.php PROJECT_ID DATASET_ID TABLE_ID BUCKET_NAME OBJECT_NAME [FORMAT]\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID BUCKET_NAME OBJECT_NAME [FORMAT]\n", __FILE__);
2929
}
3030

3131
list($_, $projectId, $datasetId, $tableId, $bucketName, $objectName) = $argv;

bigquery/api/snippets/import_from_local_csv.php

Lines changed: 1 addition & 1 deletion
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_local_csv.php PROJECT_ID DATASET_ID TABLE_ID SOURCE\n");
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID SOURCE\n", __FILE__);
2929
}
3030

3131
list($_, $projectId, $datasetId, $tableId, $source) = $argv;

bigquery/api/snippets/import_from_storage_csv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Include Google Cloud dependendencies using Composer
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626
if (count($argv) != 3) {
27-
return print("Usage: php snippets/import_from_storage.php PROJECT_ID DATASET_ID\n");
27+
return printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
2828
}
2929

3030
list($_, $projectId, $datasetId) = $argv;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
28+
}
29+
30+
list($_, $projectId, $datasetId) = $argv;
31+
# [START bigquery_load_table_gcs_csv_autodetect]
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.csv';
48+
$loadConfig = $table->loadFromStorage($gcsUri)->autodetect(true)->skipLeadingRows(1);
49+
$job = $table->runJob($loadConfig);
50+
// poll the job until it is complete
51+
$backoff = new ExponentialBackoff(10);
52+
$backoff->execute(function () use ($job) {
53+
print('Waiting for job to complete' . PHP_EOL);
54+
$job->reload();
55+
if (!$job->isComplete()) {
56+
throw new Exception('Job has not yet completed', 500);
57+
}
58+
});
59+
// check if the job has errors
60+
if (isset($job->info()['status']['errorResult'])) {
61+
$error = $job->info()['status']['errorResult']['message'];
62+
printf('Error running job: %s' . PHP_EOL, $error);
63+
} else {
64+
print('Data imported successfully' . PHP_EOL);
65+
}
66+
# [END bigquery_load_table_gcs_csv_autodetect]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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) != 4) {
27+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID\n", __FILE__);
28+
}
29+
30+
list($_, $projectId, $datasetId, $tableId) = $argv;
31+
# [START bigquery_load_table_gcs_csv_truncate]
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+
// $tableId = 'The BigQuery table ID';
39+
40+
// instantiate the bigquery table service
41+
$bigQuery = new BigQueryClient([
42+
'projectId' => $projectId,
43+
]);
44+
$table = $bigQuery->dataset($datasetId)->table($tableId);
45+
46+
// create the import job
47+
$gcsUri = 'gs://cloud-samples-data/bigquery/us-states/us-states.csv';
48+
$loadConfig = $table->loadFromStorage($gcsUri)->skipLeadingRows(1)->writeDisposition('WRITE_TRUNCATE');
49+
$job = $table->runJob($loadConfig);
50+
51+
// poll the job until it is complete
52+
$backoff = new ExponentialBackoff(10);
53+
$backoff->execute(function () use ($job) {
54+
print('Waiting for job to complete' . PHP_EOL);
55+
$job->reload();
56+
if (!$job->isComplete()) {
57+
throw new Exception('Job has not yet completed', 500);
58+
}
59+
});
60+
61+
// check if the job has errors
62+
if (isset($job->info()['status']['errorResult'])) {
63+
$error = $job->info()['status']['errorResult']['message'];
64+
printf('Error running job: %s' . PHP_EOL, $error);
65+
} else {
66+
print('Data imported successfully' . PHP_EOL);
67+
}
68+
# [END bigquery_load_table_gcs_csv_truncate]

bigquery/api/snippets/import_from_storage_json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// Include Google Cloud dependendencies using Composer
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626
if (count($argv) != 3) {
27-
return print("Usage: php snippets/import_from_storage.php PROJECT_ID DATASET_ID\n");
27+
return printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
2828
}
2929

3030
list($_, $projectId, $datasetId) = $argv;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 printf("Usage: php %s PROJECT_ID DATASET_ID\n", __FILE__);
28+
}
29+
30+
list($_, $projectId, $datasetId) = $argv;
31+
# [START bigquery_load_table_gcs_json_autodetect]
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+
$loadConfig = $table->loadFromStorage($gcsUri)->autodetect(true)->sourceFormat('NEWLINE_DELIMITED_JSON');
49+
$job = $table->runJob($loadConfig);
50+
// poll the job until it is complete
51+
$backoff = new ExponentialBackoff(10);
52+
$backoff->execute(function () use ($job) {
53+
print('Waiting for job to complete' . PHP_EOL);
54+
$job->reload();
55+
if (!$job->isComplete()) {
56+
throw new Exception('Job has not yet completed', 500);
57+
}
58+
});
59+
// check if the job has errors
60+
if (isset($job->info()['status']['errorResult'])) {
61+
$error = $job->info()['status']['errorResult']['message'];
62+
printf('Error running job: %s' . PHP_EOL, $error);
63+
} else {
64+
print('Data imported successfully' . PHP_EOL);
65+
}
66+
# [END bigquery_load_table_gcs_json_autodetect]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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) != 4) {
27+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID\n", __FILE__);
28+
}
29+
30+
list($_, $projectId, $datasetId, $tableId) = $argv;
31+
# [START bigquery_load_table_gcs_json_truncate]
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+
// $tableID = 'The BigQuery table ID';
39+
40+
// instantiate the bigquery table service
41+
$bigQuery = new BigQueryClient([
42+
'projectId' => $projectId,
43+
]);
44+
$table = $bigQuery->dataset($datasetId)->table($tableId);
45+
46+
// create the import job
47+
$gcsUri = 'gs://cloud-samples-data/bigquery/us-states/us-states.json';
48+
$loadConfig = $table->loadFromStorage($gcsUri)->sourceFormat('NEWLINE_DELIMITED_JSON')->writeDisposition('WRITE_TRUNCATE');
49+
$job = $table->runJob($loadConfig);
50+
51+
// poll the job until it is complete
52+
$backoff = new ExponentialBackoff(10);
53+
$backoff->execute(function () use ($job) {
54+
print('Waiting for job to complete' . PHP_EOL);
55+
$job->reload();
56+
if (!$job->isComplete()) {
57+
throw new Exception('Job has not yet completed', 500);
58+
}
59+
});
60+
61+
// check if the job has errors
62+
if (isset($job->info()['status']['errorResult'])) {
63+
$error = $job->info()['status']['errorResult']['message'];
64+
printf('Error running job: %s' . PHP_EOL, $error);
65+
} else {
66+
print('Data imported successfully' . PHP_EOL);
67+
}
68+
# [END bigquery_load_table_gcs_json_truncate]

0 commit comments

Comments
 (0)