|
19 | 19 | // [START bigquerystorage_arrow_quickstart] |
20 | 20 |
|
21 | 21 | 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; |
35 | 32 | import com.google.common.base.Preconditions; |
36 | 33 | import com.google.protobuf.Timestamp; |
37 | 34 | import java.io.IOException; |
@@ -62,11 +59,12 @@ private static class SimpleRowReader implements AutoCloseable { |
62 | 59 | private final VectorSchemaRoot root; |
63 | 60 | private final VectorLoader loader; |
64 | 61 |
|
65 | | - |
66 | 62 | 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()))); |
70 | 68 | Preconditions.checkNotNull(schema); |
71 | 69 | List<FieldVector> vectors = new ArrayList<>(); |
72 | 70 | for (Field field : schema.getFields()) { |
@@ -82,10 +80,11 @@ public SimpleRowReader(ArrowSchema arrowSchema) throws IOException { |
82 | 80 | * @param batch object returned from the ReadRowsResponse. |
83 | 81 | */ |
84 | 82 | 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())), |
89 | 88 | allocator); |
90 | 89 |
|
91 | 90 | loader.load(deserializedBatch); |
@@ -94,7 +93,6 @@ public void processRows(ArrowRecordBatch batch) throws IOException { |
94 | 93 | System.out.println(root.contentToTSVString()); |
95 | 94 | // Release buffers from vectors in root. |
96 | 95 | root.clear(); |
97 | | - |
98 | 96 | } |
99 | 97 |
|
100 | 98 | @Override |
@@ -113,16 +111,14 @@ public static void main(String... args) throws Exception { |
113 | 111 | snapshotMillis = Integer.parseInt(args[1]); |
114 | 112 | } |
115 | 113 |
|
116 | | - try (BigQueryStorageClient client = BigQueryStorageClient.create()) { |
| 114 | + try (BigQueryReadClient client = BigQueryReadClient.create()) { |
117 | 115 | String parent = String.format("projects/%s", projectId); |
118 | 116 |
|
119 | 117 | // 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"); |
126 | 122 |
|
127 | 123 | // We specify the columns to be projected by adding them to the selected fields, |
128 | 124 | // and set a simple filter to restrict which rows are transmitted. |
@@ -134,20 +130,14 @@ public static void main(String... args) throws Exception { |
134 | 130 | .setRowRestriction("state = \"WA\"") |
135 | 131 | .build(); |
136 | 132 |
|
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) |
143 | 137 | // This API can also deliver data serialized in Apache Avro format. |
144 | 138 | // 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); |
151 | 141 |
|
152 | 142 | // Optionally specify the snapshot time. When unspecified, snapshot time is "now". |
153 | 143 | if (snapshotMillis != null) { |
@@ -157,9 +147,16 @@ public static void main(String... args) throws Exception { |
157 | 147 | .setNanos((int) ((snapshotMillis % 1000) * 1000000)) |
158 | 148 | .build(); |
159 | 149 | TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build(); |
160 | | - builder.setTableModifiers(modifiers); |
| 150 | + sessionBuilder.setTableModifiers(modifiers); |
161 | 151 | } |
162 | 152 |
|
| 153 | + // Begin building the session creation request. |
| 154 | + CreateReadSessionRequest.Builder builder = |
| 155 | + CreateReadSessionRequest.newBuilder() |
| 156 | + .setParent(parent) |
| 157 | + .setReadSession(sessionBuilder) |
| 158 | + .setMaxStreamCount(1); |
| 159 | + |
163 | 160 | ReadSession session = client.createReadSession(builder.build()); |
164 | 161 | // Setup a simple reader and start a read session. |
165 | 162 | try (SimpleRowReader reader = new SimpleRowReader(session.getArrowSchema())) { |
@@ -171,11 +168,10 @@ public static void main(String... args) throws Exception { |
171 | 168 | Preconditions.checkState(session.getStreamsCount() > 0); |
172 | 169 |
|
173 | 170 | // 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(); |
176 | 172 |
|
177 | 173 | ReadRowsRequest readRowsRequest = |
178 | | - ReadRowsRequest.newBuilder().setReadPosition(readPosition).build(); |
| 174 | + ReadRowsRequest.newBuilder().setReadStream(streamName).build(); |
179 | 175 |
|
180 | 176 | // Process each block of rows as they arrive and decode using our simple row reader. |
181 | 177 | ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest); |
|