我已经尝试使用BigQuery API快速入门页面中提供的完整源代码从我的java eclipse运行Google Bigquery查询:
https://developers.google.com/bigquery/bigquery-api-quickstart#authorizing
代码已更改为适合我的项目,所有客户端库都已安装,运行后我不断收到此错误消息:
线程“main”中的异常java.lang.NoSuchMethodError:com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient。(Lcom / google / api / client / http / HttpTransport; Lcom / google / api /客户机/ JSON / JsonFactory; Ljava /郎/字符串; Ljava /郎/字符串; LCOM /谷歌/ API /客户端/ HTTP / HttpRequestInitializer; Z)V 在com.google.api.services.bigquery.Bigquery。(Bigquery.java:108) 在com.google.cloud.helix.samples.BigQuery.main(BigQuery.java:61)
我正在使用的代码是:
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();
}
}
}
}
有人可以说出问题是什么,我该如何解决?
答案 0 :(得分:1)
我建议您使用链接
中提到的相同版本的罐子 google-api-client 1.10.3-beta
google-api-services-bigquery v2-rev19-1.7.2-beta
google-oauth-client 1.10.1-beta
像1.17这样的新版本已经废除了构造函数:
AbstractGoogleJsonClient(HttpTransport,JsonFactory,String,String,HttpRequestInitializer)
他们现在使用不同的实现:AbstractGoogleJsonClient