use of com.google.cloud.bigquery.storage.v1beta2.ReadSession in project beam by apache.
the class BigQueryIOStorageQueryTest method testQuerySourceInitialSplitWithBigQueryProject_EmptyResult.
@Test
@ProjectOverride
public void testQuerySourceInitialSplitWithBigQueryProject_EmptyResult() throws Exception {
TableReference sourceTableRef = BigQueryHelpers.parseTableSpec("bigquery-project-id: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.getBigQueryProject(), encodedQuery, new JobStatistics().setQuery(new JobStatistics2().setTotalBytesProcessed(1024L * 1024L).setReferencedTables(ImmutableList.of(sourceTableRef))));
String stepUuid = "testStepUuid";
TableReference tempTableReference = createTempTableReference(options.getBigQueryProject(), BigQueryResourceNaming.createJobIdPrefix(options.getJobName(), stepUuid, JobType.QUERY), Optional.empty());
CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/" + options.getBigQueryProject()).setReadSession(ReadSession.newBuilder().setTable(BigQueryHelpers.toTableResourceName(tempTableReference)).setDataFormat(DataFormat.AVRO)).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, DataFormat.AVRO, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withJobService(fakeJobService).withStorageClient(fakeStorageClient));
List<? extends BoundedSource<TableRow>> sources = querySource.split(1024L, options);
assertTrue(sources.isEmpty());
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadSession in project beam by apache.
the class BigQueryIOStorageReadTest method testReadFromBigQueryIO.
@Test
public void testReadFromBigQueryIO() 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.AVRO)).setMaxStreamCount(10).build();
ReadSession readSession = ReadSession.newBuilder().setName("readSessionName").setAvroSchema(AvroSchema.newBuilder().setSchema(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", 1, AVRO_SCHEMA), createRecord("B", 2, AVRO_SCHEMA), createRecord("C", 3, AVRO_SCHEMA), createRecord("D", 4, AVRO_SCHEMA));
List<ReadRowsResponse> readRowsResponses = Lists.newArrayList(createResponse(AVRO_SCHEMA, records.subList(0, 2), 0.0, 0.50), createResponse(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<KV<String, Long>> output = p.apply(BigQueryIO.read(new ParseKeyValue()).from("foo.com:project:dataset.table").withMethod(Method.DIRECT_READ).withFormat(DataFormat.AVRO).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();
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadSession 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());
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadSession in project beam by apache.
the class BigQueryIOStorageReadTest method testTableSourceInitialSplit_EmptyTable.
@Test
public void testTableSourceInitialSplit_EmptyTable() 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(1024L * 1024L).setSchema(new TableSchema());
fakeDatasetService.createTable(table);
CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/project-id").setReadSession(ReadSession.newBuilder().setTable("projects/foo.com:project/datasets/dataset/tables/table")).setMaxStreamCount(1024).build();
ReadSession emptyReadSession = ReadSession.newBuilder().build();
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.createReadSession(expectedRequest)).thenReturn(emptyReadSession);
BigQueryStorageTableSource<TableRow> tableSource = BigQueryStorageTableSource.create(ValueProvider.StaticValueProvider.of(tableRef), null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withStorageClient(fakeStorageClient));
List<? extends BoundedSource<TableRow>> sources = tableSource.split(1024L, options);
assertTrue(sources.isEmpty());
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadSession 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());
}
Aggregations