Skip to content

Commit c4e3584

Browse files
authored
feat: add Pub/Sub schema samples (GoogleCloudPlatform#1357)
1 parent 5c4e662 commit c4e3584

20 files changed

+1079
-8
lines changed

pubsub/api/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-pubsub": "^1.29"
3+
"google/cloud-pubsub": "^1.31",
4+
"wikimedia/avro": "^1.9"
45
}
56
}

pubsub/api/src/create_avro_schema.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Copyright 2021 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/blob/master/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_create_avro_schema]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
use Google\Cloud\PubSub\V1\Schema\Type;
28+
29+
/**
30+
* Create a Schema with an AVRO definition.
31+
*
32+
* @param string $projectId
33+
* @param string $schemaId
34+
* @param string $avscFile
35+
*/
36+
function create_avro_schema($projectId, $schemaId, $avscFile)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
42+
$definition = file_get_contents($avscFile);
43+
$schema = $pubsub->createSchema($schemaId, Type::AVRO, $definition);
44+
45+
printf('Schema %s created.', $schema->name());
46+
}
47+
# [END pubsub_create_avro_schema]
48+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Copyright 2021 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/blob/master/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_create_proto_schema]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
use Google\Cloud\PubSub\V1\Schema\Type;
28+
29+
/**
30+
* Create a Schema with an Protocol Buffer definition.
31+
*
32+
* @param string $projectId
33+
* @param string $schemaId
34+
* @param string $protoFile
35+
*/
36+
function create_proto_schema($projectId, $schemaId, $protoFile)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
42+
$definition = file_get_contents($protoFile);
43+
$schema = $pubsub->createSchema($schemaId, Type::PROTOCOL_BUFFER, $definition);
44+
45+
printf('Schema %s created.', $schema->name());
46+
}
47+
# [END pubsub_create_proto_schema]
48+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
49+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2021 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/blob/master/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_create_topic_with_schema]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
use Google\Cloud\PubSub\Schema;
28+
29+
/**
30+
* Create a topic with a schema.
31+
*
32+
* @param string $projectId
33+
* @param string $topicId
34+
* @param string $schemaId
35+
* @param string $encoding
36+
*/
37+
function create_topic_with_schema($projectId, $topicId, $schemaId, $encoding)
38+
{
39+
$pubsub = new PubSubClient([
40+
'projectId' => $projectId,
41+
]);
42+
43+
$schema = $pubsub->schema($schemaId);
44+
45+
$topic = $pubsub->createTopic($topicId, [
46+
'schemaSettings' => [
47+
// The schema may be provided as an instance of the schema type,
48+
// or by using the schema ID directly.
49+
'schema' => $schema,
50+
// Encoding may be either `BINARY` or `JSON`.
51+
// Provide a string or a constant from Google\Cloud\PubSub\V1\Encoding.
52+
'encoding' => $encoding,
53+
]
54+
]);
55+
56+
printf('Topic %s created', $topic->name());
57+
}
58+
# [END pubsub_create_topic_with_schema]
59+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
60+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/src/data/generated/Metadata.php

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubsub/api/src/data/generated/StateProto.php

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubsub/api/src/data/us-states.avsc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"type": "record",
3+
"name": "State",
4+
"namespace": "utilities",
5+
"doc": "A list of states in the United States of America.",
6+
"fields": [
7+
{
8+
"name": "name",
9+
"type": "string",
10+
"doc": "The common name of the state."
11+
},
12+
{
13+
"name": "post_abbr",
14+
"type": "string",
15+
"doc": "The postal code abbreviation of the state."
16+
}
17+
]
18+
}

pubsub/api/src/data/us-states.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
package utilities;
4+
5+
message StateProto {
6+
string name = 1;
7+
string post_abbr = 2;
8+
}

pubsub/api/src/delete_schema.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2021 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/blob/master/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_delete_schema]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
28+
/**
29+
* Delete a schema.
30+
*
31+
* @param string $projectId
32+
* @param string $schemaId
33+
*/
34+
function delete_schema($projectId, $schemaId)
35+
{
36+
$pubsub = new PubSubClient([
37+
'projectId' => $projectId,
38+
]);
39+
40+
$schema = $pubsub->schema($schemaId);
41+
42+
if ($schema->exists()) {
43+
$schema->delete();
44+
45+
printf('Schema %s deleted.', $schema->name());
46+
} else {
47+
printf('Schema %s does not exist.', $schema->name());
48+
}
49+
}
50+
# [END pubsub_delete_schema]
51+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
52+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)