use of com.google.cloud.bigquery.storage.v1beta2.ReadStream in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageLongRunningTest method testLongRunningReadSession.
@Test
public void testLongRunningReadSession() throws InterruptedException, ExecutionException {
// This test reads a larger table with the goal of doing a simple validation of timeout settings
// for a longer running session.
String table = BigQueryResource.FormatTableResource(/* projectId = */
"bigquery-public-data", /* datasetId = */
"samples", /* tableId = */
"wikipedia");
ReadSession session = client.createReadSession(/* parent = */
parentProjectId, /* readSession = */
ReadSession.newBuilder().setTable(table).setDataFormat(DataFormat.AVRO).build(), /* maxStreamCount = */
5);
assertEquals(String.format("Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", table, session.toString()), 5, session.getStreamsCount());
List<Callable<Long>> tasks = new ArrayList<>(session.getStreamsCount());
for (final ReadStream stream : session.getStreamsList()) {
tasks.add(new Callable<Long>() {
@Override
public Long call() throws Exception {
return readAllRowsFromStream(stream);
}
});
}
ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
List<Future<Long>> results = executor.invokeAll(tasks);
long rowCount = 0;
for (Future<Long> result : results) {
rowCount += result.get();
}
assertEquals(313_797_035, rowCount);
}
use of com.google.cloud.bigquery.storage.v1beta2.ReadStream 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.ReadStream in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageLongRunningTest method testLongRunningReadSession.
@Test
public void testLongRunningReadSession() throws InterruptedException, ExecutionException {
// This test reads a larger table with the goal of doing a simple validation of timeout settings
// for a longer running session.
String table = BigQueryResource.FormatTableResource(/* projectId = */
"bigquery-public-data", /* datasetId = */
"samples", /* tableId = */
"wikipedia");
ReadSession session = client.createReadSession(/* parent = */
parentProjectId, /* readSession = */
ReadSession.newBuilder().setTable(table).setDataFormat(DataFormat.AVRO).build(), /* maxStreamCount = */
5);
assertEquals(String.format("Did not receive expected number of streams for table '%s' CreateReadSession response:%n%s", table, session.toString()), 5, session.getStreamsCount());
List<Callable<Long>> tasks = new ArrayList<>(session.getStreamsCount());
for (final ReadStream stream : session.getStreamsList()) {
tasks.add(new Callable<Long>() {
@Override
public Long call() throws Exception {
return readAllRowsFromStream(stream);
}
});
}
ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
List<Future<Long>> results = executor.invokeAll(tasks);
long rowCount = 0;
for (Future<Long> result : results) {
rowCount += result.get();
}
assertEquals(313_797_035, rowCount);
}
Aggregations