gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:10:09 27 4
gpt4 key购买 nike

我是 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]
}
}

最佳答案

从您的错误代码来看,这可能是由于您的项目 ID 未设置:“no_app_id”。以下是如何设置应用程序引擎的项目 ID:https://developers.google.com/eclipse/docs/appengine_appid_version .

关于java - 无法从 Eclipse 中的本地 App Engine 实例连接到 BigQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com