Skip to content

Commit

Permalink
docs(samples): add query pagination (#596)
Browse files Browse the repository at this point in the history
* docs(samples): add query pagination

* docs(samples): update code
  • Loading branch information
Praful Makani authored Jul 24, 2020
1 parent 4787dd0 commit 918c8bd
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_query_pagination]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableResult;

// Sample to run query with pagination.
public class QueryPagination {

public static void runQueryPagination() {
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
String query =
"SELECT name, SUM(number) as total_people"
+ " FROM `bigquery-public-data.usa_names.usa_1910_2013`"
+ " GROUP BY name"
+ " ORDER BY total_people DESC"
+ " LIMIT 100";
queryPagination(datasetName, tableName, query);
}

public static void queryPagination(String datasetName, String tableName, String query) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

TableId tableId = TableId.of(datasetName, tableName);
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query)
// save results into a table.
.setDestinationTable(tableId)
.build();

bigquery.query(queryConfig);

TableResult results =
bigquery.listTableData(tableId, BigQuery.TableDataListOption.pageSize(20));

results
.getNextPage()
.iterateAll()
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));

System.out.println("Query pagination performed successfully.");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query not performed \n" + e.toString());
}
}
}
// [END bigquery_query_pagination]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.TestCase.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class QueryPaginationIT {

private String tableName;
private ByteArrayOutputStream bout;
private PrintStream out;

private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");

private static String requireEnvVar(String varName) {
String value = System.getenv(varName);
assertNotNull(
"Environment variable " + varName + " is required to perform these tests.",
System.getenv(varName));
return value;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("BIGQUERY_DATASET_NAME");
}

@Before
public void setUp() {
tableName =
"QUERY_PAGINATION_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
// Clean up
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
System.setOut(null);
}

@Test
public void testQueryPagination() {
String query =
"SELECT name, SUM(number) as total_people"
+ " FROM `bigquery-public-data.usa_names.usa_1910_2013`"
+ " GROUP BY name"
+ " ORDER BY total_people DESC"
+ " LIMIT 100";
QueryPagination.queryPagination(BIGQUERY_DATASET_NAME, tableName, query);
assertThat(bout.toString()).contains("Query pagination performed successfully.");
}
}

0 comments on commit 918c8bd

Please sign in to comment.