找到我的问题的答案:对于那些有同样问题的人。
解答:使用HTTP servlet时,我需要在WEB-INF / lib目录中安装jar。否则我可以将它们保存在java构建路径(库)下。因此,在eclispe中,右键单击lib,然后添加Google API和select BigQuery。
我正在使用大查询测试谷歌应用引擎。
当我将其作为应用程序运行时,我能够在eclipse中运行大查询,但是当我将其作为HttpServlet运行时,我不断收到以下错误!
java.lang.NoClassDefFoundError:com / google / api / client / json / JsonFactory
以下是我正在使用的确切代码。
package com.hw3.test;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.jackson2.JacksonFactory;
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.IOException;
import javax.servlet.http.*;
import java.util.List;
import java.util.Scanner;
@SuppressWarnings("serial")
public class HelloWord3Servlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Bigquery bigquery = createAuthorizedClient(); //If i comment this out i will get the text below, else i get the error from the title.
resp.setContentType("text/plain");
resp.getWriter().println("\nQuery Results:\n------------\n");
}
private static List<TableRow> executeQuery(String querySql, Bigquery bigquery, String projectId)
throws IOException {
QueryResponse query = bigquery.jobs().query(projectId, new QueryRequest().setQuery(querySql)).execute();
// Execute it
GetQueryResultsResponse queryResult = bigquery.jobs()
.getQueryResults(query.getJobReference().getProjectId(), query.getJobReference().getJobId()).execute();
return queryResult.getRows();
}
public static Bigquery createAuthorizedClient() throws IOException {
// Create the credential
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
// Depending on the environment that provides the default credentials
// (e.g. Compute Engine, App
// Engine), the credentials may require us to specify the scopes we need
// explicitly.
// Check for this case, and inject the Bigquery scope if required.
if (credential.createScopedRequired()) {
credential = credential.createScoped(BigqueryScopes.all());
}
return new Bigquery.Builder(transport, jsonFactory, credential).setApplicationName("Bigquery Samples").build();
}
public static void main(String[] args) throws IOException {
Scanner sc;
if (args.length == 0) {
// Prompt the user to enter the id of the project to run the queries
// under
System.out.print("Enter the project ID: ");
sc = new Scanner(System.in);
} else {
sc = new Scanner(args[0]);
}
String projectId = sc.nextLine();
// Create a new Bigquery client authorized via Application Default
// Credentials.
Bigquery bigquery = createAuthorizedClient();
List<TableRow> rows = executeQuery(
"SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words " + "FROM [publicdata:samples.shakespeare]",
bigquery, projectId);
printResults(rows);
}
private static void printResults(List<TableRow> rows) {
System.out.print("\nQuery Results:\n------------\n");
for (TableRow row : rows) {
for (TableCell field : row.getF()) {
System.out.printf("%-50s", field.getV());
}
System.out.println();
}
}
}
我直接从谷歌网站获得此代码,虽然我稍微修改了它,以便我可以测试app引擎。但是,在使用应用引擎时它无效。
非常感谢任何帮助!
答案 0 :(得分:1)
当您作为HttpServlet运行时,听起来好像没有正确配置依赖项。你如何告诉你的应用程序使用哪些依赖项?你想加载什么版本?该版本是否可在Google App Engine中使用?
请注意,您需要的jackson库的特定版本会根据您运行的环境而更改。有关各种环境中所需的依赖项列表,请参阅https://developers.google.com/api-client-library/java/google-http-java-client/setup。
答案 1 :(得分:0)
解答:使用HTTP servlet时,我需要在WEB-INF / lib目录中安装jar。否则我可以将它们保存在java构建路径(库)下。因此,在eclispe中,右键单击lib,然后添加Google API并选择BigQuery。