GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — chore: update bigquerystorage example to use V1 bindings (#2315) · shinyay/java-docs-samples@a7a5e0d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bigquery/bigquerystorage
    • pom.xml
    • src/main/java/com/example/bigquerystorage
      • StorageArrowSample.java
      • StorageSample.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bigquery/bigquerystorage/pom.xml‎

Copy file name to clipboardExpand all lines: bigquery/bigquerystorage/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.google.cloud</groupId>
4444
<artifactId>google-cloud-bigquerystorage</artifactId>
45-
<version>0.118.0-beta</version>
45+
<version>0.125.0-beta</version>
4646
</dependency>
4747

4848
<dependency>
Collapse file

‎bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageArrowSample.java‎

Copy file name to clipboardExpand all lines: bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageArrowSample.java
+41-45Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@
1919
// [START bigquerystorage_arrow_quickstart]
2020

2121
import com.google.api.gax.rpc.ServerStream;
22-
import com.google.cloud.bigquery.storage.v1beta1.ArrowProto.ArrowRecordBatch;
23-
import com.google.cloud.bigquery.storage.v1beta1.ArrowProto.ArrowSchema;
24-
import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient;
25-
import com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions;
26-
import com.google.cloud.bigquery.storage.v1beta1.Storage;
27-
import com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest;
28-
import com.google.cloud.bigquery.storage.v1beta1.Storage.DataFormat;
29-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest;
30-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse;
31-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession;
32-
import com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition;
33-
import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableModifiers;
34-
import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference;
22+
import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch;
23+
import com.google.cloud.bigquery.storage.v1.ArrowSchema;
24+
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
25+
import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest;
26+
import com.google.cloud.bigquery.storage.v1.DataFormat;
27+
import com.google.cloud.bigquery.storage.v1.ReadRowsRequest;
28+
import com.google.cloud.bigquery.storage.v1.ReadRowsResponse;
29+
import com.google.cloud.bigquery.storage.v1.ReadSession;
30+
import com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers;
31+
import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions;
3532
import com.google.common.base.Preconditions;
3633
import com.google.protobuf.Timestamp;
3734
import java.io.IOException;
@@ -62,11 +59,12 @@ private static class SimpleRowReader implements AutoCloseable {
6259
private final VectorSchemaRoot root;
6360
private final VectorLoader loader;
6461

65-
6662
public SimpleRowReader(ArrowSchema arrowSchema) throws IOException {
67-
Schema schema = MessageSerializer.deserializeSchema(new ReadChannel(
68-
new ByteArrayReadableSeekableByteChannel(
69-
arrowSchema.getSerializedSchema().toByteArray())));
63+
Schema schema =
64+
MessageSerializer.deserializeSchema(
65+
new ReadChannel(
66+
new ByteArrayReadableSeekableByteChannel(
67+
arrowSchema.getSerializedSchema().toByteArray())));
7068
Preconditions.checkNotNull(schema);
7169
List<FieldVector> vectors = new ArrayList<>();
7270
for (Field field : schema.getFields()) {
@@ -82,10 +80,11 @@ public SimpleRowReader(ArrowSchema arrowSchema) throws IOException {
8280
* @param batch object returned from the ReadRowsResponse.
8381
*/
8482
public void processRows(ArrowRecordBatch batch) throws IOException {
85-
org.apache.arrow.vector.ipc.message.ArrowRecordBatch deserializedBatch = MessageSerializer
86-
.deserializeRecordBatch(
87-
new ReadChannel(new ByteArrayReadableSeekableByteChannel(
88-
batch.getSerializedRecordBatch().toByteArray())),
83+
org.apache.arrow.vector.ipc.message.ArrowRecordBatch deserializedBatch =
84+
MessageSerializer.deserializeRecordBatch(
85+
new ReadChannel(
86+
new ByteArrayReadableSeekableByteChannel(
87+
batch.getSerializedRecordBatch().toByteArray())),
8988
allocator);
9089

9190
loader.load(deserializedBatch);
@@ -94,7 +93,6 @@ public void processRows(ArrowRecordBatch batch) throws IOException {
9493
System.out.println(root.contentToTSVString());
9594
// Release buffers from vectors in root.
9695
root.clear();
97-
9896
}
9997

10098
@Override
@@ -113,16 +111,14 @@ public static void main(String... args) throws Exception {
113111
snapshotMillis = Integer.parseInt(args[1]);
114112
}
115113

116-
try (BigQueryStorageClient client = BigQueryStorageClient.create()) {
114+
try (BigQueryReadClient client = BigQueryReadClient.create()) {
117115
String parent = String.format("projects/%s", projectId);
118116

119117
// This example uses baby name data from the public datasets.
120-
TableReference tableReference =
121-
TableReference.newBuilder()
122-
.setProjectId("bigquery-public-data")
123-
.setDatasetId("usa_names")
124-
.setTableId("usa_1910_current")
125-
.build();
118+
String srcTable =
119+
String.format(
120+
"projects/%s/datasets/%s/tables/%s",
121+
"bigquery-public-data", "usa_names", "usa_1910_current");
126122

127123
// We specify the columns to be projected by adding them to the selected fields,
128124
// and set a simple filter to restrict which rows are transmitted.
@@ -134,20 +130,14 @@ public static void main(String... args) throws Exception {
134130
.setRowRestriction("state = \"WA\"")
135131
.build();
136132

137-
// Begin building the session request.
138-
CreateReadSessionRequest.Builder builder =
139-
CreateReadSessionRequest.newBuilder()
140-
.setParent(parent)
141-
.setTableReference(tableReference)
142-
.setReadOptions(options)
133+
// Start specifying the read session we want created.
134+
ReadSession.Builder sessionBuilder =
135+
ReadSession.newBuilder()
136+
.setTable(srcTable)
143137
// This API can also deliver data serialized in Apache Avro format.
144138
// This example leverages Apache Arrow.
145-
.setFormat(DataFormat.ARROW)
146-
// We use a LIQUID strategy in this example because we only
147-
// read from a single stream. Consider BALANCED if you're consuming
148-
// multiple streams concurrently and want more consistent stream sizes.
149-
.setShardingStrategy(Storage.ShardingStrategy.LIQUID)
150-
.setRequestedStreams(1);
139+
.setDataFormat(DataFormat.ARROW)
140+
.setReadOptions(options);
151141

152142
// Optionally specify the snapshot time. When unspecified, snapshot time is "now".
153143
if (snapshotMillis != null) {
@@ -157,9 +147,16 @@ public static void main(String... args) throws Exception {
157147
.setNanos((int) ((snapshotMillis % 1000) * 1000000))
158148
.build();
159149
TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build();
160-
builder.setTableModifiers(modifiers);
150+
sessionBuilder.setTableModifiers(modifiers);
161151
}
162152

153+
// Begin building the session creation request.
154+
CreateReadSessionRequest.Builder builder =
155+
CreateReadSessionRequest.newBuilder()
156+
.setParent(parent)
157+
.setReadSession(sessionBuilder)
158+
.setMaxStreamCount(1);
159+
163160
ReadSession session = client.createReadSession(builder.build());
164161
// Setup a simple reader and start a read session.
165162
try (SimpleRowReader reader = new SimpleRowReader(session.getArrowSchema())) {
@@ -171,11 +168,10 @@ public static void main(String... args) throws Exception {
171168
Preconditions.checkState(session.getStreamsCount() > 0);
172169

173170
// Use the first stream to perform reading.
174-
StreamPosition readPosition =
175-
StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
171+
String streamName = session.getStreams(0).getName();
176172

177173
ReadRowsRequest readRowsRequest =
178-
ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
174+
ReadRowsRequest.newBuilder().setReadStream(streamName).build();
179175

180176
// Process each block of rows as they arrive and decode using our simple row reader.
181177
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
Collapse file

‎bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageSample.java‎

Copy file name to clipboardExpand all lines: bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageSample.java
+31-36Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919
// [START bigquerystorage_quickstart]
2020

2121
import com.google.api.gax.rpc.ServerStream;
22-
import com.google.cloud.bigquery.storage.v1beta1.AvroProto.AvroRows;
23-
import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient;
24-
import com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions;
25-
import com.google.cloud.bigquery.storage.v1beta1.Storage;
26-
import com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest;
27-
import com.google.cloud.bigquery.storage.v1beta1.Storage.DataFormat;
28-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest;
29-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse;
30-
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession;
31-
import com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition;
32-
import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableModifiers;
33-
import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference;
22+
import com.google.cloud.bigquery.storage.v1.AvroRows;
23+
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
24+
import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest;
25+
import com.google.cloud.bigquery.storage.v1.DataFormat;
26+
import com.google.cloud.bigquery.storage.v1.ReadRowsRequest;
27+
import com.google.cloud.bigquery.storage.v1.ReadRowsResponse;
28+
import com.google.cloud.bigquery.storage.v1.ReadSession;
29+
import com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers;
30+
import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions;
3431
import com.google.common.base.Preconditions;
3532
import com.google.protobuf.Timestamp;
3633
import java.io.IOException;
@@ -89,16 +86,14 @@ public static void main(String... args) throws Exception {
8986
snapshotMillis = Integer.parseInt(args[1]);
9087
}
9188

92-
try (BigQueryStorageClient client = BigQueryStorageClient.create()) {
89+
try (BigQueryReadClient client = BigQueryReadClient.create()) {
9390
String parent = String.format("projects/%s", projectId);
9491

9592
// This example uses baby name data from the public datasets.
96-
TableReference tableReference =
97-
TableReference.newBuilder()
98-
.setProjectId("bigquery-public-data")
99-
.setDatasetId("usa_names")
100-
.setTableId("usa_1910_current")
101-
.build();
93+
String srcTable =
94+
String.format(
95+
"projects/%s/datasets/%s/tables/%s",
96+
"bigquery-public-data", "usa_names", "usa_1910_current");
10297

10398
// We specify the columns to be projected by adding them to the selected fields,
10499
// and set a simple filter to restrict which rows are transmitted.
@@ -110,20 +105,14 @@ public static void main(String... args) throws Exception {
110105
.setRowRestriction("state = \"WA\"")
111106
.build();
112107

113-
// Begin building the session request.
114-
CreateReadSessionRequest.Builder builder =
115-
CreateReadSessionRequest.newBuilder()
116-
.setParent(parent)
117-
.setTableReference(tableReference)
118-
.setReadOptions(options)
119-
// This API can also deliver data serialized in Apache Arrow format.
108+
// Start specifying the read session we want created.
109+
ReadSession.Builder sessionBuilder =
110+
ReadSession.newBuilder()
111+
.setTable(srcTable)
112+
// This API can also deliver data serialized in Apache Avro format.
120113
// This example leverages Apache Avro.
121-
.setFormat(DataFormat.AVRO)
122-
// We use a LIQUID strategy in this example because we only
123-
// read from a single stream. Consider BALANCED if you're consuming
124-
// multiple streams concurrently and want more consistent stream sizes.
125-
.setShardingStrategy(Storage.ShardingStrategy.LIQUID)
126-
.setRequestedStreams(1);
114+
.setDataFormat(DataFormat.AVRO)
115+
.setReadOptions(options);
127116

128117
// Optionally specify the snapshot time. When unspecified, snapshot time is "now".
129118
if (snapshotMillis != null) {
@@ -133,9 +122,16 @@ public static void main(String... args) throws Exception {
133122
.setNanos((int) ((snapshotMillis % 1000) * 1000000))
134123
.build();
135124
TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build();
136-
builder.setTableModifiers(modifiers);
125+
sessionBuilder.setTableModifiers(modifiers);
137126
}
138127

128+
// Begin building the session creation request.
129+
CreateReadSessionRequest.Builder builder =
130+
CreateReadSessionRequest.newBuilder()
131+
.setParent(parent)
132+
.setReadSession(sessionBuilder)
133+
.setMaxStreamCount(1);
134+
139135
// Request the session creation.
140136
ReadSession session = client.createReadSession(builder.build());
141137

@@ -148,11 +144,10 @@ public static void main(String... args) throws Exception {
148144
Preconditions.checkState(session.getStreamsCount() > 0);
149145

150146
// Use the first stream to perform reading.
151-
StreamPosition readPosition =
152-
StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
147+
String streamName = session.getStreams(0).getName();
153148

154149
ReadRowsRequest readRowsRequest =
155-
ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
150+
ReadRowsRequest.newBuilder().setReadStream(streamName).build();
156151

157152
// Process each block of rows as they arrive and decode using our simple row reader.
158153
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.