Skip to content

Commit 6b1b759

Browse files
samples: add schema samples (#509)
* samples: add three schema samples * address kamal's comments * add avro-tools generated class State * add 4 more examples * remove validate schema sample * place common code outside case switch body * add subscribe with avro schema example * move generated class under utitlies * add proto file and generated class * add comment about topic encoding * apply kamal's suggestions * add publish and subscribe proto messages examples * update comment * add resources to install-without-bom and snapshots * Fix schema integration test and avro example * add resources to install-without-bom and snapshots * fix file path * lint * lint: add checkstyle-suppressions.xml * change checkstyle working directory * address kamal's comment about try catch * address kamal's comment about method naming * update assert * address kolea2's comments Co-authored-by: Kamal Aboul-Hosn <kamal.aboulhosn@gmail.com>
1 parent 1d853f7 commit 6b1b759

28 files changed

Lines changed: 2379 additions & 10 deletions

java-pubsub/.github/sync-repo-settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ permissionRules:
4646
- team: yoshi-java-admins
4747
permission: admin
4848
- team: yoshi-java
49-
permission: push
49+
permission: push

java-pubsub/.github/workflows/samples.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ jobs:
1111
java-version: 8
1212
- name: Run checkstyle
1313
run: mvn -P lint --quiet --batch-mode checkstyle:check
14-
working-directory: samples/snippets
14+
working-directory: samples
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0"?>
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+
<!DOCTYPE suppressions PUBLIC
19+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
20+
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
21+
22+
<suppressions>
23+
<suppress checks="EmptyLineSeparatorCheck" files="StateProto.java|State.java"/>
24+
<suppress checks="IndentationCheck" files="StateProto.java"/>
25+
<suppress checks="LeftCurlyCheck" files="StateProto.java"/>
26+
<suppress checks="MemberNameCheck" files="StateProto.java|State.java"/>
27+
<suppress checks="NeedBracesCheck" files="StateProto.java"/>
28+
<suppress checks="OverloadMethodsDeclarationOrderCheck" files="StateProto.java"/>
29+
<suppress checks="EmptyBlockCheck" files="StateProto.java"/>
30+
<suppress checks="WhitespaceAroundCheck" files="StateProto.java"/>
31+
<suppress checks="IllegalTokenTextCheck" files="StateProto.java"/>
32+
<suppress checks="ParameterNameCheck" files="State.java"/>
33+
<suppress checks="LineLengthCheck" files="State.java"/>
34+
</suppressions>

java-pubsub/samples/install-without-bom/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848
</dependency>
4949
<!-- [END pubsub_install_without_bom] -->
5050

51+
<dependency>
52+
<groupId>org.apache.avro</groupId>
53+
<artifactId>avro</artifactId>
54+
<version>1.10.1</version>
55+
<scope>compile</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.xerial.snappy</groupId>
59+
<artifactId>snappy-java</artifactId>
60+
<version>1.1.8.4</version>
61+
</dependency>
62+
5163
<dependency>
5264
<groupId>junit</groupId>
5365
<artifactId>junit</artifactId>
@@ -60,6 +72,11 @@
6072
<version>1.1.2</version>
6173
<scope>test</scope>
6274
</dependency>
75+
<dependency>
76+
<groupId>com.google.protobuf</groupId>
77+
<artifactId>protobuf-java-util</artifactId>
78+
<version>3.14.0</version>
79+
</dependency>
6380
</dependencies>
6481

6582
<!-- compile and run all snippet tests -->
@@ -81,6 +98,19 @@
8198
</sources>
8299
</configuration>
83100
</execution>
101+
<execution>
102+
<id>add-snippets-resources</id>
103+
<goals>
104+
<goal>add-resource</goal>
105+
</goals>
106+
<configuration>
107+
<resources>
108+
<resource>
109+
<directory>../snippets/src/main/resources</directory>
110+
</resource>
111+
</resources>
112+
</configuration>
113+
</execution>
84114
<execution>
85115
<id>add-snippets-tests</id>
86116
<goals>
@@ -92,6 +122,19 @@
92122
</sources>
93123
</configuration>
94124
</execution>
125+
<execution>
126+
<id>add-snippets-tests-resource</id>
127+
<goals>
128+
<goal>add-test-resource</goal>
129+
</goals>
130+
<configuration>
131+
<resources>
132+
<resource>
133+
<directory>../snippets/src/test/resources</directory>>
134+
</resource>
135+
</resources>
136+
</configuration>
137+
</execution>
95138
</executions>
96139
</plugin>
97140
</plugins>

java-pubsub/samples/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@
6868
</plugin>
6969
</plugins>
7070
</build>
71+
72+
<reporting>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-checkstyle-plugin</artifactId>
77+
<version>3.1.2</version>
78+
<configuration>
79+
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</reporting>
7184
</project>

java-pubsub/samples/snapshot/pom.xml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@
4747
</dependency>
4848
<!-- {x-version-update-end} -->
4949

50+
<dependency>
51+
<groupId>org.apache.avro</groupId>
52+
<artifactId>avro</artifactId>
53+
<version>1.10.1</version>
54+
<scope>compile</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.xerial.snappy</groupId>
58+
<artifactId>snappy-java</artifactId>
59+
<version>1.1.8.4</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.protobuf</groupId>
63+
<artifactId>protobuf-java-util</artifactId>
64+
<version>3.14.0</version>
65+
</dependency>
66+
5067
<dependency>
5168
<groupId>junit</groupId>
5269
<artifactId>junit</artifactId>
@@ -80,6 +97,19 @@
8097
</sources>
8198
</configuration>
8299
</execution>
100+
<execution>
101+
<id>add-snippets-resources</id>
102+
<goals>
103+
<goal>add-resource</goal>
104+
</goals>
105+
<configuration>
106+
<resources>
107+
<resource>
108+
<directory>../snippets/src/main/resources</directory>
109+
</resource>
110+
</resources>
111+
</configuration>
112+
</execution>
83113
<execution>
84114
<id>add-snippets-tests</id>
85115
<goals>
@@ -91,8 +121,21 @@
91121
</sources>
92122
</configuration>
93123
</execution>
124+
<execution>
125+
<id>add-snippets-tests-resource</id>
126+
<goals>
127+
<goal>add-test-resource</goal>
128+
</goals>
129+
<configuration>
130+
<resources>
131+
<resource>
132+
<directory>../snippets/src/test/resources</directory>>
133+
</resource>
134+
</resources>
135+
</configuration>
136+
</execution>
94137
</executions>
95138
</plugin>
96139
</plugins>
97140
</build>
98-
</project>
141+
</project>

java-pubsub/samples/snippets/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,26 @@
5656
<dependency>
5757
<groupId>com.google.cloud</groupId>
5858
<artifactId>google-cloud-pubsub</artifactId>
59+
<version>1.111.0</version>
5960
</dependency>
6061
<!-- [END pubsub_install_with_bom] -->
6162

63+
<dependency>
64+
<groupId>org.apache.avro</groupId>
65+
<artifactId>avro</artifactId>
66+
<version>1.10.1</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.xerial.snappy</groupId>
70+
<artifactId>snappy-java</artifactId>
71+
<version>1.1.8.4</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.protobuf</groupId>
75+
<artifactId>protobuf-java-util</artifactId>
76+
<version>3.14.0</version>
77+
</dependency>
78+
6279
<dependency>
6380
<groupId>junit</groupId>
6481
<artifactId>junit</artifactId>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pubsub;
18+
19+
// [START pubsub_create_avro_schema]
20+
21+
import com.google.api.gax.rpc.AlreadyExistsException;
22+
import com.google.cloud.pubsub.v1.SchemaServiceClient;
23+
import com.google.pubsub.v1.ProjectName;
24+
import com.google.pubsub.v1.Schema;
25+
import com.google.pubsub.v1.SchemaName;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
29+
30+
public class CreateAvroSchemaExample {
31+
32+
public static void main(String... args) throws Exception {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String schemaId = "your-schema-id";
36+
String avscFile = "path/to/an/avro/schema/file/(.avsc)/formatted/in/json";
37+
38+
createAvroSchemaExample(projectId, schemaId, avscFile);
39+
}
40+
41+
public static void createAvroSchemaExample(String projectId, String schemaId, String avscFile)
42+
throws IOException {
43+
44+
ProjectName projectName = ProjectName.of(projectId);
45+
SchemaName schemaName = SchemaName.of(projectId, schemaId);
46+
47+
// Read an Avro schema file formatted in JSON as a string.
48+
String avscSource = new String(Files.readAllBytes(Paths.get(avscFile)));
49+
50+
try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
51+
52+
Schema schema =
53+
schemaServiceClient.createSchema(
54+
projectName,
55+
Schema.newBuilder()
56+
.setName(schemaName.toString())
57+
.setType(com.google.pubsub.v1.Schema.Type.AVRO)
58+
.setDefinition(avscSource)
59+
.build(),
60+
schemaId);
61+
62+
System.out.println("Created a schema using an Avro schema:\n" + schema);
63+
} catch (AlreadyExistsException e) {
64+
System.out.println(schemaName + "already exists.");
65+
}
66+
}
67+
}
68+
// [END pubsub_create_avro_schema]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pubsub;
18+
19+
// [START pubsub_create_proto_schema]
20+
21+
import com.google.api.gax.rpc.AlreadyExistsException;
22+
import com.google.cloud.pubsub.v1.SchemaServiceClient;
23+
import com.google.pubsub.v1.ProjectName;
24+
import com.google.pubsub.v1.Schema;
25+
import com.google.pubsub.v1.SchemaName;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
29+
30+
public class CreateProtoSchemaExample {
31+
32+
public static void main(String... args) throws Exception {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "your-project-id";
35+
String schemaId = "your-schema-id";
36+
String protoFile = "path/to/a/proto/file/(.proto)/formatted/in/protocol/buffers";
37+
38+
createProtoSchemaExample(projectId, schemaId, protoFile);
39+
}
40+
41+
public static void createProtoSchemaExample(String projectId, String schemaId, String protoFile)
42+
throws IOException {
43+
44+
ProjectName projectName = ProjectName.of(projectId);
45+
SchemaName schemaName = SchemaName.of(projectId, schemaId);
46+
47+
// Read a proto file as a string.
48+
String protoSource = new String(Files.readAllBytes(Paths.get(protoFile)));
49+
50+
try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
51+
52+
Schema schema =
53+
schemaServiceClient.createSchema(
54+
projectName,
55+
Schema.newBuilder()
56+
.setName(schemaName.toString())
57+
.setType(Schema.Type.PROTOCOL_BUFFER)
58+
.setDefinition(protoSource)
59+
.build(),
60+
schemaId);
61+
62+
System.out.println("Created a schema using a protobuf schema:\n" + schema);
63+
} catch (AlreadyExistsException e) {
64+
System.out.println(schemaName + "already exists.");
65+
}
66+
}
67+
}
68+
// [END pubsub_create_proto_schema]

java-pubsub/samples/snippets/src/main/java/pubsub/CreateTopicExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,3 @@ public static void createTopicExample(String projectId, String topicId) throws I
4343
}
4444
// [END pubsub_create_topic]
4545
// [END pubsub_quickstart_create_topic]
46-

0 commit comments

Comments
 (0)