Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 9fd7aca

Browse files
docs(tutorials): Add IT to JsonWriteDefaultStream tutorial (#1522)
Includes two small code fixes: - Change a field name in the schema. - Detect schema updates while streaming.
1 parent a5b80ff commit 9fd7aca

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
<id>include-samples</id>
254254
<modules>
255255
<module>samples</module>
256+
<module>tutorials</module>
256257
</modules>
257258
</profile>
258259
</profiles>

tutorials/JsonWriterDefaultStream/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
<artifactId>arrow-memory-netty</artifactId>
4242
<version>6.0.1</version>
4343
</dependency>
44+
<!-- Test Dependencies -->
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.13.2</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.google.truth</groupId>
53+
<artifactId>truth</artifactId>
54+
<version>1.1.3</version>
55+
<scope>test</scope>
56+
</dependency>
4457
</dependencies>
4558

4659
<build>
@@ -57,7 +70,6 @@
5770
</goals>
5871
<configuration>
5972
<sources>
60-
<source>.</source>
6173
<source>../../samples/snippets/src/main/java/com/example/bigquerystorage</source>
6274
</sources>
6375
</configuration>

tutorials/JsonWriterDefaultStream/src/main/java/com/example/JsonWriterDefaultStream.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void createDestinationTable(
6767
.build(),
6868
Field.of("author", StandardSQLTypeName.STRING),
6969
Field.of("committer", StandardSQLTypeName.STRING),
70-
Field.of("ts", StandardSQLTypeName.DATETIME),
70+
Field.of("commit_date", StandardSQLTypeName.DATETIME),
7171
Field.of("subject", StandardSQLTypeName.STRING),
7272
Field.of("message", StandardSQLTypeName.STRING),
7373
Field.of("repo_name", StandardSQLTypeName.STRING));
@@ -114,6 +114,12 @@ public static void writeToDefaultStream(
114114
} // batch
115115
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr);
116116
AppendRowsResponse response = future.get();
117+
if (response.hasUpdatedSchema()) {
118+
// The destination table schema has changed. The client library automatically
119+
// reestablishes a connection to the backend using the new schema, so we can continue
120+
// to send data without interruption.
121+
System.out.println("Table schema changed.");
122+
}
117123
}
118124
System.out.println("Appended records successfully.");
119125
} catch (ExecutionException e) {

tutorials/JsonWriterDefaultStream/src/test/java/com/example/JsonWriterDefaultStreamIT.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,65 @@
1616

1717
package com.example.bigquerystorage;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.DatasetId;
25+
import com.google.cloud.bigquery.DatasetInfo;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.nio.file.FileSystems;
29+
import java.nio.file.Path;
30+
import java.util.UUID;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
1938
@RunWith(JUnit4.class)
2039
public class JsonWriterDefaultStreamIT {
21-
// TODO(mwasson): ADD Integration Test
40+
41+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
43+
private ByteArrayOutputStream bout;
44+
private PrintStream out;
45+
private BigQuery bigquery;
46+
private String datasetName;
47+
48+
@BeforeClass
49+
public static void beforeClass() {}
50+
51+
@Before
52+
public void setUp() {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
57+
bigquery = BigQueryOptions.getDefaultInstance().getService();
58+
59+
// Create a new dataset for each test.
60+
datasetName = "JAVA_WRITER_DEFAULT_STREAM_TEST" + UUID.randomUUID().toString().substring(0, 8);
61+
bigquery.create(DatasetInfo.newBuilder(datasetName).build());
62+
}
63+
64+
@Test
65+
public void testJsonWriterDefaultStream() throws Exception {
66+
Path dataFilePath = FileSystems.getDefault().getPath("src/test/resources", "TestData.json");
67+
68+
System.out.println(dataFilePath.toString());
69+
String[] args = {GOOGLE_CLOUD_PROJECT, datasetName, "github", dataFilePath.toString()};
70+
JsonWriterDefaultStream.main(args);
71+
assertThat(bout.toString()).contains("Appended records successfully.");
72+
}
73+
74+
@After
75+
public void tearDown() {
76+
bigquery.delete(
77+
DatasetId.of(GOOGLE_CLOUD_PROJECT, datasetName), DatasetDeleteOption.deleteContents());
78+
System.setOut(null);
79+
}
2280
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{"commit":"0001","parent":["00001"],"author":"user1","committer":"GitHub","subject":"Commit 1","message":"This is a commit.\n\n","repo_name":"googleapis/java-bigquerystorage","commit_date":"2019-07-23T20:28:01"}
2+
{"commit":"0002","parent":["00002"],"author":"user1","committer":"GitHub","subject":"Commit 2","message":"This is a commit.\n\n","repo_name":"googleapis/java-bigquerystorage","commit_date":"2019-12-05T16:05:16"}
3+
{"commit":"0003","parent":["00003"],"author":"user1","committer":"GitHub","subject":"Commit 3","message":"This is a commit.\n\n","repo_name":"googleapis/java-bigquerystorage","commit_date":"2019-03-21T16:59:23"}
4+
{"commit":"0004","parent":["00004"],"author":"user1","committer":"GitHub","subject":"Commit 4","message":"This is a commit.\n\n","repo_name":"googleapis/java-bigquerystorage","commit_date":"2019-01-11T01:31:39"}
5+
{"commit":"0005","parent":["00005"],"author":"user1","committer":"GitHub","subject":"Commit 5","message":"This is a commit.\n\n","repo_name":"googleapis/java-bigquerystorage","commit_date":"2019-07-31T19:09:09"}

0 commit comments

Comments
 (0)