Search in sources :

Example 6 with ReadStream

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

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

the class BigQueryIOStorageReadTest method testStreamSourceSplitAtFractionRepeatedArrow.

@Test
public void testStreamSourceSplitAtFractionRepeatedArrow() 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);
    List<String> names = Arrays.asList("A", "B", "C", "D", "E", "F");
    List<Long> values = Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L);
    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, 4), values.subList(2, 4), 0.25, 0.5), createResponseArrow(ARROW_SCHEMA, names.subList(4, 6), values.subList(4, 6), 0.5, 0.75));
    // Mock the initial ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(0).getName()).build(), "")).thenReturn(new FakeBigQueryServerStream<>(parentResponses));
    // 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());
    List<ReadRowsResponse> otherResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(1, 3), values.subList(1, 3), 0.0, 0.50), createResponseArrow(ARROW_SCHEMA, names.subList(3, 4), values.subList(3, 4), 0.5, 0.75));
    // Mock the second ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(1).getName()).setOffset(1).build(), "")).thenReturn(new FakeBigQueryServerStream<>(otherResponses));
    // 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());
    List<ReadRowsResponse> lastResponses = Lists.newArrayList(createResponseArrow(ARROW_SCHEMA, names.subList(2, 4), values.subList(2, 4), 0.80, 0.90));
    // Mock the third ReadRows call.
    when(fakeStorageClient.readRows(ReadRowsRequest.newBuilder().setReadStream(readStreams.get(2).getName()).setOffset(2).build(), "")).thenReturn(new FakeBigQueryServerStream<>(lastResponses));
    BoundedSource<TableRow> source = BigQueryStorageStreamSource.create(ReadSession.newBuilder().setName("readSession").setArrowSchema(ArrowSchema.newBuilder().setSerializedSchema(serializeArrowSchema(ARROW_SCHEMA)).build()).setDataFormat(DataFormat.ARROW).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 : 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) ReadStream(com.google.cloud.bigquery.storage.v1.ReadStream) TableRow(com.google.api.services.bigquery.model.TableRow) FakeBigQueryServices(org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices) Test(org.junit.Test)

Aggregations

StorageClient (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.StorageClient)7 TableRow (com.google.api.services.bigquery.model.TableRow)6 TableRowParser (org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TableRowParser)6 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)6 Test (org.junit.Test)6 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)5 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)5 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)4 ReadStream (com.google.cloud.bigquery.storage.v1.ReadStream)3 ByteString (com.google.protobuf.ByteString)3 ArrayList (java.util.ArrayList)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 Table (com.google.api.services.bigquery.model.Table)1 TableSchema (com.google.api.services.bigquery.model.TableSchema)1 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)1 Schema (org.apache.avro.Schema)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1