|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + * not use this file except in compliance with the License. You may obtain a |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.samples; |
| 18 | + |
| 19 | +import com.google.api.client.util.Data; |
| 20 | +import com.google.api.services.bigquery.Bigquery; |
| 21 | +import com.google.api.services.bigquery.Bigquery.Datasets; |
| 22 | +import com.google.api.services.bigquery.model.DatasetList; |
| 23 | +import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
| 24 | +import com.google.api.services.bigquery.model.ProjectList; |
| 25 | +import com.google.api.services.bigquery.model.QueryRequest; |
| 26 | +import com.google.api.services.bigquery.model.QueryResponse; |
| 27 | +import com.google.api.services.bigquery.model.TableCell; |
| 28 | +import com.google.api.services.bigquery.model.TableRow; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.lang.Thread; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * Invokes the BigQuery basic APIs for the given project id specified. |
| 37 | + * |
| 38 | + * Samples used in this page: |
| 39 | + * |
| 40 | + * https://cloud.google.com/bigquery/bigquery-api-quickstart |
| 41 | + */ |
| 42 | +public class ListDatasetsProjects { |
| 43 | + /** |
| 44 | + * Run the sample. |
| 45 | + */ |
| 46 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 47 | + if (args.length != 1) { |
| 48 | + System.err.println("Usage: QuickStart <project-id>"); |
| 49 | + return; |
| 50 | + } |
| 51 | + String projectId = args[0]; |
| 52 | + |
| 53 | + Bigquery bigquery = BigqueryServiceFactory.getService(); |
| 54 | + String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count " |
| 55 | + + "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;"; |
| 56 | + |
| 57 | + System.out.println(); |
| 58 | + System.out.println("----- Running the asynchronous query and printing it to stdout."); |
| 59 | + runQueryRpcAndPrint(bigquery, projectId, query, System.out); |
| 60 | + |
| 61 | + System.out.println(); |
| 62 | + System.out.println("----- Listing all the Datasets in the projectId"); |
| 63 | + listDatasets(bigquery, projectId); |
| 64 | + |
| 65 | + System.out.println(); |
| 66 | + System.out.println("----- Listing all the Projects"); |
| 67 | + listProjects(bigquery); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Lists all Datasets in a project specified by the projectId. |
| 72 | + * |
| 73 | + * @param bigquery The BigQuery object. |
| 74 | + * @param projectId The projectId from which lists the existing Datasets. |
| 75 | + * @throws IOException if there's trouble with the network request. |
| 76 | + */ |
| 77 | + // [START listDatasets] |
| 78 | + public static void listDatasets(Bigquery bigquery, String projectId) throws IOException { |
| 79 | + Datasets.List datasetRequest = bigquery.datasets().list(projectId); |
| 80 | + DatasetList datasetList = datasetRequest.execute(); |
| 81 | + |
| 82 | + if (datasetList.getDatasets() != null) { |
| 83 | + List<DatasetList.Datasets> datasets = datasetList.getDatasets(); |
| 84 | + System.out.println("Dataset list:"); |
| 85 | + |
| 86 | + for (DatasetList.Datasets dataset : datasets) { |
| 87 | + System.out.format("%s\n", dataset.getDatasetReference().getDatasetId()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + // [END listDatasets] |
| 92 | + |
| 93 | + /** |
| 94 | + * Lists all Projects. |
| 95 | + * |
| 96 | + * @param bigquery The BigQuery object. |
| 97 | + * @throws IOException if there's trouble with the network request. |
| 98 | + */ |
| 99 | + // [START listProjects] |
| 100 | + public static void listProjects(Bigquery bigquery) throws IOException { |
| 101 | + Bigquery.Projects.List projectListRequest = bigquery.projects().list(); |
| 102 | + ProjectList projectList = projectListRequest.execute(); |
| 103 | + |
| 104 | + if (projectList.getProjects() != null) { |
| 105 | + List<ProjectList.Projects> projects = projectList.getProjects(); |
| 106 | + System.out.println("Project list:"); |
| 107 | + |
| 108 | + for (ProjectList.Projects project : projects) { |
| 109 | + System.out.format("%s\n", project.getFriendlyName()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + // [END listProjects] |
| 114 | + |
| 115 | + /** |
| 116 | + * Runs a synchronous BigQuery query and displays the result. |
| 117 | + * |
| 118 | + * @param bigquery An authorized BigQuery client |
| 119 | + * @param projectId The current project id |
| 120 | + * @param query A String containing a BigQuery SQL statement |
| 121 | + * @param out A PrintStream for output, normally System.out |
| 122 | + */ |
| 123 | + static void runQueryRpcAndPrint(Bigquery bigquery, String projectId, String query, |
| 124 | + PrintStream out) throws IOException, InterruptedException { |
| 125 | + QueryRequest queryRequest = new QueryRequest().setQuery(query); |
| 126 | + QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute(); |
| 127 | + if (queryResponse.getJobComplete()) { |
| 128 | + printRows(queryResponse.getRows(), out); |
| 129 | + if (null == queryResponse.getPageToken()) { |
| 130 | + return; |
| 131 | + } |
| 132 | + } |
| 133 | + // This loop polls until results are present, then loops over result pages. |
| 134 | + String pageToken = null; |
| 135 | + while (true) { |
| 136 | + GetQueryResultsResponse queryResults = bigquery.jobs() |
| 137 | + .getQueryResults(projectId, queryResponse.getJobReference().getJobId()) |
| 138 | + .setPageToken(pageToken).execute(); |
| 139 | + if (queryResults.getJobComplete()) { |
| 140 | + printRows(queryResults.getRows(), out); |
| 141 | + pageToken = queryResults.getPageToken(); |
| 142 | + if (null == pageToken) { |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | + Thread.sleep(500); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Print the given rows. |
| 152 | + * |
| 153 | + * @param rows the rows to print. |
| 154 | + * @param out the place to print them. |
| 155 | + */ |
| 156 | + private static void printRows(java.util.List<TableRow> rows, PrintStream out) { |
| 157 | + if (rows != null) { |
| 158 | + for (TableRow row : rows) { |
| 159 | + for (TableCell cell : row.getF()) { |
| 160 | + // Data.isNull() is the recommended way to check for the 'null object' in TableCell. |
| 161 | + out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString()); |
| 162 | + } |
| 163 | + out.println(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments