【问题标题】:How do I access the cacheHit property of my BigQuery result set?如何访问我的 BigQuery 结果集的 cacheHit 属性?
【发布时间】:2019-11-30 03:18:51
【问题描述】:

显然,在使用 BigQuery API 时,BigQuery 结果有一个cacheHit 属性。我试过找到这个属性,但我不确定我需要如何访问它。这是我使用 BigQuery API 的 Java 代码。 cacheHit 不是我得到的 TableResult tr 的属性:

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();
}

我看了这个问题 - BigQuery cacheHit property

...但这不是 Java,我还没有找到我可以使用的任何 results() 属性,正如该问题中所建议的那样。

这里有一些关于 JobStatistics2 对象的文档,它显然有一个 cacheHit 属性。

我可以得到一个JobStatistics不是一个JobStatistics2对象),像这样:

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();

...但我在js 上看不到任何cacheHit 属性。当我尝试创建JobStatistics2 时,通过更改我实例化JobStatistics 的行,如下所示:

JobStatistics2 js = queryJob.getStatistics();

我收到一个错误Type parameter S has incompatible upper bounds: JobStatistics and JobStatistics2。这并不意味着什么,当我用谷歌搜索错误时,没有有用的结果。

我没有发现 Google 文档太有用。如代码示例所示,如何访问cacheHit 属性,并仍然获得我的rowList

【问题讨论】:

    标签: java google-cloud-platform google-bigquery


    【解决方案1】:

    QueryStatisticsJobStatistics 的嵌套类之一,可以看到here 并有一个getCacheHit() 方法:

    import com.google.cloud.bigquery.JobStatistics.QueryStatistics;
    
    ...
    
    QueryStatistics js = queryJob.getStatistics();
    
    System.out.println(js.getCacheHit());
    

    查看完整代码here 进行我的测试。

    关于JobStatistics2,这是针对com.google.api.services.bigquery 库而不是com.google.cloud.bigquery。在这种情况下,您可以使用JobStatistics 中的getQuery() 来获取JobStatistics2 对象,然后使用getCacheHit()

    【讨论】:

    • 很棒的答案 - 我在 StackOverflow 上看到的最好的努力之一。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2020-04-14
    相关资源
    最近更新 更多