Search in sources :

Example 21 with TableRowParser

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

the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionRepeated.

@Test
public void testStreamSourceSplitAtFractionRepeated() throws Exception {
    List<ReadStream> readStreams = Lists.newArrayList(ReadStream.newBuilder().setName("stream1").build(), ReadStream.newBuilder().setName("stream2").build(), ReadStream.newBuilder().setName("stream3").build());
    StorageClient fakeStorageClient = mock(StorageClient.class);
    // Mock the initial ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(0).getName()).build(), "")).thenReturn(new FakeBigQueryServerStream<>(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), createRecord("D", 4, AVRO_SCHEMA)), 0.25, 0.50), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("E", 5, AVRO_SCHEMA), createRecord("F", 6, AVRO_SCHEMA)), 0.5, 0.75))));
    // Mock the first SplitReadStream call.
    when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName(readStreams.get(0).getName()).setFraction(0.83f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(readStreams.get(1)).setRemainderStream(ReadStream.newBuilder().setName("ignored")).build());
    // Mock the second ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(1).getName()).setOffset(1).build(), "")).thenReturn(new FakeBigQueryServerStream<>(Lists.newArrayList(createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("B", 2, AVRO_SCHEMA), createRecord("C", 3, AVRO_SCHEMA)), 0.0, 0.50), createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("D", 4, AVRO_SCHEMA), createRecord("E", 5, AVRO_SCHEMA)), 0.5, 0.75))));
    // Mock the second SplitReadStream call.
    when(fakeStorageClient.splitReadStream(SplitReadStreamRequest.newBuilder().setName(readStreams.get(1).getName()).setFraction(0.75f).build())).thenReturn(SplitReadStreamResponse.newBuilder().setPrimaryStream(readStreams.get(2)).setRemainderStream(ReadStream.newBuilder().setName("ignored")).build());
    // Mock the third ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(2).getName()).setOffset(2).build(), "")).thenReturn(new FakeBigQueryServerStream<>(Lists.newArrayList(createResponse(AVRO_SCHEMA, Lists.newArrayList(createRecord("C", 3, AVRO_SCHEMA), createRecord("D", 4, AVRO_SCHEMA)), 0.80, 0.90))));
    BoundedSource<TableRow> source = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setAvroSchema(AvroSchema.newBuilder().setSchema(AVRO_SCHEMA_STRING)).build(), readStreams.get(0), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withStorageClient(fakeStorageClient));
    BoundedReader<TableRow> reader = source.createReader(options);
    assertTrue(reader.start());
    assertEquals("A", reader.getCurrent().get("name"));
    BoundedSource<TableRow> residualSource = reader.splitAtFraction(0.83f);
    assertNotNull(residualSource);
    assertEquals("A", reader.getCurrent().get("name"));
    assertTrue(reader.advance());
    assertEquals("B", reader.getCurrent().get("name"));
    residualSource = reader.splitAtFraction(0.75f);
    assertNotNull(residualSource);
    assertEquals("B", reader.getCurrent().get("name"));
    assertTrue(reader.advance());
    assertEquals("C", reader.getCurrent().get("name"));
    assertTrue(reader.advance());
    assertEquals("D", reader.getCurrent().get("name"));
    assertFalse(reader.advance());
}
Also used : ReadStream(com.google.cloud.bigquery.storage.v1.ReadStream) 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 22 with TableRowParser

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

the class BigQueryIOStorageReadTest method testBuildTableBasedSourceWithoutValidation.

@Test
public void testBuildTableBasedSourceWithoutValidation() {
    BigQueryIO.TypedRead<TableRow> typedRead = BigQueryIO.read(new TableRowParser()).withCoder(TableRowJsonCoder.of()).withMethod(Method.DIRECT_READ).from("foo.com:project:dataset.table").withoutValidation();
    checkTypedReadTableObject(typedRead, "foo.com:project", "dataset", "table");
    assertFalse(typedRead.getValidate());
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) TableRowParser(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser) Test(org.junit.Test)

Example 23 with TableRowParser

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

the class BigQueryIOStorageReadTest method testStreamSourceEstimatedSizeBytes.

@Test
public void testStreamSourceEstimatedSizeBytes() throws Exception {
    BigQueryStorageStreamSource<TableRow> streamSource = BigQueryStorageStreamSource.create(ReadSession.getDefaultInstance(), ReadStream.getDefaultInstance(), TABLE_SCHEMA, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices());
    assertEquals(0, streamSource.getEstimatedSizeBytes(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 24 with TableRowParser

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

the class BigQueryIOStorageReadTest method testReadFromStreamSourceArrow.

@Test
public void testReadFromStreamSourceArrow() 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");
    List<Long> values = Arrays.asList(1L, 2L, 3L);
    List<ReadRowsResponse> responses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(0, 2), values.subList(0, 2), 0.0, 0.50), createResponseArrow(ARROW_SCHEMA, names.subList(2, 3), values.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) 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 25 with TableRowParser

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

the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPoint.

@Test
public void testStreamSourceSplitAtFractionFailsWhenParentIsPastSplitPoint() 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.
    // 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(), "")).thenThrow(new FailedPreconditionException("Given row offset is invalid for stream.", new StatusRuntimeException(Status.FAILED_PRECONDITION), GrpcStatusCode.of(Code.FAILED_PRECONDITION), /* retryable = */
    false));
    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 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) FailedPreconditionException(com.google.api.gax.rpc.FailedPreconditionException) StatusRuntimeException(io.grpc.StatusRuntimeException) 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)

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