Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

Commit f3aec70

Browse files
samples: add samples for subscription with filtering (#1492)
* samples: add samples for subscription with filtering * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * samples: add sample filter string and fix test Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 073a1b8 commit f3aec70

5 files changed

Lines changed: 189 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree
128128
| Create Push Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createPushSubscription.js,samples/README.md) |
129129
| Create Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscription.js,samples/README.md) |
130130
| Create Subscription With Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithDeadLetterPolicy.js,samples/README.md) |
131+
| Create Subscription With Filtering | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithFiltering.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithFiltering.js,samples/README.md) |
131132
| Create Subscription with ordering enabled | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithOrdering.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithOrdering.js,samples/README.md) |
132133
| Create Topic | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createTopic.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createTopic.js,samples/README.md) |
133134
| Create Topic With Schema | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createTopicWithSchema.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createTopicWithSchema.js,samples/README.md) |

samples/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ guides.
2525
* [Create Push Subscription](#create-push-subscription)
2626
* [Create Subscription](#create-subscription)
2727
* [Create Subscription With Dead Letter Policy](#create-subscription-with-dead-letter-policy)
28+
* [Create Subscription With Filtering](#create-subscription-with-filtering)
2829
* [Create Subscription with ordering enabled](#create-subscription-with-ordering-enabled)
2930
* [Create Topic](#create-topic)
3031
* [Create Topic With Schema](#create-topic-with-schema)
@@ -178,6 +179,25 @@ __Usage:__
178179

179180

180181

182+
### Create Subscription With Filtering
183+
184+
Creates a new subscription with filtering.
185+
186+
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithFiltering.js).
187+
188+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithFiltering.js,samples/README.md)
189+
190+
__Usage:__
191+
192+
193+
`node createSubscriptionWithFiltering.js <topic-name-or-id> <subscription-name-or-id> <filter-string>`
194+
195+
196+
-----
197+
198+
199+
200+
181201
### Create Subscription with ordering enabled
182202

183203
Creates a new subscription with ordering enabled.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* This application demonstrates how to perform basic operations on
17+
* subscriptions with the Google Cloud Pub/Sub API.
18+
*
19+
* For more information, see the README.md under /pubsub and the documentation
20+
* at https://cloud.google.com/pubsub/docs.
21+
*/
22+
23+
// This is a generated sample. Please see typescript/README.md for more info.
24+
25+
'use strict';
26+
27+
// sample-metadata:
28+
// title: Create Subscription With Filtering
29+
// description: Creates a new subscription with filtering.
30+
// usage: node createSubscriptionWithFiltering.js <topic-name-or-id> <subscription-name-or-id> <filter-string>
31+
32+
// [START pubsub_create_subscription_with_filter]
33+
/**
34+
* TODO(developer): Uncomment these variables before running the sample.
35+
*/
36+
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
37+
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
38+
// const filterString = 'YOUR_FILTER_STRING'; // e.g. 'attributes.author="unknown"'
39+
40+
// Imports the Google Cloud client library
41+
const {PubSub} = require('@google-cloud/pubsub');
42+
43+
// Creates a client; cache this for further use
44+
const pubSubClient = new PubSub();
45+
46+
async function createSubscriptionWithFilter(
47+
topicNameOrId,
48+
subscriptionNameOrId,
49+
filterString
50+
) {
51+
// Creates a new subscription
52+
await pubSubClient
53+
.topic(topicNameOrId)
54+
.createSubscription(subscriptionNameOrId, {
55+
filter: filterString,
56+
});
57+
console.log(
58+
`Created subscription ${subscriptionNameOrId} with filter ${filterString}.`
59+
);
60+
}
61+
// [END pubsub_create_subscription_with_filter]
62+
63+
function main(
64+
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
65+
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
66+
filterString = 'YOUR_FILTER_STRING'
67+
) {
68+
createSubscriptionWithFilter(
69+
topicNameOrId,
70+
subscriptionNameOrId,
71+
filterString
72+
).catch(err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});
76+
}
77+
78+
main(...process.argv.slice(2));

samples/system-test/subscriptions.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('subscriptions', () => {
3333
const subscriptionNameSeven = `sub7-${runId}`;
3434
const subscriptionNameEight = `sub8-${runId}`;
3535
const subscriptionNameDetach = `testdetachsubsxyz-${runId}`;
36+
const subscriptionNameFiltered = `testfilteredsub-${runId}`;
3637
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
3738
const fullSubscriptionNameOne = `projects/${projectId}/subscriptions/${subscriptionNameOne}`;
3839
const fullSubscriptionNameTwo = `projects/${projectId}/subscriptions/${subscriptionNameTwo}`;
@@ -70,6 +71,21 @@ describe('subscriptions', () => {
7071
assert.strictEqual(subscriptions[0].name, fullSubscriptionNameOne);
7172
});
7273

74+
it('should create a subscription with filtering', async () => {
75+
const filter = 'attributes.author="unknown"';
76+
const output = execSync(
77+
`${commandFor(
78+
'createSubscriptionWithFiltering'
79+
)} ${topicNameOne} ${subscriptionNameFiltered} '${filter}'`
80+
);
81+
assert.include(
82+
output,
83+
`Created subscription ${subscriptionNameFiltered} with filter ${filter}`
84+
);
85+
const [subscriptions] = await pubsub.topic(topicNameOne).getSubscriptions();
86+
assert.strictEqual(subscriptions[0].name, fullSubscriptionNameOne);
87+
});
88+
7389
it('should create a push subscription', async () => {
7490
const output = execSync(
7591
`${commandFor(
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* This application demonstrates how to perform basic operations on
17+
* subscriptions with the Google Cloud Pub/Sub API.
18+
*
19+
* For more information, see the README.md under /pubsub and the documentation
20+
* at https://cloud.google.com/pubsub/docs.
21+
*/
22+
23+
// sample-metadata:
24+
// title: Create Subscription With Filtering
25+
// description: Creates a new subscription with filtering.
26+
// usage: node createSubscriptionWithFiltering.js <topic-name-or-id> <subscription-name-or-id> <filter-string>
27+
28+
// [START pubsub_create_subscription_with_filter]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
33+
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
34+
// const filterString = 'YOUR_FILTER_STRING'; // e.g. 'attributes.author="unknown"'
35+
36+
// Imports the Google Cloud client library
37+
import {PubSub} from '@google-cloud/pubsub';
38+
39+
// Creates a client; cache this for further use
40+
const pubSubClient = new PubSub();
41+
42+
async function createSubscriptionWithFilter(
43+
topicNameOrId: string,
44+
subscriptionNameOrId: string,
45+
filterString: string
46+
) {
47+
// Creates a new subscription
48+
await pubSubClient
49+
.topic(topicNameOrId)
50+
.createSubscription(subscriptionNameOrId, {
51+
filter: filterString,
52+
});
53+
console.log(
54+
`Created subscription ${subscriptionNameOrId} with filter ${filterString}.`
55+
);
56+
}
57+
// [END pubsub_create_subscription_with_filter]
58+
59+
function main(
60+
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
61+
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
62+
filterString = 'YOUR_FILTER_STRING'
63+
) {
64+
createSubscriptionWithFilter(
65+
topicNameOrId,
66+
subscriptionNameOrId,
67+
filterString
68+
).catch(err => {
69+
console.error(err.message);
70+
process.exitCode = 1;
71+
});
72+
}
73+
74+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)