2016-05-09 4 views
0

私の質問に対する答えを見つけました。google bigqueryとgoogleのアプリエンジンが動作しないようにする

回答:HTTPサーブレットで作業する場合、私はWEB-INF/libディレクトリ内にjarファイルを置く必要がありました。それ以外の場合は、それらをjavaビルドパス(ライブラリ)の下に置くことができます。したがって、eclispeでは、libを右クリックし、次にGoogle APIを追加し、BigQueryを選択します。

私は大きなクエリでGoogleのアプリケーションエンジンをテストしています。

私はアプリとしてそれを実行すると、私は大きなクエリを実行することができますが、私はHttpServletとして実行すると、私は次のエラーを取得し続ける!

ます。java.lang.NoClassDefFoundError:COM /グーグル/ API /クライアント/ 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(); 
     } 
    } 
} 

私はアプリエンジンをテストできるように、私は少しそれを変更しなかったが、私はGoogleのWebサイトから直接このコードを得ました。ただし、アプリエンジンを使用している場合は機能しません。

すべてのヘルプは大歓迎です!

+0

ログにタイムアウトエラーが表示されますか?フロントエンドインスタンスを30秒以上使用することはできません。 –

+0

すぐに失敗します。注意:アプリケーションとして実行するとうまく動作しますが、httpサーブレットとして実行しようとすると失敗します。 – ADL

答えて

0

回答:HTTPサーブレットで作業する場合、私はWEB-INF/libディレクトリ内にjarファイルを置く必要がありました。それ以外の場合は、それらをjavaビルドパス(ライブラリ)の下に置くことができます。したがって、eclispeでは、libを右クリックし、次にGoogle APIを追加し、BigQueryを選択します。

1

HttpServletとして実行しているときに、依存関係が正しく設定されていないようです。どのような依存関係を使用するようにアプリに指示しますか?どのバージョンをロードしようとしていますか?そのバージョンはGoogle App Engineで利用できますか?

あなたが必要とするジャクソンライブラリの特定のバージョンは、実行している環境によって異なります。さまざまな環境で必要となる依存関係のリストについては、https://developers.google.com/api-client-library/java/google-http-java-client/setupを参照してください。

+0

こんにちはマイケル、応答のおかげで、私は同じ人が私の質問とあなたの答えに投票したと思う。 Jarrod Robersonという人がいます。あなたが言及したようにJava用のHTTPクライアントライブラリとバージョンをチェックアウトします。お知らせいたします。ありがとう! – ADL

+0

ねえ、私はそれを考え出した。 HTTPサーブレットで作業するときには、WEB-INF/libディレクトリ内にjarファイルを置く必要がありました。それ以外の場合は、それらをjavaビルドパス(ライブラリ)の下に置くことができます。ご協力いただきありがとうございます。 – ADL

関連する問題