Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 918c8bd

Browse files
author
Praful Makani
authoredJul 24, 2020
docs(samples): add query pagination (#596)
* docs(samples): add query pagination * docs(samples): update code
1 parent 4787dd0 commit 918c8bd

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class QueryPaginationIT {
31+
32+
private String tableName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
tableName =
54+
"QUERY_PAGINATION_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
System.setOut(out);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
// Clean up
63+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
64+
System.setOut(null);
65+
}
66+
67+
@Test
68+
public void testQueryPagination() {
69+
String query =
70+
"SELECT name, SUM(number) as total_people"
71+
+ " FROM `bigquery-public-data.usa_names.usa_1910_2013`"
72+
+ " GROUP BY name"
73+
+ " ORDER BY total_people DESC"
74+
+ " LIMIT 100";
75+
QueryPagination.queryPagination(BIGQUERY_DATASET_NAME, tableName, query);
76+
assertThat(bout.toString()).contains("Query pagination performed successfully.");
77+
}
78+
}

0 commit comments

Comments
 (0)
Failed to load comments.