无法从Eclipse中的本地App Engine实例连接到BigQuery

时间:2017-05-15 20:09:28

标签: java google-app-engine google-bigquery

我是Google App Engine的新手,我正在尝试浏览一些教程,看看这对我的组织有什么用处。我们正在考虑将一些数据放入BigQuery并将一些Web应用程序转换为需要访问BigQuery数据的App Engine。

我使用的是java-docs-samples-master代码,特别是bigquery / cloud-client / src / main / java / com / example / bigquery / 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]
  }
}

2 个答案:

答案 0 :(得分:1)

根据您的错误代码的外观,可能是由于您的项目ID未设置:&#34; no_app_id&#34;。以下是如何设置应用引擎的项目ID:https://developers.google.com/eclipse/docs/appengine_appid_version

答案 1 :(得分:0)

不知道我是否迟到,但是在使用Firestore时遇到了此类错误,这是由于未在App Engine运行配置的“云平台”标签上设置任何项目。当我登录一个帐户并选择一个项目ID时,此错误消失了。