2017-05-15 163 views
0

我是Google App Engine的新手,我試圖通過一些教程來看看這對我的組織是如何工作的。我們正在考慮將一些數據放入BigQuery中,並將一些Web應用程序轉換爲需要訪問BigQuery數據的App Engine。無法從Eclipse的本地App Engine實例連接到BigQuery

我使用java的文檔樣本主代碼,具體地大量查詢/雲客戶端/ SRC /主/ JAVA/COM /示例/大量查詢/ SimpleApp.java

我可以從命令運行該使用

mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleAppMain

我的代碼合併到App Engine的,這我在Eclipse中運行並創建了一個包裝線,所以我仍然可以在命令行中運行它。它在從命令行運行時起作用,但在Eclipse中從App Engine運行時出現錯誤。

有什麼我很想配置我的本地App Engine連接到Big Query嗎?

錯誤:

com.google.cloud.bigquery.BigQueryException: Invalid project ID 'no_app_id'. Project IDs must contain 6-63 lowercase letters, digits, or dashes. IDs must start with a letter and may not end with a dash. 
at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.translate(HttpBigQueryRpc.java:86) 
at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.create(HttpBigQueryRpc.java:170) 
at com.google.cloud.bigquery.BigQueryImpl$3.call(BigQueryImpl.java:208) 
... 
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 
{ "code" : 400, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Invalid project ID 'no_app_id'. Project IDs must contain 6-63 lowercase letters, digits, or dashes. IDs must start with a letter and may not end with a dash.", 
    "reason" : "invalid" 
    } ], 
    "message" : "Invalid project ID 'no_app_id'. Project IDs must contain 6-63 lowercase letters, digits, or dashes. IDs must start with a letter and may not end with a dash." 
} 

代碼:

package com.example.bigquery; 

import com.google.cloud.bigquery.BigQuery; 
import com.google.cloud.bigquery.BigQueryOptions; 
import com.google.cloud.bigquery.FieldValue; 
import com.google.cloud.bigquery.Job; 
import com.google.cloud.bigquery.JobId; 
import com.google.cloud.bigquery.JobInfo; 
import com.google.cloud.bigquery.QueryJobConfiguration; 
import com.google.cloud.bigquery.QueryResponse; 
import com.google.cloud.bigquery.QueryResult; 

import java.util.List; 
import java.util.UUID; 

public class SimpleApp { 
    public void runBQ() throws Exception { 
    // [START create_client] 
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); 
    // [END create_client] 
    // [START run_query] 
    QueryJobConfiguration queryConfig = 
     QueryJobConfiguration.newBuilder(
       "SELECT " 
        + "APPROX_TOP_COUNT(corpus, 10) as title, " 
        + "COUNT(*) as unique_words " 
        + "FROM `publicdata.samples.shakespeare`;") 
      // Use standard SQL syntax for queries. 
      // See: https://cloud.google.com/bigquery/sql-reference/ 
      .setUseLegacySql(false) 
      .build(); 

    // Create a job ID so that we can safely retry. 
    JobId jobId = JobId.of(UUID.randomUUID().toString()); 
    Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()); 

    // Wait for the query to complete. 
    queryJob = queryJob.waitFor(); 

    // Check for errors 
    if (queryJob == null) { 
     throw new RuntimeException("Job no longer exists"); 
    } else if (queryJob.getStatus().getError() != null) { 
     // You can also look at queryJob.getStatus().getExecutionErrors() for all 
     // errors, not just the latest one. 
     throw new RuntimeException(queryJob.getStatus().getError().toString()); 
    } 

    // Get the results. 
    QueryResponse response = bigquery.getQueryResults(jobId); 
    // [END run_query] 

    // [START print_results] 
    QueryResult result = response.getResult(); 

    // Print all pages of the results. 
    while (result != null) { 
     for (List<FieldValue> row : result.iterateAll()) { 
     List<FieldValue> titles = row.get(0).getRepeatedValue(); 
     System.out.println("titles:"); 

     for (FieldValue titleValue : titles) { 
      List<FieldValue> titleRecord = titleValue.getRecordValue(); 
      String title = titleRecord.get(0).getStringValue(); 
      long uniqueWords = titleRecord.get(1).getLongValue(); 
      System.out.printf("\t%s: %d\n", title, uniqueWords); 
     } 

     long uniqueWords = row.get(1).getLongValue(); 
     System.out.printf("total unique words: %d\n", uniqueWords); 
     } 

     result = result.getNextPage(); 
    } 
    // [END print_results] 
    } 
} 

回答

1

從你的錯誤代碼外觀上來看,它可能是由於未設置項目ID: 「no_app_id」。以下是如何設置應用程序引擎的項目ID:https://developers.google.com/eclipse/docs/appengine_appid_version

+0

我看着你提供的鏈接,我沒有谷歌菜單選項,當我右鍵點擊該項目。 僅供參考 - 我已安裝「Google Cloud Platform for Eclipse 4.5及更高版本」插件。從我可以看到的是有一個較舊的「Google Plugin for Eclipse」,但它想要卸載我安裝的其他插件。還有什麼我失蹤? – RyanD

+0

您可以看到它是否存在於文件中:「特定App Engine項目的項目標識和版本由appengine-web.xml文件定義,可在war/WEB-INF目錄中找到」? –

+0

我沒有在appengine-web.xml中列出他們中的任何一個。我查了一下格式並添加了標籤,它給了我一個警告,即「應該在部署時指定項目ID」 – RyanD

相關問題