Skip to content

Commit

Permalink
samples: Schema evolution (#1499)
Browse files Browse the repository at this point in the history
* samples: schema evolution

* samples: schema evolution

* Format fixes

* Fix documentation for field.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Add back in working asserts

* Formatting fixes

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Version/delete fixes

* samples: schema evolution

* samples: schema evolution

* Format fixes

* Fix documentation for field.

* Add back in working asserts

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* Formatting fixes

* Version/delete fixes

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
kamalaboulhosn and gcf-owl-bot[bot] committed Feb 23, 2023
1 parent 94fa8ab commit 18e9c3b
Show file tree
Hide file tree
Showing 15 changed files with 835 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.md

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions samples/snippets/src/main/java/pubsub/CommitAvroSchemaExample.java
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]
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) {
System.out.println(schemaName + "already exists.");
return null;
}
}
}
// [END pubsub_commit_proto_schema]
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void main(String... args) throws Exception {
createAvroSchemaExample(projectId, schemaId, avscFile);
}

public static void createAvroSchemaExample(String projectId, String schemaId, String avscFile)
public static Schema createAvroSchemaExample(String projectId, String schemaId, String avscFile)
throws IOException {

ProjectName projectName = ProjectName.of(projectId);
Expand All @@ -60,8 +60,10 @@ public static void createAvroSchemaExample(String projectId, String schemaId, St
schemaId);

System.out.println("Created a schema using an Avro schema:\n" + schema);
return schema;
} catch (AlreadyExistsException e) {
System.out.println(schemaName + "already exists.");
return null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void main(String... args) throws Exception {
createProtoSchemaExample(projectId, schemaId, protoFile);
}

public static void createProtoSchemaExample(String projectId, String schemaId, String protoFile)
public static Schema createProtoSchemaExample(String projectId, String schemaId, String protoFile)
throws IOException {

ProjectName projectName = ProjectName.of(projectId);
Expand All @@ -60,8 +60,10 @@ public static void createProtoSchemaExample(String projectId, String schemaId, S
schemaId);

System.out.println("Created a schema using a protobuf schema:\n" + schema);
return schema;
} catch (AlreadyExistsException e) {
System.out.println(schemaName + "already exists.");
return null;
}
}
}
Expand Down
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]
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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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]
Loading

0 comments on commit 18e9c3b

Please sign in to comment.