use of com.google.cloud.bigquery.storage.v1beta2.ReadSession.TableReadOptions in project spark-bigquery-connector by GoogleCloudDataproc.
the class ReadSessionCreatorTest method testSerializedInstanceIsPropagated.
@Test
public void testSerializedInstanceIsPropagated() throws Exception {
TableReadOptions tableReadOptions = TableReadOptions.newBuilder().build();
ReadSession readSession = ReadSession.newBuilder().setName("abc").setReadOptions(tableReadOptions).build();
CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder().setReadSession(readSession).build();
Optional<String> encodedBase = Optional.of(java.util.Base64.getEncoder().encodeToString(request.toByteArray()));
ReadSessionCreatorConfig config = new ReadSessionCreatorConfigBuilder().setRequestEncodedBase(encodedBase).build();
ReadSessionCreator creator = new ReadSessionCreator(config, bigQueryClient, bigQueryReadClientFactory);
when(bigQueryReadClientFactory.getBigQueryReadClient()).thenReturn(readClient);
when(bigQueryClient.getTable(any())).thenReturn(table);
when(stub.createReadSessionCallable()).thenReturn(createReadSessionCall);
creator.create(TableId.of("dataset", "table"), ImmutableList.of("col1", "col2"), Optional.empty()).getReadSession();
ArgumentCaptor<CreateReadSessionRequest> requestCaptor = ArgumentCaptor.forClass(CreateReadSessionRequest.class);
verify(createReadSessionCall, times(1)).call(requestCaptor.capture());
ReadSession actual = requestCaptor.getValue().getReadSession();
assertThat(actual.getName()).isEqualTo("abc");
assertThat(actual.getReadOptions().getSelectedFieldsList()).containsExactly("col1", "col2");
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadSession.TableReadOptions in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method testFilter.
@Test
public void testFilter() throws IOException {
String table = BigQueryResource.FormatTableResource(/* projectId = */
"bigquery-public-data", /* datasetId = */
"samples", /* tableId = */
"shakespeare");
TableReadOptions options = TableReadOptions.newBuilder().setRowRestriction("word_count > 100").build();
CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder().setParent(parentProjectId).setMaxStreamCount(1).setReadSession(ReadSession.newBuilder().setTable(table).setReadOptions(options).setDataFormat(DataFormat.AVRO).build()).build();
ReadSession session = client.createReadSession(request);
assertEquals(String.format("Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", table, session.toString()), 1, session.getStreamsCount());
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(session.getStreams(0).getName()).build();
SimpleRowReader reader = new SimpleRowReader(new Schema.Parser().parse(session.getAvroSchema().getSchema()));
long rowCount = 0;
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
rowCount += response.getRowCount();
reader.processRows(response.getAvroRows(), new AvroRowConsumer() {
@Override
public void accept(GenericData.Record record) {
Long wordCount = (Long) record.get("word_count");
assertWithMessage("Row not matching expectations: %s", record.toString()).that(wordCount).isGreaterThan(100L);
}
});
}
assertEquals(1_333, rowCount);
}
Aggregations