Search in sources :

Example 11 with FakeBigQueryServices

use of org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices in project beam by apache.

the class BigQueryIOStorageReadTest method testTableSourceEstimatedSize_WithDefaultProject.

@Test
public void testTableSourceEstimatedSize_WithDefaultProject() throws Exception {
    fakeDatasetService.createDataset("project-id", "dataset", "", "", null);
    TableReference tableRef = BigQueryHelpers.parseTableSpec("project-id:dataset.table");
    Table table = new Table().setTableReference(tableRef).setNumBytes(100L);
    fakeDatasetService.createTable(table);
    BigQueryStorageTableSource<TableRow> tableSource = BigQueryStorageTableSource.create(ValueProvider.StaticValueProvider.of(BigQueryHelpers.parseTableSpec("dataset.table")), null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService));
    assertEquals(100, tableSource.getEstimatedSizeBytes(options));
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 12 with FakeBigQueryServices

use of org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices in project beam by apache.

the class BigQueryIOStorageReadTest method testReadFromStreamSource.

@Test
public void testReadFromStreamSource() throws Exception {
    ReadSession readSession = ReadSession.newBuilder().setName("readSession").setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).build();
    ReadRowsRequest expectedRequest = ReadRowsRequest.newBuilder().setReadStream("readStream").build();
    List<GenericRecord> records = Lists.newArrayList(createRecord("A", 1, AVRO_SCHEMA), createRecord("B", 2, AVRO_SCHEMA), createRecord("C", 3, AVRO_SCHEMA));
    List<ReadRowsResponse> responses = Lists.newArrayList(createResponse(AVRO_SCHEMA, records.subList(0, 2), 0.0, 0.50), createResponse(AVRO_SCHEMA, records.subList(2, 3), 0.5, 0.75));
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.readRows(expectedRequest, "")).thenReturn(new FakeBigQueryServerStream<>(responses));
    BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(readSession, ReadStream.newBuilder().setName("readStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
    List<TableRow> rows = new ArrayList<>();
    BoundedReader<TableRow> reader = streamSource.createReader(options);
    for (boolean hasNext = reader.start(); hasNext; hasNext = reader.advance()) {
        rows.add(reader.getCurrent());
    }
    System.out.println("Rows: " + rows);
    assertEquals(3, rows.size());
}
Also used : ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ArrayList(java.util.ArrayList) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) 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) Test(org.junit.Test)

Example 13 with FakeBigQueryServices

use of org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices in project beam by apache.

the class BigQueryIOStorageReadTest method testTableSourceEstimatedSize_WithBigQueryProject.

@Test
@ProjectOverride
public void testTableSourceEstimatedSize_WithBigQueryProject() throws Exception {
    fakeDatasetService.createDataset("bigquery-project-id", "dataset", "", "", null);
    TableReference tableRef = BigQueryHelpers.parseTableSpec("bigquery-project-id:dataset.table");
    Table table = new Table().setTableReference(tableRef).setNumBytes(100L);
    fakeDatasetService.createTable(table);
    BigQueryStorageTableSource<TableRow> tableSource = BigQueryStorageTableSource.create(ValueProvider.StaticValueProvider.of(BigQueryHelpers.parseTableSpec("dataset.table")), null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService));
    assertEquals(100, tableSource.getEstimatedSizeBytes(options));
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 14 with FakeBigQueryServices

use of org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices in project beam by apache.

the class BigQueryIOStorageReadTest method testTableSourceCreateReader.

@Test
public void testTableSourceCreateReader() throws Exception {
    BigQueryStorageTableSource<TableRow> tableSource = BigQueryStorageTableSource.create(ValueProvider.StaticValueProvider.of(BigQueryHelpers.parseTableSpec("foo.com:project:dataset.table")), null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService));
    thrown.expect(UnsupportedOperationException.class);
    thrown.expectMessage("BigQuery storage source must be split before reading");
    tableSource.createReader(options);
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 15 with FakeBigQueryServices

use of org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices in project beam by apache.

the class BigQueryIOStorageReadTest method testStreamSourceSplit.

@Test
public void testStreamSourceSplit() throws Exception {
    BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.getDefaultInstance(), ReadStream.getDefaultInstance(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices());
    assertThat(streamSource.split(0, options), containsInAnyOrder(streamSource));
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Aggregations

FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)35 TableRow (com.google.api.services.bigquery.model.TableRow)32 Test (org.junit.Test)29 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)28 StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)26 Table (com.google.api.services.bigquery.model.Table)18 TableReference (com.google.api.services.bigquery.model.TableReference)18 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)18 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)17 ByteString (com.google.protobuf.ByteString)15 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)11 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)10 BigQueryResourceNaming.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference)8 TableSchema (com.google.api.services.bigquery.model.TableSchema)6 GenericRecord (org.apache.avro.generic.GenericRecord)6 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)5 JobStatistics2 (com.google.api.services.bigquery.model.JobStatistics2)5 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)5 KV (org.apache.beam.sdk.values.KV)5 FakeJobService (org.apache.beam.sdk.io.gcp.testing.FakeJobService)3