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

Commit bf07b87

Browse files
committed
Multiple appends
1 parent e47f230 commit bf07b87

1 file changed

Lines changed: 28 additions & 36 deletions

File tree

samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStreamArrow renamed to samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStreamWithArrow.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.google.cloud.bigquery.storage.v1.TableName;
4040
import com.google.common.collect.ImmutableList;
4141
import com.google.common.util.concurrent.MoreExecutors;
42-
import com.google.protobuf.ByteString;
4342
import com.google.protobuf.Descriptors.DescriptorValidationException;
4443
import java.io.IOException;
4544
import java.util.List;
@@ -78,38 +77,25 @@ public static void main(String[] args)
7877
writeToDefaultStreamWithArrow(projectId, datasetName, tableName);
7978
}
8079

81-
private static ByteString buildByteString() {
82-
byte[] bytes = new byte[] {1, 2, 3, 4, 5};
83-
return ByteString.copyFrom(bytes);
84-
}
85-
86-
// Create a JSON object that is compatible with the table schema.
87-
private static ArrowRecordBatch buildRecordBatchWithThreeRows(VectorSchemaRoot root) {
80+
// Create an ArrowRecordBatch object that is compatible with the table schema.
81+
private static ArrowRecordBatch buildRecordBatch(VectorSchemaRoot root, int rowCount) {
8882
VarCharVector test_string = (VarCharVector) root.getVector("test_string");
89-
test_string.allocateNew(3);
90-
test_string.set(0, "A".getBytes());
91-
test_string.set(1, "B".getBytes());
92-
test_string.set(2, "C".getBytes());
9383
BigIntVector test_int = (BigIntVector) root.getVector("test_int");
94-
test_int.allocateNew(3);
95-
test_int.set(0, 1);
96-
test_int.set(1, 2);
97-
test_int.set(2, 3);
9884
VarCharVector test_geo = (VarCharVector) root.getVector("test_geo");
99-
test_geo.allocateNew(3);
100-
test_geo.set(
101-
0,
102-
"POLYGON((-124.49 47.35,-124.49 40.73,-116.49 40.73,-113.49 47.35,-124.49 47.35))"
103-
.getBytes());
104-
test_geo.set(
105-
1,
106-
"POLYGON((-124.49 47.35,-124.49 40.73,-116.49 40.73,-115.49 47.35,-124.49 47.35))"
107-
.getBytes());
108-
test_geo.set(
109-
2,
110-
"POLYGON((-124.49 47.35,-124.49 40.73,-116.49 40.73,-116.49 47.35,-124.49 47.35))"
111-
.getBytes());
112-
root.setRowCount(3);
85+
86+
test_string.allocateNew(rowCount);
87+
test_int.allocateNew(rowCount);
88+
test_geo.allocateNew(rowCount);
89+
90+
for (int i = 0; i < rowCount; i++) {
91+
test_string.set(i, ("A" + i).getBytes());
92+
test_int.set(i, i + 100);
93+
test_geo.set(
94+
i,
95+
"POLYGON((-124.49 47.35,-124.49 40.73,-116.49 40.73,-113.49 47.35,-124.49 47.35))"
96+
.getBytes());
97+
}
98+
root.setRowCount(rowCount);
11399

114100
CompressionCodec codec =
115101
NoCompressionCodec.Factory.INSTANCE.createCodec(CompressionUtil.CodecType.NO_COMPRESSION);
@@ -130,14 +116,20 @@ public static void writeToDefaultStreamWithArrow(
130116
long initialRowCount = getRowCount(parentTable);
131117

132118
BufferAllocator allocator = new RootAllocator();
133-
try (VectorSchemaRoot root = VectorSchemaRoot.create(arrowSchema, allocator)) {
134-
ArrowRecordBatch batch = buildRecordBatchWithThreeRows(root);
135-
writer.append(new ArrowData(arrowSchema, batch));
136119

137-
// Final cleanup for the stream during worker teardown.
138-
writer.cleanup();
120+
// A writer should be used to ingest as much data as possible before teardown.
121+
// Append 100 batches.
122+
for (int i = 0; i < 100; i++) {
123+
try (VectorSchemaRoot root = VectorSchemaRoot.create(arrowSchema, allocator)) {
124+
// Each batch has 10 rows.
125+
ArrowRecordBatch batch = buildRecordBatch(root, 10);
126+
writer.append(new ArrowData(arrowSchema, batch));
127+
}
139128
}
140-
verifyExpectedRowCount(parentTable, initialRowCount + 3);
129+
// Final cleanup for the stream during worker teardown.
130+
writer.cleanup();
131+
132+
verifyExpectedRowCount(parentTable, initialRowCount + 1000);
141133
System.out.println("Appended records successfully.");
142134
}
143135

0 commit comments

Comments
 (0)