use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method testSimpleReadAndResume.
@Test
public void testSimpleReadAndResume() {
String table = BigQueryResource.FormatTableResource(/* projectId = */
"bigquery-public-data", /* datasetId = */
"samples", /* tableId = */
"shakespeare");
ReadSession session = client.createReadSession(/* parent = */
parentProjectId, /* readSession = */
ReadSession.newBuilder().setTable(table).setDataFormat(DataFormat.AVRO).build(), /* maxStreamCount = */
1);
assertEquals(String.format("Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", table, session.toString()), 1, session.getStreamsCount());
// We have to read some number of rows in order to be able to resume. More details:
long rowCount = ReadStreamToOffset(session.getStreams(0), /* rowOffset = */
34_846);
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(session.getStreams(0).getName()).setOffset(rowCount).build();
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
rowCount += response.getRowCount();
}
// Verifies that the number of rows skipped and read equals to the total number of rows in the
// table.
assertEquals(164_656, rowCount);
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method ReadStreamToOffset.
/**
* Reads to the specified row offset within the stream. If the stream does not have the desired
* rows to read, it will read all of them.
*
* @param readStream
* @param rowOffset
* @return the number of requested rows to skip or the total rows read if stream had less rows.
*/
private long ReadStreamToOffset(ReadStream readStream, long rowOffset) {
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(readStream.getName()).build();
long rowCount = 0;
ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(readRowsRequest);
Iterator<ReadRowsResponse> responseIterator = serverStream.iterator();
while (responseIterator.hasNext()) {
ReadRowsResponse response = responseIterator.next();
rowCount += response.getRowCount();
if (rowCount >= rowOffset) {
return rowOffset;
}
}
return rowCount;
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method testSimpleRead.
@Test
public void testSimpleRead() {
String table = BigQueryResource.FormatTableResource(/* projectId = */
"bigquery-public-data", /* datasetId = */
"samples", /* tableId = */
"shakespeare");
ReadSession session = client.createReadSession(/* parent = */
parentProjectId, /* readSession = */
ReadSession.newBuilder().setTable(table).setDataFormat(DataFormat.AVRO).build(), /* maxStreamCount = */
1);
assertEquals(String.format("Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", table, session.toString()), 1, session.getStreamsCount());
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(session.getStreams(0).getName()).build();
long rowCount = 0;
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
rowCount += response.getRowCount();
}
assertEquals(164_656, rowCount);
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project java-bigquerystorage by googleapis.
the class ReadRowsRetryTest method getRowCount.
private int getRowCount(ReadRowsRequest request) {
ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(request);
int rowCount = 0;
for (ReadRowsResponse readRowsResponse : serverStream) {
rowCount += readRowsResponse.getRowCount();
}
return rowCount;
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageLongRunningTest method readAllRowsFromStream.
private long readAllRowsFromStream(ReadStream readStream) {
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(readStream.getName()).build();
long rowCount = 0;
ServerStream<ReadRowsResponse> serverStream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : serverStream) {
rowCount += response.getRowCount();
}
LOG.info(String.format("Read total of %d rows from stream '%s'.", rowCount, readStream.getName()));
return rowCount;
}
Aggregations