Search in sources :

Example 1 with TableRowParser

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser in project beam by apache.

the class BigQueryIOStorageReadTest method testFractionConsumedArrow.

@Test
public void testFractionConsumedArrow() throws Exception {
    ReadSession readSession = ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).build();
    ReadRowsRequest expectedRequest = ReadRowsRequest.newBuilder().setReadStream("readStream").build();
    List<String> names = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
    List<Long> values = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L);
    List<ReadRowsResponse> responses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.25), createResponseArrow(ARROW_SCHEMA, Lists.newArrayList(), Lists.newArrayList(), 0.25, 0.25), createResponseArrow(ARROW_SCHEMA, names.subList(2, 4), values.subList(2, 4), 0.3, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(4, 7), values.subList(4, 7), 0.7, 1.0));
    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));
    BoundedReader<TableRow> reader = streamSource.createReader(options);
    // Before call to BoundedReader#start, fraction consumed must be zero.
    assertEquals(0.0, reader.getFractionConsumed(), DELTA);
    // Reads A.
    assertTrue(reader.start());
    assertEquals(0.125, reader.getFractionConsumed(), DELTA);
    // Reads B.
    assertTrue(reader.advance());
    assertEquals(0.25, reader.getFractionConsumed(), DELTA);
    // Reads C.
    assertTrue(reader.advance());
    assertEquals(0.4, reader.getFractionConsumed(), DELTA);
    // Reads D.
    assertTrue(reader.advance());
    assertEquals(0.5, reader.getFractionConsumed(), DELTA);
    // Reads E.
    assertTrue(reader.advance());
    assertEquals(0.8, reader.getFractionConsumed(), DELTA);
    // Reads F.
    assertTrue(reader.advance());
    assertEquals(0.9, reader.getFractionConsumed(), DELTA);
    // Reads G.
    assertTrue(reader.advance());
    assertEquals(1.0, reader.getFractionConsumed(), DELTA);
    // Reaches the end.
    assertFalse(reader.advance());
    // We are done with the stream, so we should report 100% consumption.
    assertEquals(Double.valueOf(1.0), reader.getFractionConsumed());
}
Also used : 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) 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) Test(org.junit.Test)

Example 2 with TableRowParser

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser in project beam by apache.

the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionFailsWhenSplitIsNotPossible.

@Test
public void testStreamSourceSplitAtFractionFailsWhenSplitIsNotPossible() throws Exception {
    List<ReadRowsResponse> parentResponses = Lists.newArrayList(createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("A", 1, AVRO_SCHEMA), createRecord("B", 2, AVRO_SCHEMA)), 0.0, 0.25), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("C", 3, AVRO_SCHEMA)), 0.25, 0.50), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("D", 4, AVRO_SCHEMA), createRecord("E", 5, AVRO_SCHEMA)), 0.5, 0.75));
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("parentStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
    // Mocks the split call. A response without a primary_stream and remainder_stream means
    // that the split is not possible.
    when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName("parentStream").setFraction(0.5f).build())).thenReturn(SplitReadStreamResponse.getDefaultInstance());
    BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).build(), ReadStream.newBuilder().setName("parentStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
    // Read a few records from the parent stream and ensure that records are returned in the
    // prescribed order.
    BoundedReader<TableRow> parent = streamSource.createReader(options);
    assertTrue(parent.start());
    assertEquals("A", parent.getCurrent().get("name"));
    assertTrue(parent.advance());
    assertEquals("B", parent.getCurrent().get("name"));
    assertNull(parent.splitAtFraction(0.5));
    verify(fakeStorageClient, times(1)).splitReadStream(ArgumentMatchers.any());
    // Verify that subsequent splitAtFraction() calls after a failed splitAtFraction() attempt
    // do NOT invoke SplitReadStream.
    assertNull(parent.splitAtFraction(0.5));
    verify(fakeStorageClient, times(1)).splitReadStream(ArgumentMatchers.any());
    // Verify that the parent source still works okay even after an unsuccessful split attempt.
    assertTrue(parent.advance());
    assertEquals("C", parent.getCurrent().get("name"));
    assertTrue(parent.advance());
    assertEquals("D", parent.getCurrent().get("name"));
    assertTrue(parent.advance());
    assertEquals("E", parent.getCurrent().get("name"));
    assertFalse(parent.advance());
}
Also used : ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableRow(com.google.api.services.bigquery.model.TableRow) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 3 with TableRowParser

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser in project beam by apache.

the class BigQueryIOStorageReadTest method testTableSourceInitialSplit_WithDefaultProject.

