Skip to content

Commit 5243d69

Browse files
chore: add sample demonstrating sending a nested proto (googleapis#2945)
1 parent b828283 commit 5243d69

File tree

7 files changed

+343
-0
lines changed

7 files changed

+343
-0
lines changed

samples/install-without-bom/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</parent>
1919

2020
<properties>
21+
<project.protobuf-java.version>3.25.4</project.protobuf-java.version>
2122
<maven.compiler.target>1.8</maven.compiler.target>
2223
<maven.compiler.source>1.8</maven.compiler.source>
2324
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -64,6 +65,11 @@
6465
<artifactId>exporter-metrics</artifactId>
6566
<version>0.34.0</version>
6667
</dependency>
68+
<dependency>
69+
<groupId>com.google.protobuf</groupId>
70+
<artifactId>protobuf-java-util</artifactId>
71+
<version>${project.protobuf-java.version}</version>
72+
</dependency>
6773

6874
<dependency>
6975
<groupId>junit</groupId>
@@ -81,6 +87,13 @@
8187

8288
<!-- compile and run all snippet tests -->
8389
<build>
90+
<extensions>
91+
<extension>
92+
<groupId>kr.motd.maven</groupId>
93+
<artifactId>os-maven-plugin</artifactId>
94+
<version>1.7.1</version>
95+
</extension>
96+
</extensions>
8497
<plugins>
8598
<plugin>
8699
<groupId>org.codehaus.mojo</groupId>
@@ -95,6 +108,7 @@
95108
<configuration>
96109
<sources>
97110
<source>../snippets/src/main/java</source>
111+
<source>../snippets/src/main/proto</source>
98112
</sources>
99113
</configuration>
100114
</execution>
@@ -111,6 +125,22 @@
111125
</execution>
112126
</executions>
113127
</plugin>
128+
<plugin>
129+
<groupId>org.xolstice.maven.plugins</groupId>
130+
<artifactId>protobuf-maven-plugin</artifactId>
131+
<version>0.6.1</version>
132+
<configuration>
133+
<protocArtifact>com.google.protobuf:protoc:${project.protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
134+
<protoSourceRoot>../snippets/src/main/proto</protoSourceRoot>
135+
</configuration>
136+
<executions>
137+
<execution>
138+
<goals>
139+
<goal>compile</goal>
140+
</goals>
141+
</execution>
142+
</executions>
143+
</plugin>
114144
</plugins>
115145
</build>
116146
</project>

samples/snapshot/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</parent>
1919

2020
<properties>
21+
<project.protobuf-java.version>3.25.4</project.protobuf-java.version>
2122
<maven.compiler.target>1.8</maven.compiler.target>
2223
<maven.compiler.source>1.8</maven.compiler.source>
2324
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -66,6 +67,12 @@
6667
<version>0.34.0</version>
6768
</dependency>
6869

70+
<dependency>
71+
<groupId>com.google.protobuf</groupId>
72+
<artifactId>protobuf-java-util</artifactId>
73+
<version>${project.protobuf-java.version}</version>
74+
</dependency>
75+
6976
<dependency>
7077
<groupId>junit</groupId>
7178
<artifactId>junit</artifactId>
@@ -82,6 +89,13 @@
8289

8390
<!-- compile and run all snippet tests -->
8491
<build>
92+
<extensions>
93+
<extension>
94+
<groupId>kr.motd.maven</groupId>
95+
<artifactId>os-maven-plugin</artifactId>
96+
<version>1.7.1</version>
97+
</extension>
98+
</extensions>
8599
<plugins>
86100
<plugin>
87101
<groupId>org.codehaus.mojo</groupId>
@@ -96,6 +110,7 @@
96110
<configuration>
97111
<sources>
98112
<source>../snippets/src/main/java</source>
113+
<source>../snippets/src/main/proto</source>
99114
</sources>
100115
</configuration>
101116
</execution>
@@ -112,6 +127,22 @@
112127
</execution>
113128
</executions>
114129
</plugin>
130+
<plugin>
131+
<groupId>org.xolstice.maven.plugins</groupId>
132+
<artifactId>protobuf-maven-plugin</artifactId>
133+
<version>0.6.1</version>
134+
<configuration>
135+
<protocArtifact>com.google.protobuf:protoc:${project.protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
136+
<protoSourceRoot>../snippets/src/main/proto</protoSourceRoot>
137+
</configuration>
138+
<executions>
139+
<execution>
140+
<goals>
141+
<goal>compile</goal>
142+
</goals>
143+
</execution>
144+
</executions>
145+
</plugin>
115146
</plugins>
116147
</build>
117148
</project>

samples/snippets/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</parent>
1919

2020
<properties>
21+
<project.protobuf-java.version>3.25.4</project.protobuf-java.version>
2122
<maven.compiler.target>1.8</maven.compiler.target>
2223
<maven.compiler.source>1.8</maven.compiler.source>
2324
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -81,6 +82,11 @@
8182
<artifactId>exporter-metrics</artifactId>
8283
<version>0.34.0</version>
8384
</dependency>
85+
<dependency>
86+
<groupId>com.google.protobuf</groupId>
87+
<artifactId>protobuf-java-util</artifactId>
88+
<version>${project.protobuf-java.version}</version>
89+
</dependency>
8490

8591

8692
<!-- Test Dependencies -->
@@ -97,4 +103,31 @@
97103
<scope>test</scope>
98104
</dependency>
99105
</dependencies>
106+
107+
<build>
108+
<extensions>
109+
<extension>
110+
<groupId>kr.motd.maven</groupId>
111+
<artifactId>os-maven-plugin</artifactId>
112+
<version>1.7.1</version>
113+
</extension>
114+
</extensions>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.xolstice.maven.plugins</groupId>
118+
<artifactId>protobuf-maven-plugin</artifactId>
119+
<version>0.6.1</version>
120+
<configuration>
121+
<protocArtifact>com.google.protobuf:protoc:${project.protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
122+
</configuration>
123+
<executions>
124+
<execution>
125+
<goals>
126+
<goal>compile</goal>
127+
</goals>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
</plugins>
132+
</build>
100133
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2025 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 com.example.bigquerystorage;
18+
19+
// [START bigquerystorage_writenestedproto]
20+
import com.google.api.core.ApiFuture;
21+
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse;
22+
import com.google.cloud.bigquery.storage.v1.ProtoRows;
23+
import com.google.cloud.bigquery.storage.v1.ProtoSchemaConverter;
24+
import com.google.cloud.bigquery.storage.v1.StreamWriter;
25+
import com.google.protobuf.Descriptors.DescriptorValidationException;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
public class WriteNestedProto {
30+
31+
public static void runWriteNestedProto(String projectId, String datasetName, String tableName)
32+
throws DescriptorValidationException, InterruptedException, IOException {
33+
StreamWriter streamWriter =
34+
StreamWriter.newBuilder(
35+
"projects/"
36+
+ projectId
37+
+ "/datasets/"
38+
+ datasetName
39+
+ "/tables/"
40+
+ tableName
41+
+ "/_default")
42+
.setWriterSchema(ProtoSchemaConverter.convert(HasNestedMessage.getDescriptor()))
43+
.build();
44+
ProtoRows protoRows =
45+
ProtoRows.newBuilder()
46+
.addSerializedRows(
47+
HasNestedMessage.newBuilder()
48+
.setFoo("foo")
49+
.setBar(
50+
HasNestedMessage.InnerMessage.newBuilder()
51+
.setMyInt(12345)
52+
.setMyString("bar")
53+
.build())
54+
.build()
55+
.toByteString())
56+
.addSerializedRows(
57+
HasSeparateNestedMessage.newBuilder()
58+
.setFoo("foo2")
59+
.setBar(
60+
SeparateMessage.newBuilder().setMyInt(123456).setMyString("bar2").build())
61+
.build()
62+
.toByteString())
63+
.build();
64+
ApiFuture<AppendRowsResponse> future = streamWriter.append(protoRows);
65+
try {
66+
AppendRowsResponse response = future.get();
67+
System.out.println("Appended records successfully.");
68+
} catch (ExecutionException e) {
69+
System.out.println(e);
70+
}
71+
}
72+
}
73+
// [END bigquerystorage_writenestedproto]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 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+
syntax = "proto3";
18+
package nestedprotos;
19+
20+
import "separate.proto";
21+
22+
option java_multiple_files = true;
23+
option java_package = "com.example.bigquerystorage";
24+
option java_outer_classname = "NestedProtos";
25+
26+
message HasNestedMessage {
27+
optional string foo = 1;
28+
29+
message InnerMessage {
30+
optional int64 my_int = 1;
31+
optional string my_string = 2;
32+
}
33+
34+
optional InnerMessage bar = 2;
35+
}
36+
37+
message HasSeparateNestedMessage {
38+
optional string foo = 1;
39+
optional SeparateMessage bar = 2;
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2025 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+
syntax = "proto3";
18+
package nestedprotos;
19+
20+
option java_multiple_files = true;
21+
option java_package = "com.example.bigquerystorage";
22+
option java_outer_classname = "SeparateProtos";
23+
24+
message SeparateMessage {
25+
optional int64 my_int = 1;
26+
optional string my_string = 2;
27+
}

0 commit comments

Comments
 (0)