user3794161
user3794161

Reputation: 21

BigQuery API Quickstart Complete Source Code (Java) error

I have beeen trying to run a Google Bigquery Query from my java eclipse, using the Complete Source Code that is presented in the BigQuery API Quickstart page:

https://developers.google.com/bigquery/bigquery-api-quickstart#authorizing

The code was changed to fit my project, all the client libraries are installed, and I keep getting this error message after running :

Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.(Lcom/google/api/client/http/HttpTransport;Lcom/google/api/client/json/JsonFactory;Ljava/lang/String;Ljava/lang/String;Lcom/google/api/client/http/HttpRequestInitializer;Z)V at com.google.api.services.bigquery.Bigquery.(Bigquery.java:108) at com.google.cloud.helix.samples.BigQuery.main(BigQuery.java:61)

the code Im using is this:

  package com.google.cloud.helix.samples;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.Data;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import com.google.api.services.bigquery.model.QueryRequest;
import com.google.api.services.bigquery.model.QueryResponse;
import com.google.api.services.bigquery.model.TableCell;
import com.google.api.services.bigquery.model.TableRow;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * BigQuery sample code. Runs queries using client_secrets.json authentication.
 */
public class BigQuery {

  // Enter your Google Developer Project number or string id.
  private static final String PROJECT_ID = "61613421242";

  // Use a Google APIs Client standard client_secrets.json OAuth 2.0 parameters file.
  private static final String CLIENTSECRETS_LOCATION = "client_secrets.json";

  // Objects for handling HTTP transport and JSON formatting of API calls.
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();

  public static void main(String[] args) throws IOException 
  {

    String filePath = System.getProperty("user.dir")+"\\src\\"+CLIENTSECRETS_LOCATION;

    InputStream inputStream = new FileInputStream(filePath);
    InputStreamReader stReader= new InputStreamReader(inputStream);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(new JacksonFactory(),stReader);

    Credential credential = getCredentials(clientSecrets, new Scanner(System.in));

    Bigquery bigquery = new Bigquery(HTTP_TRANSPORT, JSON_FACTORY, credential);
    String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count "
        + "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;";
    runQueryRpcAndPrint(bigquery, PROJECT_ID, query, System.out);
  }

  static Credential getCredentials(GoogleClientSecrets clientSecrets, Scanner scanner)
      throws IOException {
    String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(
        clientSecrets, clientSecrets.getInstalled().getRedirectUris().get(0),
        Collections.singleton(BigqueryScopes.BIGQUERY)).build();
    System.out.println(
        "Paste this URL into a web browser to authorize BigQuery Access:\n" + authorizeUrl);
    System.out.println("... and paste the code you received here: ");
    String authorizationCode = scanner.nextLine();

    // Exchange the auth code for an access token.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Arrays.asList(BigqueryScopes.BIGQUERY))
        .build();
    GoogleTokenResponse response = flow.newTokenRequest(authorizationCode)
        .setRedirectUri(clientSecrets.getInstalled().getRedirectUris().get(0)).execute();
    return flow.createAndStoreCredential(response, null);
  }



  /**
   * Runs a synchronous BigQuery query and displays the result.
   *
   * @param bigquery An authorized BigQuery client
   * @param projectId The current project id
   * @param query A String containing a BigQuery SQL statement
   * @param out A PrintStream for output, normally System.out
   */
  static void runQueryRpcAndPrint(
      Bigquery bigquery, String projectId, String query, PrintStream out) throws IOException {
    QueryRequest queryRequest = new QueryRequest().setQuery(query);
    QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute();
    if (queryResponse.getJobComplete()) {
      printRows(queryResponse.getRows(), out);
      if (null == queryResponse.getPageToken()) {
        return;
      }
    }
    // This loop polls until results are present, then loops over result pages.
    String pageToken = null;
    while (true) {
      GetQueryResultsResponse queryResults = bigquery.jobs()
          .getQueryResults(projectId, queryResponse.getJobReference().getJobId())
          .setPageToken(pageToken).execute();
      if (queryResults.getJobComplete()) {
        printRows(queryResults.getRows(), out);
        pageToken = queryResults.getPageToken();
        if (null == pageToken) {
          return;
        }
      }
    }
  }


  private static void printRows(List<TableRow> rows, PrintStream out) {
    if (rows != null) {
      for (TableRow row : rows) {
        for (TableCell cell : row.getF()) {
          // Data.isNull() is the recommended way to check for the 'null object' in TableCell.
          out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString());
        }
        out.println();
      }
    }
  }

}

Can anybody tell what is the problem and how can I fix it?

Upvotes: 2

Views: 1922

Answers (1)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

I suggest you have the exact same versions of the jars mentioned in the link

google-api-client 1.10.3-beta
google-api-services-bigquery v2-rev19-1.7.2-beta
google-oauth-client 1.10.1-beta

The new versions like 1.17 have done away with the constructor:

AbstractGoogleJsonClient(HttpTransport,JsonFactory,String,String,HttpRequestInitializer)

They use a different implementation now: AbstractGoogleJsonClient

Upvotes: 1

Related Questions