Search in sources :

Example 6 with CreateReadSessionRequest

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

the class BigQueryIOStorageQueryTest method doQuerySourceInitialSplit.

private void doQuerySourceInitialSplit(long bundleSize, int requestedStreamCount, int expectedStreamCount) throws Exception {
    TableReference sourceTableRef = BigQueryHelpers.parseTableSpec("project:dataset.table");
    fakeDatasetService.createDataset(sourceTableRef.getProjectId(), sourceTableRef.getDatasetId(), "asia-northeast1", "Fake plastic tree^H^H^H^Htables", null);
    fakeDatasetService.createTable(new Table().setTableReference(sourceTableRef).setLocation("asia-northeast1"));
    Table queryResultTable = new Table().setSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"), new TableFieldSchema().setName("number").setType("INTEGER")))).setNumBytes(1024L * 1024L);
    String encodedQuery = FakeBigQueryServices.encodeQueryResult(queryResultTable);
    fakeJobService.expectDryRunQuery(options.getProject(), encodedQuery, new JobStatistics().setQuery(new JobStatistics2().setTotalBytesProcessed(1024L * 1024L).setReferencedTables(ImmutableList.of(sourceTableRef))));
    String stepUuid = "testStepUuid";
    TableReference tempTableReference = createTempTableReference(options.getProject(), BigQueryResourceNaming.createJobIdPrefix(options.getJobName(), stepUuid, JobType.QUERY), Optional.empty());
    CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/" + options.getProject()).setReadSession(ReadSession.newBuilder().setTable(BigQueryHelpers.toTableResourceName(tempTableReference))).setMaxStreamCount(requestedStreamCount).build();
    Schema sessionSchema = SchemaBuilder.record("__root__").fields().name("name").type().nullable().stringType().noDefault().name("number").type().nullable().longType().noDefault().endRecord();
    ReadSession.Builder builder = ReadSession.newBuilder().setAvroSchema(AvroSchema.newBuilder().setSchema(sessionSchema.toString())).setDataFormat(DataFormat.AVRO);
    for (int i = 0; i < expectedStreamCount; i++) {
        builder.addStreams(ReadStream.newBuilder().setName("stream-" + i));
    }
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.createReadSession(expectedRequest)).thenReturn(builder.build());
    BigQueryStorageQuerySource<TableRow> querySource = BigQueryStorageQuerySource.create(stepUuid, ValueProvider.StaticValueProvider.of(encodedQuery), /* flattenResults = */
    true, /* useLegacySql = */
    true, /* priority = */
    QueryPriority.BATCH, /* location = */
    null, /* queryTempDataset = */
    null, /* kmsKey = */
    null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withJobService(fakeJobService).withStorageClient(fakeStorageClient));
    List<? extends BoundedSource<TableRow>> sources = querySource.split(bundleSize, options);
    assertEquals(expectedStreamCount, sources.size());
}
Also used : JobStatistics(com.google.api.services.bigquery.model.JobStatistics) JobStatistics2(com.google.api.services.bigquery.model.JobStatistics2) Table(com.google.api.services.bigquery.model.Table) TableSchema(com.google.api.services.bigquery.model.TableSchema) AvroSchema(com.google.cloud.bigquery.storage.v1.AvroSchema) TableSchema(com.google.api.services.bigquery.model.TableSchema) Schema(org.apache.avro.Schema) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) ByteString(com.google.protobuf.ByteString) TableFieldSchema(com.google.api.services.bigquery.model.TableFieldSchema) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) TableReference(com.google.api.services.bigquery.model.TableReference) BigQueryResourceNaming.createTempTableReference(org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)

Example 7 with CreateReadSessionRequest

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

the class BigQueryIOStorageQueryTest method testQuerySourceInitialSplit_EmptyResult.

