|
| 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.google.cloud.bigquery.storage.v1.it.util; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +import com.google.api.core.ApiFutureCallback; |
| 23 | +import com.google.api.gax.rpc.ServerStream; |
| 24 | +import com.google.auth.oauth2.ServiceAccountCredentials; |
| 25 | +import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; |
| 26 | +import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; |
| 27 | +import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest; |
| 28 | +import com.google.cloud.bigquery.storage.v1.DataFormat; |
| 29 | +import com.google.cloud.bigquery.storage.v1.ReadRowsRequest; |
| 30 | +import com.google.cloud.bigquery.storage.v1.ReadRowsResponse; |
| 31 | +import com.google.cloud.bigquery.storage.v1.ReadSession; |
| 32 | +import com.google.common.base.Preconditions; |
| 33 | +import com.google.protobuf.util.Timestamps; |
| 34 | +import java.io.ByteArrayInputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import org.apache.avro.Schema; |
| 40 | +import org.apache.avro.generic.GenericData; |
| 41 | +import org.apache.avro.generic.GenericRecordBuilder; |
| 42 | + |
| 43 | +public class Helper { |
| 44 | + |
| 45 | + public static final Long[] INPUT_TIMESTAMPS_MICROS = |
| 46 | + new Long[] { |
| 47 | + 1735734896123456L, // 2025-01-01T12:34:56.123456Z |
| 48 | + 1580646896123456L, // 2020-02-02T12:34:56.123456Z |
| 49 | + 636467696123456L, // 1990-03-03T12:34:56.123456Z |
| 50 | + 165846896123456L // 1975-04-04T12:34:56.123456Z |
| 51 | + }; |
| 52 | + |
| 53 | + public static final Long[] EXPECTED_TIMESTAMPS_MICROS = |
| 54 | + new Long[] { |
| 55 | + 1735734896123456L, // 2025-01-01T12:34:56.123456Z |
| 56 | + 1580646896123456L, // 2020-02-02T12:34:56.123456Z |
| 57 | + 636467696123456L, // 1990-03-03T12:34:56.123456Z |
| 58 | + 165846896123456L // 1975-04-04T12:34:56.123456Z |
| 59 | + }; |
| 60 | + |
| 61 | + public static ServiceAccountCredentials loadCredentials(String credentialFile) { |
| 62 | + try (InputStream keyStream = new ByteArrayInputStream(credentialFile.getBytes())) { |
| 63 | + return ServiceAccountCredentials.fromStream(keyStream); |
| 64 | + } catch (IOException e) { |
| 65 | + fail("Couldn't create fake JSON credentials."); |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + public static class AppendCompleteCallback implements ApiFutureCallback<AppendRowsResponse> { |
| 71 | + private final Object lock = new Object(); |
| 72 | + private int batchCount = 0; |
| 73 | + |
| 74 | + public void onSuccess(AppendRowsResponse response) { |
| 75 | + synchronized (lock) { |
| 76 | + if (response.hasError()) { |
| 77 | + System.out.format("Error: %s\n", response.getError()); |
| 78 | + } else { |
| 79 | + ++batchCount; |
| 80 | + System.out.format("Wrote batch %d\n", batchCount); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void onFailure(Throwable throwable) { |
| 86 | + System.out.format("Error: %s\n", throwable.toString()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Reads all the rows from the specified table. |
| 92 | + * |
| 93 | + * <p>For every row, the consumer is called for processing. |
| 94 | + * |
| 95 | + * @param table |
| 96 | + * @param snapshotInMillis Optional. If specified, all rows up to timestamp will be returned. |
| 97 | + * @param filter Optional. If specified, it will be used to restrict returned data. |
| 98 | + * @param consumer that receives all Avro rows. |
| 99 | + * @throws IOException |
| 100 | + */ |
| 101 | + public static void processRowsAtSnapshot( |
| 102 | + BigQueryReadClient client, |
| 103 | + String parentProjectId, |
| 104 | + String table, |
| 105 | + Long snapshotInMillis, |
| 106 | + String filter, |
| 107 | + SimpleRowReaderAvro.AvroRowConsumer consumer) |
| 108 | + throws IOException { |
| 109 | + Preconditions.checkNotNull(table); |
| 110 | + Preconditions.checkNotNull(consumer); |
| 111 | + |
| 112 | + CreateReadSessionRequest.Builder createSessionRequestBuilder = |
| 113 | + CreateReadSessionRequest.newBuilder() |
| 114 | + .setParent(parentProjectId) |
| 115 | + .setMaxStreamCount(1) |
| 116 | + .setReadSession( |
| 117 | + ReadSession.newBuilder().setTable(table).setDataFormat(DataFormat.AVRO).build()); |
| 118 | + |
| 119 | + if (snapshotInMillis != null) { |
| 120 | + createSessionRequestBuilder |
| 121 | + .getReadSessionBuilder() |
| 122 | + .setTableModifiers( |
| 123 | + ReadSession.TableModifiers.newBuilder() |
| 124 | + .setSnapshotTime(Timestamps.fromMillis(snapshotInMillis)) |
| 125 | + .build()); |
| 126 | + } |
| 127 | + |
| 128 | + if (filter != null && !filter.isEmpty()) { |
| 129 | + createSessionRequestBuilder |
| 130 | + .getReadSessionBuilder() |
| 131 | + .setReadOptions( |
| 132 | + ReadSession.TableReadOptions.newBuilder().setRowRestriction(filter).build()); |
| 133 | + } |
| 134 | + |
| 135 | + ReadSession session = client.createReadSession(createSessionRequestBuilder.build()); |
| 136 | + assertEquals( |
| 137 | + String.format( |
| 138 | + "Did not receive expected number of streams for table '%s' CreateReadSession" |
| 139 | + + " response:%n%s", |
| 140 | + table, session.toString()), |
| 141 | + 1, |
| 142 | + session.getStreamsCount()); |
| 143 | + |
| 144 | + ReadRowsRequest readRowsRequest = |
| 145 | + ReadRowsRequest.newBuilder().setReadStream(session.getStreams(0).getName()).build(); |
| 146 | + |
| 147 | + SimpleRowReaderAvro reader = |
| 148 | + new SimpleRowReaderAvro(new Schema.Parser().parse(session.getAvroSchema().getSchema())); |
| 149 | + |
| 150 | + ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest); |
| 151 | + for (ReadRowsResponse response : stream) { |
| 152 | + reader.processRows(response.getAvroRows(), consumer); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Reads all the rows from the specified table and returns a list as generic Avro records. |
| 158 | + * |
| 159 | + * @param table |
| 160 | + * @param filter Optional. If specified, it will be used to restrict returned data. |
| 161 | + * @return |
| 162 | + */ |
| 163 | + public static List<GenericData.Record> readAllRows( |
| 164 | + BigQueryReadClient client, String parentProjectId, String table, String filter) |
| 165 | + throws IOException { |
| 166 | + final List<GenericData.Record> rows = new ArrayList<>(); |
| 167 | + processRowsAtSnapshot( |
| 168 | + client, |
| 169 | + parentProjectId, |
| 170 | + /* table= */ table, |
| 171 | + /* snapshotInMillis= */ null, |
| 172 | + /* filter= */ filter, |
| 173 | + (SimpleRowReaderAvro.AvroRowConsumer) |
| 174 | + record -> { |
| 175 | + // clone the record since that reference will be reused by the reader. |
| 176 | + rows.add(new GenericRecordBuilder(record).build()); |
| 177 | + }); |
| 178 | + return rows; |
| 179 | + } |
| 180 | +} |
0 commit comments