Search in sources :

Example 16 with CreateReadSessionRequest

use of com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest in project beam by apache.

the class BigQueryIOStorageReadTest method testReadFromBigQueryIOWithTrimmedSchema.

@Test
public void testReadFromBigQueryIOWithTrimmedSchema() throws Exception {
    fakeDatasetService.createDataset("foo.com:project", "dataset", "", "", null);
    TableReference tableRef = BigQueryHelpers.parseTableSpec("foo.com:project:dataset.table");
    Table table = new Table().setTableReference(tableRef).setNumBytes(10L).setSchema(TABLE_SCHEMA);
    fakeDatasetService.createTable(table);
    CreateReadSessionRequest expectedCreateReadSessionRequest = CreateReadSessionRequest.newBuilder().setParent("projects/project-id").setReadSession(ReadSession.newBuilder().setTable("projects/foo.com:project/datasets/dataset/tables/table").setReadOptions(ReadSession.TableReadOptions.newBuilder().addSelectedFields("name")).setDataFormat(DataFormat.AVRO)).setMaxStreamCount(10).build();
    ReadSession readSession = ReadSession.newBuilder().setName("readSessionName").setAvroSchema(AvroSchema.newBuilder().setSchema(TRIMMED_AVRO_SCHEMA_STRING)).addStreams(ReadStream.newBuilder().setName("streamName")).setDataFormat(DataFormat.AVRO).build();
    ReadRowsRequest expectedReadRowsRequest = ReadRowsRequest.newBuilder().setReadStream("streamName").build();
    List<GenericRecord> records = Lists.newArrayList(createRecord("A", TRIMMED_AVRO_SCHEMA), createRecord("B", TRIMMED_AVRO_SCHEMA), createRecord("C", TRIMMED_AVRO_SCHEMA), createRecord("D", TRIMMED_AVRO_SCHEMA));
    List<ReadRowsResponse> readRowsResponses = Lists.newArrayList(createResponse(TRIMMED_AVRO_SCHEMA, records.subList(0, 2), 0.0, 0.50), createResponse(TRIMMED_AVRO_SCHEMA, records.subList(2, 4), 0.5, 0.75));
    StorageClient fakeStorageClient = mock(StorageClient.class, withSettings().serializable());
    when(fakeStorageClient.createReadSession(expectedCreateReadSessionRequest)).thenReturn(readSession);
    when(fakeStorageClient.readRows(expectedReadRowsRequest, "")).thenReturn(new FakeBigQueryServerStream<>(readRowsResponses));
    PCollection<TableRow> output = p.apply(BigQueryIO.readTableRows().from("foo.com:project:dataset.table").withMethod(Method.DIRECT_READ).withSelectedFields(Lists.newArrayList("name")).withFormat(DataFormat.AVRO).withTestServices(new FakeBigQueryServices().withDatasetService(fakeDatasetService).withStorageClient(fakeStorageClient)));
    PAssert.that(output).containsInAnyOrder(ImmutableList.of(new TableRow().set("name", "A"), new TableRow().set("name", "B"), new TableRow().set("name", "C"), new TableRow().set("name", "D")));
    p.run();
}
Also used : Table(com.google.api.services.bigquery.model.Table) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) TableReference(com.google.api.services.bigquery.model.TableReference) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) GenericRecord(org.apache.avro.generic.GenericRecord) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Example 17 with CreateReadSessionRequest

use of com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest in project beam by apache.

the class BigQueryServicesImplTest method testCreateReadSessionSetsRequestCountMetric.

@Test
public void testCreateReadSessionSetsRequestCountMetric() throws InterruptedException, IOException {
    BigQueryServicesImpl.StorageClientImpl client = mock(BigQueryServicesImpl.StorageClientImpl.class);
    CreateReadSessionRequest.Builder builder = CreateReadSessionRequest.newBuilder();
    builder.getReadSessionBuilder().setTable("myproject:mydataset.mytable");
    CreateReadSessionRequest request = builder.build();
    when(client.callCreateReadSession(request)).thenReturn(// Mock implementation.
    ReadSession.newBuilder().build());
    // Real implementation.
    when(client.createReadSession(any())).thenCallRealMethod();
    client.createReadSession(request);
    verifyReadMetricWasSet("myproject", "mydataset", "mytable", "ok", 1);
}
Also used : CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Example 18 with CreateReadSessionRequest

use of com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest in project beam by apache.

the class BigQueryServicesImplTest method testCreateReadSessionSetsRequestCountMetricOnError.

@Test
public void testCreateReadSessionSetsRequestCountMetricOnError() throws InterruptedException, IOException {
    BigQueryServicesImpl.StorageClientImpl client = mock(BigQueryServicesImpl.StorageClientImpl.class);
    CreateReadSessionRequest.Builder builder = CreateReadSessionRequest.newBuilder();
    builder.getReadSessionBuilder().setTable("myproject:mydataset.mytable");
    CreateReadSessionRequest request = builder.build();
    StatusCode statusCode = new StatusCode() {

        @Override
        public Code getCode() {
            return Code.NOT_FOUND;
        }

        @Override
        public Object getTransportCode() {
            return null;
        }
    };
    when(client.callCreateReadSession(request)).thenThrow(// Mock implementation.
    new ApiException("Not Found", null, statusCode, false));
    // Real implementation.
    when(client.createReadSession(any())).thenCallRealMethod();
    thrown.expect(ApiException.class);
    thrown.expectMessage("Not Found");
    client.createReadSession(request);
    verifyReadMetricWasSet("myproject", "mydataset", "mytable", "not_found", 1);
}
Also used : StatusCode(com.google.api.gax.rpc.StatusCode) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 19 with CreateReadSessionRequest

use of com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest 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 20 with CreateReadSessionRequest

use of com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest 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)18 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)16 Test (org.junit.Test)16 Table (com.google.api.services.bigquery.model.Table)12 StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)12 TableReference (com.google.api.services.bigquery.model.TableReference)11 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)11 TableRow (com.google.api.services.bigquery.model.TableRow)9 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)8 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)5 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)5 ByteString (com.google.protobuf.ByteString)5 Schema (org.apache.avro.Schema)5 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)4 JobStatistics2 (com.google.api.services.bigquery.model.JobStatistics2)4 TableSchema (com.google.api.services.bigquery.model.TableSchema)4 TableReadOptions (com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions)4 GenericData (org.apache.avro.generic.GenericData)4 BigQueryResourceNaming.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference)4 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)2