How do I access the cacheHit property of my BigQue

2019-08-09 13:53发布

问题:

Apparently, when using the BigQuery API, there is a cacheHit property of a BigQuery result. I've tried finding this property and I'm not sure how I need to access it. Here's my Java code that uses the BigQuery API. cacheHit isn't a property of the TableResult tr that I get:

try
{
    QueryJobConfiguration queryJobConfiguration =

        QueryJobConfiguration.newBuilder(
                "mySQLqueryText"
        )
        .setUseLegacySql(false)
        .setAllowLargeResults(false)
        .setUseQueryCache(true)
        .build();

    try {
        TableResult tr = bigQuery.query(queryJobConfiguration);

        Iterable<FieldValueList> rowList = tr.getValues();

        ....
    }
    catch (BigQueryException e) {
        // do stuff
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

I looked at this question - BigQuery cacheHit property

... but that's not Java, and I haven't found any results() property I can use, as suggested in that question.

There's some documentation here about the JobStatistics2 object, that apparently has a cacheHit property.

I can get a JobStatistics (not a JobStatistics2 object), like this:

QueryJobConfiguration queryJobConfiguration =

    QueryJobConfiguration.newBuilder(
            "myQueryString"
    )
    .setUseLegacySql(false)
    .setAllowLargeResults(false)
    .setUseQueryCache(true)
    .build();

    JobId jobId = JobId.of(UUID.randomUUID().toString());
    Job queryJob = bigQuery.create(JobInfo.newBuilder(queryJobConfiguration).setJobId(jobId).build());

try {

    queryJob = queryJob.waitFor();

    if (queryJob != null) {

        JobStatistics js = queryJob.getStatistics();

    Iterable<FieldValueList> rowList = bigQuery.query(queryJobConfiguration).getValues();

... but I don't see any cacheHit property on js. When I try creating a JobStatistics2 instead, by changing the line where I'm instantiating JobStatistics, like this:

JobStatistics2 js = queryJob.getStatistics();

I get an error Type parameter S has incompatible upper bounds: JobStatistics and JobStatistics2. This doesn't mean much, and when I Google the error there are no useful results.

I'm not finding the Google documentation too useful. How can I access the cacheHit property, and still obtain my rowList as shown in the code example?

回答1:

QueryStatistics is one of the nested classes of JobStatistics as can be seen here and has a getCacheHit() method:

import com.google.cloud.bigquery.JobStatistics.QueryStatistics;

...

QueryStatistics js = queryJob.getStatistics();

System.out.println(js.getCacheHit());

See full code here for my test.

Regarding JobStatistics2 this is for com.google.api.services.bigquery library and not com.google.cloud.bigquery. In that case you could use getQuery() from JobStatistics to get a JobStatistics2 object and then use getCacheHit().