-
Notifications
You must be signed in to change notification settings - Fork 100
samples: Schema evolution #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
3c6897c
samples: schema evolution
kamalaboulhosn 7ea48d9
samples: schema evolution
kamalaboulhosn 3438da5
Merge branch 'googleapis:main' into master
kamalaboulhosn 4e5c026
Format fixes
kamalaboulhosn 032a92f
Merge branch 'master' of https://github.com/kamalaboulhosn/java-pubsub
kamalaboulhosn 8a81408
Fix documentation for field.
kamalaboulhosn 90800ac
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] a88377c
Add back in working asserts
kamalaboulhosn 6a741ea
Merge branch 'master' of https://github.com/kamalaboulhosn/java-pubsub
kamalaboulhosn 9a07bbd
Formatting fixes
kamalaboulhosn df27d57
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7d1c561
Version/delete fixes
kamalaboulhosn d917065
Merge branch 'master' of https://github.com/kamalaboulhosn/java-pubsub
kamalaboulhosn 646afa7
samples: schema evolution
kamalaboulhosn e0cce5a
samples: schema evolution
kamalaboulhosn 51deeb0
Format fixes
kamalaboulhosn a53b564
Fix documentation for field.
kamalaboulhosn f1224e6
Add back in working asserts
kamalaboulhosn de55b05
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 503339f
Formatting fixes
kamalaboulhosn cbd910c
Version/delete fixes
kamalaboulhosn 7d15680
Merge branch 'master' of https://github.com/kamalaboulhosn/java-pubsub
kamalaboulhosn 1c7e840
samples: Schema evolution (#1499)
kamalaboulhosn f5d8b39
Merge branch 'googleapis:main' into master
kamalaboulhosn 6195b1c
Minor fixes for comments
kamalaboulhosn 24cc817
Merge branch 'master' of https://github.com/kamalaboulhosn/java-pubsub
kamalaboulhosn 18e9c3b
samples: Schema evolution (#1499)
kamalaboulhosn 37a35cb
Merge branch 'schema-evolution-examples' of https://github.com/google…
kamalaboulhosn 79b0514
Merge branch 'schema-evolution-examples' into master
kamalaboulhosn b1d2309
Fix rollback example
kamalaboulhosn 3f74191
Formatting
kamalaboulhosn 8b1288a
Formatting and wording fixes
kamalaboulhosn 5057f17
Add new schemas to test directory
kamalaboulhosn ba38121
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
samples/snippets/src/main/java/pubsub/CommitAvroSchemaExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package pubsub; | ||
|
|
||
| // [START pubsub_commit_avro_schema] | ||
|
|
||
| import com.google.api.gax.rpc.AlreadyExistsException; | ||
| import com.google.cloud.pubsub.v1.SchemaServiceClient; | ||
| import com.google.pubsub.v1.ProjectName; | ||
| import com.google.pubsub.v1.Schema; | ||
| import com.google.pubsub.v1.SchemaName; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
|
|
||
| public class CommitAvroSchemaExample { | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String schemaId = "your-schema-id"; | ||
| String avscFile = "path/to/an/avro/schema/file/(.avsc)/formatted/in/json"; | ||
|
|
||
| commitAvroSchemaExample(projectId, schemaId, avscFile); | ||
| } | ||
|
|
||
| public static Schema commitAvroSchemaExample(String projectId, String schemaId, String avscFile) | ||
| throws IOException { | ||
|
|
||
| ProjectName projectName = ProjectName.of(projectId); | ||
| SchemaName schemaName = SchemaName.of(projectId, schemaId); | ||
|
|
||
| // Read an Avro schema file formatted in JSON as a string. | ||
| String avscSource = new String(Files.readAllBytes(Paths.get(avscFile))); | ||
|
|
||
| try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) { | ||
|
|
||
| Schema schema = | ||
| schemaServiceClient.commitSchema( | ||
| schemaName.toString(), | ||
| Schema.newBuilder() | ||
| .setName(schemaName.toString()) | ||
| .setType(Schema.Type.AVRO) | ||
| .setDefinition(avscSource) | ||
| .build()); | ||
|
|
||
| System.out.println("Committed a schema using an Avro schema:\n" + schema); | ||
| return schema; | ||
| } catch (AlreadyExistsException e) { | ||
| System.out.println(schemaName + "already exists."); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_commit_avro_schema] | ||
69 changes: 69 additions & 0 deletions
69
samples/snippets/src/main/java/pubsub/CommitProtoSchemaExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package pubsub; | ||
|
|
||
| // [START pubsub_commit_proto_schema] | ||
|
|
||
| import com.google.api.gax.rpc.AlreadyExistsException; | ||
| import com.google.cloud.pubsub.v1.SchemaServiceClient; | ||
| import com.google.pubsub.v1.ProjectName; | ||
| import com.google.pubsub.v1.Schema; | ||
| import com.google.pubsub.v1.SchemaName; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
|
|
||
| public class CommitProtoSchemaExample { | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String schemaId = "your-schema-id"; | ||
| String protoFile = "path/to/a/proto/file/(.proto)/formatted/in/protocol/buffers"; | ||
|
|
||
| commitProtoSchemaExample(projectId, schemaId, protoFile); | ||
| } | ||
|
|
||
| public static Schema commitProtoSchemaExample(String projectId, String schemaId, String protoFile) | ||
| throws IOException { | ||
|
|
||
| ProjectName projectName = ProjectName.of(projectId); | ||
| SchemaName schemaName = SchemaName.of(projectId, schemaId); | ||
|
|
||
| // Read a proto file as a string. | ||
| String protoSource = new String(Files.readAllBytes(Paths.get(protoFile))); | ||
|
|
||
| try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) { | ||
|
|
||
| Schema schema = | ||
| schemaServiceClient.commitSchema( | ||
| schemaName.toString(), | ||
| Schema.newBuilder() | ||
| .setName(schemaName.toString()) | ||
| .setType(Schema.Type.PROTOCOL_BUFFER) | ||
| .setDefinition(protoSource) | ||
| .build()); | ||
|
|
||
| System.out.println("Committed a schema using a protobuf schema:\n" + schema); | ||
| return schema; | ||
| } catch (AlreadyExistsException e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| System.out.println(schemaName + "already exists."); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_commit_proto_schema] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
samples/snippets/src/main/java/pubsub/CreateTopicWithSchemaRevisionsExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package pubsub; | ||
|
|
||
| // [START pubsub_create_topic_with_schema_revisions] | ||
|
|
||
| import com.google.api.gax.rpc.AlreadyExistsException; | ||
| import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
| import com.google.pubsub.v1.Encoding; | ||
| import com.google.pubsub.v1.SchemaName; | ||
| import com.google.pubsub.v1.SchemaSettings; | ||
| import com.google.pubsub.v1.Topic; | ||
| import com.google.pubsub.v1.TopicName; | ||
| import java.io.IOException; | ||
|
|
||
| public class CreateTopicWithSchemaRevisionsExample { | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String topicId = "your-topic-id"; | ||
| // Use an existing schema. | ||
| String schemaId = "your-schema-id"; | ||
| // Choose either BINARY or JSON message serialization in this topic. | ||
| Encoding encoding = Encoding.BINARY; | ||
| // Set the minimum and maximum revsion ID | ||
| String firstRevisionId = "your-revision-id"; | ||
| String lastRevisionId = "your-revision-id"; | ||
|
|
||
| createTopicWithSchemaRevisionsExample( | ||
| projectId, topicId, schemaId, firstRevisionId, lastRevisionId, encoding); | ||
| } | ||
|
|
||
| public static void createTopicWithSchemaRevisionsExample( | ||
| String projectId, | ||
| String topicId, | ||
| String schemaId, | ||
| String firstRevisionid, | ||
| String lastRevisionId, | ||
| Encoding encoding) | ||
| throws IOException { | ||
| TopicName topicName = TopicName.of(projectId, topicId); | ||
| SchemaName schemaName = SchemaName.of(projectId, schemaId); | ||
|
|
||
| SchemaSettings schemaSettings = | ||
| SchemaSettings.newBuilder() | ||
| .setSchema(schemaName.toString()) | ||
| .setFirstRevisionId(firstRevisionid) | ||
| .setLastRevisionId(lastRevisionId) | ||
| .setEncoding(encoding) | ||
| .build(); | ||
|
|
||
| try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { | ||
|
|
||
| Topic topic = | ||
| topicAdminClient.createTopic( | ||
| Topic.newBuilder() | ||
| .setName(topicName.toString()) | ||
| .setSchemaSettings(schemaSettings) | ||
| .build()); | ||
|
|
||
| System.out.println("Created topic with schema: " + topic.getName()); | ||
| } catch (AlreadyExistsException e) { | ||
| System.out.println(schemaName + "already exists."); | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_create_topic_with_schema_revisions] |
55 changes: 55 additions & 0 deletions
55
samples/snippets/src/main/java/pubsub/DeleteSchemaRevisionExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package pubsub; | ||
|
|
||
| // [START pubsub_delete_schema_revision] | ||
|
|
||
| import com.google.api.gax.rpc.NotFoundException; | ||
| import com.google.cloud.pubsub.v1.SchemaServiceClient; | ||
| import com.google.pubsub.v1.DeleteSchemaRevisionRequest; | ||
| import com.google.pubsub.v1.SchemaName; | ||
| import java.io.IOException; | ||
|
|
||
| public class DeleteSchemaRevisionExample { | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String schemaId = "your-schema-id@your-revision-id"; | ||
|
|
||
| deleteSchemaRevisionExample(projectId, schemaId); | ||
| } | ||
|
|
||
| public static void deleteSchemaRevisionExample(String projectId, String schemaId) | ||
| throws IOException { | ||
| SchemaName schemaName = SchemaName.of(projectId, schemaId); | ||
|
|
||
| try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) { | ||
|
|
||
| DeleteSchemaRevisionRequest request = | ||
| DeleteSchemaRevisionRequest.newBuilder().setName(schemaName.toString()).build(); | ||
|
|
||
| schemaServiceClient.deleteSchemaRevision(request); | ||
|
|
||
| System.out.println("Deleted a schema revision:" + schemaName); | ||
|
|
||
| } catch (NotFoundException e) { | ||
| System.out.println(schemaName + "not found."); | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_delete_schema_revision] |
51 changes: 51 additions & 0 deletions
51
samples/snippets/src/main/java/pubsub/GetSchemaRevisionExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package pubsub; | ||
|
|
||
| // [START pubsub_get_schema_revision] | ||
|
|
||
| import com.google.api.gax.rpc.NotFoundException; | ||
| import com.google.cloud.pubsub.v1.SchemaServiceClient; | ||
| import com.google.pubsub.v1.Schema; | ||
| import com.google.pubsub.v1.SchemaName; | ||
| import java.io.IOException; | ||
|
|
||
| public class GetSchemaRevisionExample { | ||
|
|
||
| public static void main(String... args) throws Exception { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "your-project-id"; | ||
| String schemaId = "your-schema-id[@your-schema-revision]"; | ||
| getSchemaRevisionExample(projectId, schemaId); | ||
| } | ||
|
|
||
| public static void getSchemaRevisionExample(String projectId, String schemaId) | ||
| throws IOException { | ||
| SchemaName schemaName = SchemaName.of(projectId, schemaId); | ||
|
|
||
| try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) { | ||
|
|
||
| Schema schema = schemaServiceClient.getSchema(schemaName); | ||
|
|
||
| System.out.println("Got a schema:\n" + schema); | ||
|
|
||
| } catch (NotFoundException e) { | ||
| System.out.println(schemaName + "not found."); | ||
| } | ||
| } | ||
| } | ||
| // [END pubsub_get_schema_revision] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably change to FailedPrecondition/InvalidArgument/NotFound/ResourceExhausted, since we can't get AlreadyExists.