|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.cloud.bigquery.samples; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +import com.google.api.client.auth.oauth2.Credential; |
| 20 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 21 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 22 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 23 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl; |
| 24 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 25 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 26 | +import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; |
| 27 | +import com.google.api.client.http.HttpTransport; |
| 28 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 29 | +import com.google.api.client.json.JsonFactory; |
| 30 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 31 | +import com.google.api.client.util.store.DataStoreFactory; |
| 32 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 33 | +import com.google.api.services.bigquery.Bigquery.Datasets; |
| 34 | +import com.google.api.services.bigquery.Bigquery.Jobs.Insert; |
| 35 | +import com.google.api.services.bigquery.Bigquery; |
| 36 | +import com.google.api.services.bigquery.BigqueryScopes; |
| 37 | +import com.google.api.services.bigquery.model.DatasetList; |
| 38 | +import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
| 39 | +import com.google.api.services.bigquery.model.Job; |
| 40 | +import com.google.api.services.bigquery.model.JobConfiguration; |
| 41 | +import com.google.api.services.bigquery.model.JobConfigurationQuery; |
| 42 | +import com.google.api.services.bigquery.model.JobReference; |
| 43 | +import com.google.api.services.bigquery.model.QueryRequest; |
| 44 | +import com.google.api.services.bigquery.model.QueryResponse; |
| 45 | +import com.google.api.services.bigquery.model.TableCell; |
| 46 | +import com.google.api.services.bigquery.model.TableRow; |
| 47 | + |
| 48 | +import java.io.BufferedReader; |
| 49 | +import java.io.FileInputStream; |
| 50 | +import java.io.IOException; |
| 51 | +import java.io.InputStream; |
| 52 | +import java.io.InputStreamReader; |
| 53 | +import java.io.Reader; |
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.List; |
| 56 | +import java.util.Scanner; |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * Example of authorizing with Bigquery and reading from a public dataset. |
| 61 | + * |
| 62 | + * Specifically, this queries the shakespeare dataset to fetch the 10 of Shakespeare's works with |
| 63 | + * the greatest number of distinct words. |
| 64 | + */ |
| 65 | +public class GettingStarted { |
| 66 | + // [START build_service] |
| 67 | + /** |
| 68 | + * Creates an authorized Bigquery client service using Application Default Credentials. |
| 69 | + * |
| 70 | + * @return an authorized Bigquery client |
| 71 | + * @throws IOException if there's an error getting the default credentials. |
| 72 | + */ |
| 73 | + public static Bigquery createAuthorizedClient() throws IOException { |
| 74 | + // Create the credential |
| 75 | + HttpTransport transport = new NetHttpTransport(); |
| 76 | + JsonFactory jsonFactory = new JacksonFactory(); |
| 77 | + GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); |
| 78 | + |
| 79 | + // Depending on the environment that provides the default credentials (e.g. Compute Engine, App |
| 80 | + // Engine), the credentials may require us to specify the scopes we need explicitly. |
| 81 | + // Check for this case, and inject the Bigquery scope if required. |
| 82 | + if (credential.createScopedRequired()) { |
| 83 | + credential = credential.createScoped(BigqueryScopes.all()); |
| 84 | + } |
| 85 | + |
| 86 | + return new Bigquery.Builder(transport, jsonFactory, credential) |
| 87 | + .setApplicationName("Bigquery Samples").build(); |
| 88 | + } |
| 89 | + // [END build_service] |
| 90 | + |
| 91 | + // [START run_query] |
| 92 | + /** |
| 93 | + * Executes the given query synchronously. |
| 94 | + * |
| 95 | + * @param querySql the query to execute. |
| 96 | + * @param bigquery the Bigquery service object. |
| 97 | + * @param projectId the id of the project under which to run the query. |
| 98 | + * @return a list of the results of the query. |
| 99 | + * @throws IOException if there's an error communicating with the API. |
| 100 | + */ |
| 101 | + private static List<TableRow> executeQuery(String querySql, Bigquery bigquery, String projectId) |
| 102 | + throws IOException { |
| 103 | + QueryResponse query = bigquery.jobs().query( |
| 104 | + projectId, |
| 105 | + new QueryRequest().setQuery(querySql)) |
| 106 | + .execute(); |
| 107 | + |
| 108 | + // Execute it |
| 109 | + GetQueryResultsResponse queryResult = bigquery.jobs().getQueryResults( |
| 110 | + query.getJobReference().getProjectId(), |
| 111 | + query.getJobReference().getJobId()).execute(); |
| 112 | + |
| 113 | + return queryResult.getRows(); |
| 114 | + } |
| 115 | + // [END run_query] |
| 116 | + |
| 117 | + // [START print_results] |
| 118 | + /** |
| 119 | + * Prints the results to standard out. |
| 120 | + * |
| 121 | + * @param rows the rows to print. |
| 122 | + */ |
| 123 | + private static void printResults(List<TableRow> rows) { |
| 124 | + System.out.print("\nQuery Results:\n------------\n"); |
| 125 | + for (TableRow row : rows) { |
| 126 | + for (TableCell field : row.getF()) { |
| 127 | + System.out.printf("%-50s", field.getV()); |
| 128 | + } |
| 129 | + System.out.println(); |
| 130 | + } |
| 131 | + } |
| 132 | + // [END print_results] |
| 133 | + |
| 134 | + /** |
| 135 | + * Exercises the methods defined in this class. |
| 136 | + * |
| 137 | + * In particular, it creates an authorized Bigquery service object using Application Default |
| 138 | + * Credentials, then executes a query against the public Shakespeare dataset and prints out the |
| 139 | + * results. |
| 140 | + * |
| 141 | + * @param args the first argument, if it exists, should be the id of the project to run the test |
| 142 | + * under. If no arguments are given, it will prompt for it. |
| 143 | + * @throws IOException if there's an error communicating with the API. |
| 144 | + */ |
| 145 | + public static void main(String[] args) throws IOException { |
| 146 | + Scanner sc; |
| 147 | + if(args.length == 0) { |
| 148 | + // Prompt the user to enter the id of the project to run the queries under |
| 149 | + System.out.print("Enter the project ID: "); |
| 150 | + sc = new Scanner(System.in); |
| 151 | + } else { |
| 152 | + sc = new Scanner(args[0]); |
| 153 | + } |
| 154 | + String projectId = sc.nextLine(); |
| 155 | + |
| 156 | + // Create a new Bigquery client authorized via OAuth 2.0 protocol |
| 157 | + // dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); |
| 158 | + Bigquery bigquery = createAuthorizedClient(); |
| 159 | + |
| 160 | + List<TableRow> rows = executeQuery("SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words " |
| 161 | + + "FROM [publicdata:samples.shakespeare]", bigquery, projectId); |
| 162 | + |
| 163 | + printResults(rows); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments