Search in sources :

Example 6 with TableReadOptions

use of com.google.cloud.bigquery.storage.v1.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");
}
Also used : ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) TableReadOptions(com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Example 7 with TableReadOptions

use of com.google.cloud.bigquery.storage.v1.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);
}
Also used : AvroRowConsumer(com.google.cloud.bigquery.storage.v1.it.SimpleRowReader.AvroRowConsumer) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) GenericData(org.apache.avro.generic.GenericData) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableReadOptions(com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Aggregations

CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)5 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)5 TableReadOptions (com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions)5 Test (org.junit.Test)5 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)4 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)4 GenericData (org.apache.avro.generic.GenericData)4 BigQueryReadClient (com.google.cloud.bigquery.storage.v1.BigQueryReadClient)2 TableModifiers (com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers)2 AvroRowConsumer (com.google.cloud.bigquery.storage.v1.it.SimpleRowReader.AvroRowConsumer)2 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest)2 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest)2 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse)2 ReadSession (com.google.cloud.bigquery.storage.v1beta2.ReadSession)2 TableReadOptions (com.google.cloud.bigquery.storage.v1beta2.ReadSession.TableReadOptions)2 AvroRowConsumer (com.google.cloud.bigquery.storage.v1beta2.it.SimpleRowReader.AvroRowConsumer)2 Timestamp (com.google.protobuf.Timestamp)2 Schema (org.apache.avro.Schema)2 Utf8 (org.apache.avro.util.Utf8)2