@Test
public void testQuerySourceInitialSplit_EmptyResult() throws Exception {
    TableReference sourceTableRef = BigQueryHelpers.parseTableSpec("project:dataset.table");
    fakeDatasetService.createDataset(sourceTableRef.getProjectId(), sourceTableRef.getDatasetId(), "asia-northeast1", "Fake plastic tree^H^H^H^Htables", null);
    fakeDatasetService.createTable(new Table().setTableReference(sourceTableRef).setLocation("asia-northeast1"));
    Table queryResultTable = new Table().setSchema(TABLE_SCHEMA).setNumBytes(0L);
    String encodedQuery = FakeBigQueryServices.encodeQueryResult(queryResultTable);
    fakeJobService.expectDryRunQuery(options.getProject(), encodedQuery, new JobStatistics().setQuery(new JobStatistics2().setTotalBytesProcessed(1024L * 1024L).setReferencedTables(ImmutableList.of(sourceTableRef))));
    String stepUuid = "testStepUuid";
    TableReference tempTableReference = createTempTableReference(options.getProject(), BigQueryResourceNaming.createJobIdPrefix(options.getJobName(), stepUuid, JobType.QUERY), Optional.empty());
    CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/" + options.getProject()).setReadSession(ReadSession.newBuilder().setTable(BigQueryHelpers.toTableResourceName(tempTableReference))).setMaxStreamCount(10).build();
    ReadSession emptyReadSession = ReadSession.newBuilder().build();
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.createReadSession(expectedRequest)).thenReturn(emptyReadSession);
    BigQueryStorageQuerySource<TableRow> querySource = BigQueryStorageQuerySource.create(stepUuid, ValueProvider.StaticValueProvider.of(encodedQuery), /* flattenResults = */
    true, /* useLegacySql = */
    true, /* priority = */
    QueryPriority.BATCH, /* location = */
    null, /* queryTempDataset = */
    null, /* kmsKey = */
    null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withJobService(fakeJobService).withStorageClient(fakeStorageClient));
    List<? extends BoundedSource<TableRow>> sources = querySource.split(1024L, options);
    assertTrue(sources.isEmpty());
}
Also used : JobStatistics(com.google.api.services.bigquery.model.JobStatistics) JobStatistics2(com.google.api.services.bigquery.model.JobStatistics2) Table(com.google.api.services.bigquery.model.Table) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) ByteString(com.google.protobuf.ByteString) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) TableReference(com.google.api.services.bigquery.model.TableReference) BigQueryResourceNaming.createTempTableReference(org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Example 8 with CreateReadSessionRequest

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

the class BigQueryIOStorageReadTest method testReadFromBigQueryIOArrow.

@Test
public void testReadFromBigQueryIOArrow() 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").setDataFormat(DataFormat.ARROW)).setMaxStreamCount(10).build();
    ReadSession readSession = ReadSession.newBuilder().setName("readSessionName").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).addStreams(ReadStream.newBuilder().setName("streamName")).setDataFormat(DataFormat.ARROW).build();
    ReadRowsRequest expectedReadRowsRequest = ReadRowsRequest.newBuilder().setReadStream("streamName").build();
    List<String> names = Arrays.asList("A", "B", "C", "D");
    List<Long> values = Arrays.asList(1L, 2L, 3L, 4L);
    List<ReadRowsResponse> readRowsResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.50), createResponseArrow(ARROW_SCHEMA, names.subList(2, 4), values.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<KV<String, Long>> output = p.apply(BigQueryIO.read(new ParseKeyValue()).from("foo.com:project:dataset.table").withMethod(Method.DIRECT_READ).withFormat(DataFormat.ARROW).withTestServices(new FakeBigQueryServices().withDatasetService(fakeDatasetService).withStorageClient(fakeStorageClient)));
    PAssert.that(output).containsInAnyOrder(ImmutableList.of(KV.of("A", 1L), KV.of("B", 2L), KV.of("C", 3L), KV.of("D", 4L)));
    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) ByteString(com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV) TableReference(com.google.api.services.bigquery.model.TableReference) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Example 9 with CreateReadSessionRequest

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

use of com.google.cloud.bigquery.storage.v1.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)

Aggregations

CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)14 Table (com.google.api.services.bigquery.model.Table)12 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)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 Test (org.junit.Test)11 TableRow (com.google.api.services.bigquery.model.TableRow)9 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)8 ByteString (com.google.protobuf.ByteString)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 BigQueryResourceNaming.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference)4 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)3 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)3 Schema (org.apache.avro.Schema)3 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)2 AvroSchema (com.google.cloud.bigquery.storage.v1.AvroSchema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2