Skip to content

Commit 0ea346e

Browse files
alixhamibshaffer
authored andcommitted
BigQuery: Fix extract table sample (GoogleCloudPlatform#771)
1 parent 3b7f7fd commit 0ea346e

File tree

2 files changed

+14
-53
lines changed

2 files changed

+14
-53
lines changed

bigquery/api/src/extract_table.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,32 @@
2424
// Include Google Cloud dependendencies using Composer
2525
require_once __DIR__ . '/../vendor/autoload.php';
2626

27-
if (count($argv) < 6 || count($argv) > 7) {
28-
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID BUCKET_NAME OBJECT_NAME [FORMAT]\n", __FILE__);
27+
if (count($argv) != 5) {
28+
return printf("Usage: php %s PROJECT_ID DATASET_ID TABLE_ID BUCKET_NAME\n", __FILE__);
2929
}
3030

31-
list($_, $projectId, $datasetId, $tableId, $bucketName, $objectName) = $argv;
32-
$format = isset($argv[6]) ? $argv[6] : 'csv';
31+
list($_, $projectId, $datasetId, $tableId, $bucketName) = $argv;
3332

3433
# [START bigquery_extract_table]
3534
use Google\Cloud\BigQuery\BigQueryClient;
36-
use Google\Cloud\Storage\StorageClient;
37-
use Google\Cloud\Core\ExponentialBackoff;
3835

3936
/** Uncomment and populate these variables in your code */
4037
// $projectId = 'The Google project ID';
4138
// $datasetId = 'The BigQuery dataset ID';
4239
// $tableId = 'The BigQuery table ID';
4340
// $bucketName = 'The Cloud Storage bucket Name';
44-
// $objectName = 'The Cloud Storage object Name';
45-
// $format = 'The extract format, either "csv" or "json"';
4641

4742
$bigQuery = new BigQueryClient([
4843
'projectId' => $projectId,
4944
]);
5045
$dataset = $bigQuery->dataset($datasetId);
5146
$table = $dataset->table($tableId);
52-
// load the storage object
53-
$storage = new StorageClient([
54-
'projectId' => $projectId,
55-
]);
56-
$destinationObject = $storage->bucket($bucketName)->object($objectName);
57-
// create the extract job
58-
$options = ['destinationFormat' => $format];
59-
$extractConfig = $table->extract($destinationObject, $options);
60-
$job = $table->runJob($extractConfig);
61-
// poll the job until it is complete
62-
$backoff = new ExponentialBackoff(10);
63-
$backoff->execute(function () use ($job) {
64-
print('Waiting for job to complete' . PHP_EOL);
65-
$job->reload();
66-
if (!$job->isComplete()) {
67-
throw new Exception('Job has not yet completed', 500);
68-
}
69-
});
70-
// check if the job has errors
71-
if (isset($job->info()['status']['errorResult'])) {
72-
$error = $job->info()['status']['errorResult']['message'];
73-
printf('Error running job: %s' . PHP_EOL, $error);
74-
} else {
75-
print('Data extracted successfully' . PHP_EOL);
76-
}
47+
$destinationUri = "gs://{$bucketName}/{$tableId}.json";
48+
// Define the format to use. If the format is not specified, 'CSV' will be used.
49+
$format = 'NEWLINE_DELIMITED_JSON';
50+
// Create the extract job
51+
$extractConfig = $table->extract($destinationUri)->destinationFormat($format);
52+
// Run the job
53+
$job = $table->runJob($extractConfig); // Waits for the job to complete
54+
printf('Exported %s to %s' . PHP_EOL, $table->id(), $destinationUri);
7755
# [END bigquery_extract_table]

bigquery/api/test/bigqueryTest.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ public function testCreateAndDeleteTable()
111111
$this->assertContains('Deleted table', $output);
112112
}
113113

114-
/**
115-
* @dataProvider provideExtractTable
116-
*/
117-
public function testExtractTable($objectName, $format)
114+
public function testExtractTable()
118115
{
119116
$bucketName = $this->requireEnv('GOOGLE_STORAGE_BUCKET');
120117
$tableId = $this->createTempTable();
@@ -123,18 +120,14 @@ public function testExtractTable($objectName, $format)
123120
$output = $this->runSnippet('extract_table', [
124121
self::$datasetId,
125122
$tableId,
126-
$bucketName,
127-
$objectName,
128-
$format,
123+
$bucketName
129124
]);
130125

131-
$this->assertContains('Data extracted successfully', $output);
132-
133126
// verify the contents of the bucket
134127
$storage = new StorageClient([
135128
'projectId' => self::$projectId,
136129
]);
137-
$object = $storage->bucket($bucketName)->object($objectName);
130+
$object = $storage->bucket($bucketName)->objects(['prefix' => $tableId])->current();
138131
$contents = $object->downloadAsString();
139132
$this->assertContains('Brent Shaffer', $contents);
140133
$this->assertContains('Takashi Matsuo', $contents);
@@ -143,16 +136,6 @@ public function testExtractTable($objectName, $format)
143136
$this->assertFalse($object->exists());
144137
}
145138

146-
public function provideExtractTable()
147-
{
148-
$time = time();
149-
150-
return [
151-
[sprintf('bigquery/test_data_%s.json', $time), 'json'],
152-
[sprintf('bigquery/test_data_%s.csv', $time), 'csv'],
153-
];
154-
}
155-
156139
public function testGetTable()
157140
{
158141
$projectId = self::$projectId;

0 commit comments

Comments
 (0)