Skip to content

Commit 804e524

Browse files
kamalaboulhosnsaranshdhingraanguillanneuf
authored
samples: create BigQuery subscription (GoogleCloudPlatform#1655)
* samples: create BigQuery subscription Co-authored-by: Saransh Dhingra Co-authored-by: Tianzi Cai
1 parent 4a579f7 commit 804e524

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

.kokoro/secrets-example.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export REDIS_PORT=
102102
# PubSub
103103
export GOOGLE_PUBSUB_SUBSCRIPTION=php-example-subscription
104104
export GOOGLE_PUBSUB_TOPIC=php-example-topic
105+
# GOOGLE_PUBSUB_BIGQUERY_TABLE excludes project_id
106+
# for example if table is ${PROJECT_ID}.pubsub_test_dataset.pubsub_test_table
107+
# the value of GOOGLE_PUBSUB_BIGQUERY_TABLE should be pubsub_test_dataset.pubsub_test_table
108+
export GOOGLE_PUBSUB_BIGQUERY_TABLE=
105109

106110
# Security Center
107111
export GOOGLE_ORGANIZATION_ID=

.kokoro/secrets.sh.enc

295 Bytes
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Copyright 2022 Google Inc.
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/blob/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_create_bigquery_subscription]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
use Google\Cloud\PubSub\V1\BigQueryConfig;
29+
30+
/**
31+
* Creates a Pub/Sub BigQuery subscription.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionName The Pub/Sub subscription name.
36+
* @param string $tableName The BigQuery table to which to write.
37+
*/
38+
function create_bigquery_subscription($projectId, $topicName, $subscriptionName, $table)
39+
{
40+
$pubsub = new PubSubClient([
41+
'projectId' => $projectId,
42+
]);
43+
$topic = $pubsub->topic($topicName);
44+
$subscription = $topic->subscription($subscriptionName);
45+
$config = new BigQueryConfig(['table' => $table]);
46+
$subscription->create([
47+
'bigqueryConfig' => $config
48+
]);
49+
50+
printf('Subscription created: %s' . PHP_EOL, $subscription->name());
51+
}
52+
# [END pubsub_create_bigquery_subscription]
53+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ public function testCreateAndDeletePushSubscription()
270270
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
271271
}
272272

273+
public function testCreateAndDeleteBigQuerySubscription()
274+
{
275+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
276+
$subscription = 'test-subscription-' . rand();
277+
$projectId = $this->requireEnv('GOOGLE_PROJECT_ID');
278+
$table = $projectId . '.' . $this->requireEnv('GOOGLE_PUBSUB_BIGQUERY_TABLE');
279+
280+
$output = $this->runFunctionSnippet('create_bigquery_subscription', [
281+
self::$projectId,
282+
$topic,
283+
$subscription,
284+
$table,
285+
]);
286+
287+
$this->assertRegExp('/Subscription created:/', $output);
288+
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
289+
290+
$output = $this->runFunctionSnippet('delete_subscription', [
291+
self::$projectId,
292+
$subscription,
293+
]);
294+
295+
$this->assertRegExp('/Subscription deleted:/', $output);
296+
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
297+
}
298+
273299
public function testCreateAndDetachSubscription()
274300
{
275301
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');

0 commit comments

Comments
 (0)