use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method testColumnSelection.
@Test
public void testColumnSelection() throws IOException {
TableReference tableReference = TableReference.newBuilder().setProjectId("bigquery-public-data").setDatasetId("samples").setTableId("shakespeare").build();
TableReadOptions options = TableReadOptions.newBuilder().addSelectedFields("word").addSelectedFields("word_count").setRowRestriction("word_count > 100").build();
CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder().setParent(parentProjectId).setRequestedStreams(1).setTableReference(tableReference).setReadOptions(options).setFormat(DataFormat.AVRO).build();
ReadSession session = client.createReadSession(request);
assertEquals(String.format("Did not receive expected number of streams for table reference '%s' CreateReadSession response:%n%s", TextFormat.shortDebugString(tableReference), session.toString()), 1, session.getStreamsCount());
StreamPosition readPosition = StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
Schema avroSchema = new Schema.Parser().parse(session.getAvroSchema().getSchema());
String actualSchemaMessage = String.format("Unexpected schema. Actual schema:%n%s", avroSchema.toString(/* pretty = */
true));
assertEquals(actualSchemaMessage, Schema.Type.RECORD, avroSchema.getType());
assertEquals(actualSchemaMessage, "__root__", avroSchema.getName());
assertEquals(actualSchemaMessage, 2, avroSchema.getFields().size());
assertEquals(actualSchemaMessage, Schema.Type.STRING, avroSchema.getField("word").schema().getType());
assertEquals(actualSchemaMessage, Schema.Type.LONG, avroSchema.getField("word_count").schema().getType());
SimpleRowReader reader = new SimpleRowReader(avroSchema);
long rowCount = 0;
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
rowCount += response.getRowCount();
reader.processRows(response.getAvroRows(), new SimpleRowReader.AvroRowConsumer() {
@Override
public void accept(GenericData.Record record) {
String rowAssertMessage = String.format("Row not matching expectations: %s", record.toString());
Long wordCount = (Long) record.get("word_count");
assertWithMessage(rowAssertMessage).that(wordCount).isGreaterThan(100L);
Utf8 word = (Utf8) record.get("word");
assertWithMessage(rowAssertMessage).that(word.length()).isGreaterThan(0);
}
});
}
assertEquals(1_333, rowCount);
}
use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method testSimpleRead.
@Test
public void testSimpleRead() {
TableReference tableReference = TableReference.newBuilder().setProjectId("bigquery-public-data").setDatasetId("samples").setTableId("shakespeare").build();
ReadSession session = client.createReadSession(/* tableReference = */
tableReference, /* parent = */
parentProjectId, /* requestedStreams = */
1);
assertEquals(String.format("Did not receive expected number of streams for table reference '%s' CreateReadSession response:%n%s", TextFormat.shortDebugString(tableReference), session.toString()), 1, session.getStreamsCount());
StreamPosition readPosition = StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).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.v1beta1.Storage.Stream in project java-bigquerystorage by googleapis.
the class ITBigQueryStorageTest method ProcessRowsAtSnapshot.
/**
* Reads all the rows from the specified tableReference.
*
* <p>For every row, the consumer is called for processing.
*
* @param tableReference
* @param snapshotInMillis Optional. If specified, all rows up to timestamp will be returned.
* @param filter Optional. If specified, it will be used to restrict returned data.
* @param consumer that receives all Avro rows.
* @throws IOException
*/
private void ProcessRowsAtSnapshot(TableReference tableReference, Long snapshotInMillis, String filter, AvroRowConsumer consumer) throws IOException {
Preconditions.checkNotNull(tableReference);
Preconditions.checkNotNull(consumer);
CreateReadSessionRequest.Builder createSessionRequestBuilder = CreateReadSessionRequest.newBuilder().setParent(parentProjectId).setRequestedStreams(1).setTableReference(tableReference).setFormat(DataFormat.AVRO);
if (snapshotInMillis != null) {
Timestamp snapshotTimestamp = Timestamp.newBuilder().setSeconds(snapshotInMillis / 1_000).setNanos((int) ((snapshotInMillis % 1000) * 1000000)).build();
createSessionRequestBuilder.setTableModifiers(TableModifiers.newBuilder().setSnapshotTime(snapshotTimestamp).build());
}
if (filter != null && !filter.isEmpty()) {
createSessionRequestBuilder.setReadOptions(TableReadOptions.newBuilder().setRowRestriction(filter).build());
}
ReadSession session = client.createReadSession(createSessionRequestBuilder.build());
assertEquals(String.format("Did not receive expected number of streams for table reference '%s' CreateReadSession response:%n%s", TextFormat.shortDebugString(tableReference), session.toString()), 1, session.getStreamsCount());
StreamPosition readPosition = StreamPosition.newBuilder().setStream(session.getStreams(0)).build();
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadPosition(readPosition).build();
SimpleRowReader reader = new SimpleRowReader(new Schema.Parser().parse(session.getAvroSchema().getSchema()));
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
reader.processRows(response.getAvroRows(), consumer);
}
}
use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream in project java-bigquerystorage by googleapis.
the class BigQueryStorageClientTest method splitReadStreamExceptionTest.
@Test
@SuppressWarnings("all")
public void splitReadStreamExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockBigQueryStorage.addException(exception);
try {
Stream originalStream = Stream.newBuilder().build();
client.splitReadStream(originalStream);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception
}
}
use of com.google.cloud.bigquery.storage.v1beta1.Storage.Stream in project java-bigquerystorage by googleapis.
the class BigQueryStorageClientTest method finalizeStreamTest.
@Test
@SuppressWarnings("all")
public void finalizeStreamTest() {
Empty expectedResponse = Empty.newBuilder().build();
mockBigQueryStorage.addResponse(expectedResponse);
Stream stream = Stream.newBuilder().build();
client.finalizeStream(stream);
List<AbstractMessage> actualRequests = mockBigQueryStorage.getRequests();
Assert.assertEquals(1, actualRequests.size());
FinalizeStreamRequest actualRequest = (FinalizeStreamRequest) actualRequests.get(0);
Assert.assertEquals(stream, actualRequest.getStream());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations