0

使用以下方法,我从 BigQuery 获取数据

String query =
                "SELECT id, " +
                        "qtr, " +
                        "sales, " +
                        "year, " +
                        "comments " +
                        "FROM `gcs.database.testtable` " ;
        BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(projectId)
                .setCredentials(
                        ServiceAccountCredentials.fromStream(new
                                FileInputStream("key.json"))
                )
                .build().getService();
        TableId tableId = TableId.of(projectId, datasetName, tableName);
        QueryJobConfiguration queryConfig = QueryJobConfiguration
                .newBuilder(query)
                .setPriority(QueryJobConfiguration.Priority.BATCH)
                .build();
      
        TableResult results = bigquery.query(queryConfig);

        for (FieldValueList row : results.iterateAll()) {
            System.out.printf(
                    "ID: %s qtr: %s sales: %s year: %s comments: %s\n",
                    row.get(0).getValue(),
                    row.get(1).getValue(),
                    row.get(2).getValue(),
                    row.get(3).getValue(),
                    row.get(4).getValue()
            );
        }

这里,results.iterateAll()是从结果中获取所有记录。但我只想从结果中检索特定数据。

考虑到结果给出 10 条记录,我想根据从 0 开始的索引逐行迭代。

我没有找到任何方法来实现这一点。有人可以帮忙吗?

4

0 回答 0