|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +// [START bigquery_query_pagination] |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryException; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 24 | +import com.google.cloud.bigquery.TableId; |
| 25 | +import com.google.cloud.bigquery.TableResult; |
| 26 | + |
| 27 | +// Sample to run query with pagination. |
| 28 | +public class QueryPagination { |
| 29 | + |
| 30 | + public static void runQueryPagination() { |
| 31 | + String datasetName = "MY_DATASET_NAME"; |
| 32 | + String tableName = "MY_TABLE_NAME"; |
| 33 | + String query = |
| 34 | + "SELECT name, SUM(number) as total_people" |
| 35 | + + " FROM `bigquery-public-data.usa_names.usa_1910_2013`" |
| 36 | + + " GROUP BY name" |
| 37 | + + " ORDER BY total_people DESC" |
| 38 | + + " LIMIT 100"; |
| 39 | + queryPagination(datasetName, tableName, query); |
| 40 | + } |
| 41 | + |
| 42 | + public static void queryPagination(String datasetName, String tableName, String query) { |
| 43 | + try { |
| 44 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 45 | + // once, and can be reused for multiple requests. |
| 46 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 47 | + |
| 48 | + TableId tableId = TableId.of(datasetName, tableName); |
| 49 | + QueryJobConfiguration queryConfig = |
| 50 | + QueryJobConfiguration.newBuilder(query) |
| 51 | + // save results into a table. |
| 52 | + .setDestinationTable(tableId) |
| 53 | + .build(); |
| 54 | + |
| 55 | + bigquery.query(queryConfig); |
| 56 | + |
| 57 | + TableResult results = |
| 58 | + bigquery.listTableData(tableId, BigQuery.TableDataListOption.pageSize(20)); |
| 59 | + |
| 60 | + results |
| 61 | + .getNextPage() |
| 62 | + .iterateAll() |
| 63 | + .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString()))); |
| 64 | + |
| 65 | + System.out.println("Query pagination performed successfully."); |
| 66 | + } catch (BigQueryException | InterruptedException e) { |
| 67 | + System.out.println("Query not performed \n" + e.toString()); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +// [END bigquery_query_pagination] |
0 commit comments