在这个例子中我遇到了同样的问题。
https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries
我试图低于命令但发生 401 错误
export GOOGLE_APPLICATION_CREDENTIALS=/Users/mattheu/credentialFileName.json
我使用以下解决了凭据问题
String jsonPath = "/Users/mattheu/credentialFileName.json";
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath));
package com;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigquery.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
public class App {
public static void main(String[] args) throws InterruptedException, IOException {
String jsonPath = "/Users/mattheu/credentialFileName.json";
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath));
BigQuery bigquery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.build().getService();
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(
"SELECT "
+ "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
+ "view_count "
+ "FROM `bigquery-public-data.stackoverflow.posts_questions` "
+ "WHERE tags like '%google-bigquery%' "
+ "ORDER BY favorite_count DESC LIMIT 10")
// 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());
}
QueryResponse response = bigquery.getQueryResults(jobId);
TableResult result = queryJob.getQueryResults();
// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
String url = row.get("url").getStringValue();
long viewCount = row.get("view_count").getLongValue();
System.out.printf("url: %s views: %d%n", url, viewCount);
}
}
}
result:
url:什么是 Google 的 Dremel?它与 Mapreduce 有何不同?视图:27736
url:无法从本地 App Engine 开发服务器访问 BigQuery视图:4732
url:如何在 BigQuery 中使用 TABLE_QUERY() 函数?视图:9674
url:从非分区表迁移到分区表视图:3549
url:如何取消删除 BigQuery 表?意见:3332
网址:Google App Engine:在数据存储上使用 Big Query?浏览次数:6928
网址:Google BigQuery 中的随机抽样浏览次数:11635
网址:如何在应用程序引擎和 python 视图上使用 Bigquery 流式插入:4279
url:从 BigQuery 表视图中删除重复行:8552
url:如何提高 BigQuery 中 GeoIP 查询的性能?浏览量:3213