@Test
public void testTableSourceInitialSplit_WithDefaultProject() throws Exception {
    fakeDatasetService.createDataset("project-id", "dataset", "", "", null);
    TableReference tableRef = BigQueryHelpers.parseTableSpec("project-id:dataset.table");
    Table table = new Table().setTableReference(tableRef).setNumBytes(1024L * 1024L).setSchema(TABLE_SCHEMA);
    fakeDatasetService.createTable(table);
    CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/project-id").setReadSession(ReadSession.newBuilder().setTable("projects/project-id/datasets/dataset/tables/table")).setMaxStreamCount(1024).build();
    ReadSession.Builder builder = ReadSession.newBuilder().setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).setDataFormat(DataFormat.AVRO);
    for (int i = 0; i < 50; i++) {
        builder.addStreams(ReadStream.newBuilder().setName("stream-" + i));
    }
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.createReadSession(expectedRequest)).thenReturn(builder.build());
    BigQueryStorageTableSource<TableRow> tableSource = BigQueryStorageTableSource.create(ValueProvider.StaticValueProvider.of(BigQueryHelpers.parseTableSpec("dataset.table")), null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withStorageClient(fakeStorageClient));
    List<? extends BoundedSource<TableRow>> sources = tableSource.split(1024L, options);
    assertEquals(50L, sources.size());
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) TableRow(com.google.api.services.bigquery.model.TableRow) StorageClient(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 4 with TableRowParser

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser in project beam by apache.

the class BigQueryIOStorageReadTest method testFractionConsumed.

@Test
public void testFractionConsumed() 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), createRecord("D", 4, AVRO_SCHEMA), createRecord("E", 5, AVRO_SCHEMA), createRecord("F", 6, AVRO_SCHEMA), createRecord("G", 7, AVRO_SCHEMA));
    List<ReadRowsResponse> responses = Lists.newArrayList(createResponse(AVRO_SCHEMA, records.subList(0, 2), 0.0, 0.25), // to such responses.
    createResponse(AVRO_SCHEMA, Lists.newArrayList(), 0.25, 0.25), createResponse(AVRO_SCHEMA, records.subList(2, 4), 0.3, 0.5), createResponse(AVRO_SCHEMA, records.subList(4, 7), 0.7, 1.0));
    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));
    BoundedReader<TableRow> reader = streamSource.createReader(options);
    // Before call to BoundedReader#start, fraction consumed must be zero.
    assertEquals(0.0, reader.getFractionConsumed(), DELTA);
    // Reads A.
    assertTrue(reader.start());
    assertEquals(0.125, reader.getFractionConsumed(), DELTA);
    // Reads B.
    assertTrue(reader.advance());
    assertEquals(0.25, reader.getFractionConsumed(), DELTA);
    // Reads C.
    assertTrue(reader.advance());
    assertEquals(0.4, reader.getFractionConsumed(), DELTA);
    // Reads D.
    assertTrue(reader.advance());
    assertEquals(0.5, reader.getFractionConsumed(), DELTA);
    // Reads E.
    assertTrue(reader.advance());
    assertEquals(0.8, reader.getFractionConsumed(), DELTA);
    // Reads F.
    assertTrue(reader.advance());
    assertEquals(0.9, reader.getFractionConsumed(), DELTA);
    // Reads G.
    assertTrue(reader.advance());
    assertEquals(1.0, reader.getFractionConsumed(), DELTA);
    // Reaches the end.
    assertFalse(reader.advance());
    // We are done with the stream, so we should report 100% consumption.
    assertEquals(Double.valueOf(1.0), reader.getFractionConsumed());
}
Also used : 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) 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 5 with TableRowParser

use of org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser in project beam by apache.

the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionSucceedsArrow.

@Test
public void testStreamSourceSplitAtFractionSucceedsArrow() throws Exception {
    List<String> names = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
    List<Long> values = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L);
    List<ReadRowsResponse> parentResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.25), createResponseArrow(ARROW_SCHEMA, names.subList(2, 3), values.subList(2, 3), 0.25, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(3, 5), values.subList(3, 5), 0.5, 0.75));
    StorageClient fakeStorageClient = mock(StorageClient.class);
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("parentStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
    // Mocks the split call.
    when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName("parentStream").setFraction(0.5f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(ReadStream.newBuilder().setName("primaryStream")).setRemainderStream(ReadStream.newBuilder().setName("remainderStream")).build());
    // Mocks the ReadRows calls expected on the primary and residual streams.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("primaryStream").setOffset(2).build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses.subList(1, 2)));
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream("remainderStream").build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses.subList(2, parentResponses.size())));
    BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).build(), ReadStream.newBuilder().setName("parentStream").build(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
    // Read a few records from the parent stream and ensure that records are returned in the
    // prescribed order.
    BoundedReader<TableRow> parent = streamSource.createReader(options);
    assertTrue(parent.start());
    assertEquals("A", parent.getCurrent().get("name"));
    assertTrue(parent.advance());
    assertEquals("B", parent.getCurrent().get("name"));
    // Now split the stream, and ensure that the "parent" reader has been replaced with the
    // primary stream and that the returned source points to the residual stream.
    BoundedReader<TableRow> primary = parent;
    BoundedSource<TableRow> residualSource = parent.splitAtFraction(0.5);
    assertNotNull(residualSource);
    BoundedReader<TableRow> residual = residualSource.createReader(options);
    assertTrue(primary.advance());
    assertEquals("C", primary.getCurrent().get("name"));
    assertFalse(primary.advance());
    assertTrue(residual.start());
    assertEquals("D", residual.getCurrent().get("name"));
    assertTrue(residual.advance());
    assertEquals("E", residual.getCurrent().get("name"));
    assertFalse(residual.advance());
}
Also used : 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) 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) Test(org.junit.Test)

Aggregations

TableRow (com.google.api.services.bigquery.model.TableRow)34 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)34 Test (org.junit.Test)31 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)28 StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)22 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)14 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)13 ByteString (com.google.protobuf.ByteString)13 TableReference (com.google.api.services.bigquery.model.TableReference)12 Table (com.google.api.services.bigquery.model.Table)11 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)8 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)6 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)5 JobStatistics2 (com.google.api.services.bigquery.model.JobStatistics2)5 BigQueryResourceNaming.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference)4 TableSchema (com.google.api.services.bigquery.model.TableSchema)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 FailedPreconditionException (com.google.api.gax.rpc.FailedPreconditionException)2 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)2 AvroSchema (com.google.cloud.bigquery.storage.v1.AvroSchema)2