Search in sources :

Example 11 with AvroRowConsumer

use of com.google.cloud.bigquery.storage.v1beta1.it.SimpleRowReader.AvroRowConsumer in project java-bigquerystorage by googleapis.

the class ITBigQueryStorageTest method testFilter.

@Test
public void testFilter() throws IOException {
    String table = BigQueryResource.FormatTableResource(/* projectId = */
    "bigquery-public-data", /* datasetId = */
    "samples", /* tableId = */
    "shakespeare");
    TableReadOptions options = TableReadOptions.newBuilder().setRowRestriction("word_count > 100").build();
    CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder().setParent(parentProjectId).setMaxStreamCount(1).setReadSession(ReadSession.newBuilder().setTable(table).setReadOptions(options).setDataFormat(DataFormat.AVRO).build()).build();
    ReadSession session = client.createReadSession(request);
    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();
    SimpleRowReader reader = new SimpleRowReader(new Schema.Parser().parse(session.getAvroSchema().getSchema()));
    long rowCount = 0;
    ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
    for (ReadRowsResponse response : stream) {
        rowCount += response.getRowCount();
        reader.processRows(response.getAvroRows(), new AvroRowConsumer() {

            @Override
            public void accept(GenericData.Record record) {
                Long wordCount = (Long) record.get("word_count");
                assertWithMessage("Row not matching expectations: %s", record.toString()).that(wordCount).isGreaterThan(100L);
            }
        });
    }
    assertEquals(1_333, rowCount);
}
Also used : AvroRowConsumer(com.google.cloud.bigquery.storage.v1.it.SimpleRowReader.AvroRowConsumer) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ReadRowsRequest(com.google.cloud.bigquery.storage.v1.ReadRowsRequest) GenericData(org.apache.avro.generic.GenericData) ReadRowsResponse(com.google.cloud.bigquery.storage.v1.ReadRowsResponse) TableReadOptions(com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions) CreateReadSessionRequest(com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest) Test(org.junit.Test)

Aggregations

GenericData (org.apache.avro.generic.GenericData)10 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 AvroRowConsumer (com.google.cloud.bigquery.storage.v1.it.SimpleRowReader.AvroRowConsumer)4 AvroRowConsumer (com.google.cloud.bigquery.storage.v1beta2.it.SimpleRowReader.AvroRowConsumer)4 Field (com.google.cloud.bigquery.Field)3 Job (com.google.cloud.bigquery.Job)3 TableId (com.google.cloud.bigquery.TableId)3 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)3 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)2 ReadRowsRequest (com.google.cloud.bigquery.storage.v1.ReadRowsRequest)2 ReadRowsResponse (com.google.cloud.bigquery.storage.v1.ReadRowsResponse)2 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)2 TableReadOptions (com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions)2 AvroRowConsumer (com.google.cloud.bigquery.storage.v1beta1.it.SimpleRowReader.AvroRowConsumer)2 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest)2 ReadRowsRequest (com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest)2 ReadRowsResponse (com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse)2 ReadSession (com.google.cloud.bigquery.storage.v1beta2.ReadSession)2 TableReadOptions (com.google.cloud.bigquery.storage.v1beta2.ReadSession.TableReadOptions)2