Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用服务帐户访问具有外部数据源的BigQuery表_Java_Google Cloud Platform_Google Bigquery - Fatal编程技术网

Java 使用服务帐户访问具有外部数据源的BigQuery表

Java 使用服务帐户访问具有外部数据源的BigQuery表,java,google-cloud-platform,google-bigquery,Java,Google Cloud Platform,Google Bigquery,如何使用服务帐户作为凭证提供者查询基于外部数据源(谷歌电子表格)的BigQuery表 我对BigQuery java客户端进行如下初始化: GoogleCredentials cred = ServiceAccountCredentials .fromStream(credentialsFile.getInputStream()); BigQuery bq = BigQueryOptions.newBuilder() .setCredentials(cred).b

如何使用服务帐户作为凭证提供者查询基于外部数据源(谷歌电子表格)的BigQuery表

我对BigQuery java客户端进行如下初始化:

GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream());
BigQuery bq = BigQueryOptions.newBuilder()
        .setCredentials(cred).build()
        .getService();
GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream())
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/drive"));
但是,在尝试查询表时,我得到了身份验证错误:

Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.

添加驱动器时,如下所示:

GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream());
BigQuery bq = BigQueryOptions.newBuilder()
        .setCredentials(cred).build()
        .getService();
GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream())
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/drive"));

将返回一个
权限不足
错误。

我也遇到了同样的问题,我使用以下步骤修复了该问题

1。在项目中启用Google Drive API

登录到您的谷歌云控制台。从顶部下拉列表中选择您的项目。从左侧导航中,选择API和服务,然后选择Dashboard。在页面顶部,单击启用API和服务。使用搜索栏搜索Google Drive API,选择它,然后单击启用

2。将服务帐户客户端ID添加到Google工作表中

在谷歌云控制台中,从左侧边栏中选择IAM&admin,然后选择服务帐户。复制服务帐户ID列中的值。如果您还没有服务帐户,请按照上面的说明连接BigQuery帐户

打开你的谷歌工作表,点击共享按钮。单击底部的“高级”,然后在“邀请人员”文本输入中,输入先前复制的服务帐户ID值

3。添加“云平台”范围

GoogleCredentials cred = ServiceAccountCredentials
            .fromStream(credentialsFile.getInputStream())
            .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                    "https://www.googleapis.com/auth/cloud-platform"));
代码:

        File credentialsFile = new File("credentials.json");
        GoogleCredentials cred = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsFile))
                .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                        "https://www.googleapis.com/auth/cloud-platform"));
        BigQuery bigquery = BigQueryOptions.newBuilder()
                .setCredentials(cred).build()
                .getService();

        QueryJobConfiguration queryConfig =
                QueryJobConfiguration.newBuilder(
                        "SELECT * FROM `my_project.dataset.table1`")
                        // 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();

        QueryResponse response = bigquery.getQueryResults(jobId);

        TableResult result = queryJob.getQueryResults();

        // Print all pages of the results.
        for (FieldValueList row : result.iterateAll()) {
            String date = row.get("Date").getStringValue();
            System.out.printf("Date: %s%n", date);
        }
参考资料:

        File credentialsFile = new File("credentials.json");
        GoogleCredentials cred = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsFile))
                .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                        "https://www.googleapis.com/auth/cloud-platform"));
        BigQuery bigquery = BigQueryOptions.newBuilder()
                .setCredentials(cred).build()
                .getService();

        QueryJobConfiguration queryConfig =
                QueryJobConfiguration.newBuilder(
                        "SELECT * FROM `my_project.dataset.table1`")
                        // 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();

        QueryResponse response = bigquery.getQueryResults(jobId);

        TableResult result = queryJob.getQueryResults();

        // Print all pages of the results.
        for (FieldValueList row : result.iterateAll()) {
            String date = row.get("Date").getStringValue();
            System.out.printf("Date: %s%n", date);
        }

我也面临同样的问题,我通过以下步骤解决了这个问题

1。在项目中启用Google Drive API

登录到您的谷歌云控制台。从顶部下拉列表中选择您的项目。从左侧导航中,选择API和服务,然后选择Dashboard。在页面顶部,单击启用API和服务。使用搜索栏搜索Google Drive API,选择它,然后单击启用

2。将服务帐户客户端ID添加到Google工作表中

在谷歌云控制台中,从左侧边栏中选择IAM&admin,然后选择服务帐户。复制服务帐户ID列中的值。如果您还没有服务帐户,请按照上面的说明连接BigQuery帐户

打开你的谷歌工作表,点击共享按钮。单击底部的“高级”,然后在“邀请人员”文本输入中,输入先前复制的服务帐户ID值

3。添加“云平台”范围

GoogleCredentials cred = ServiceAccountCredentials
            .fromStream(credentialsFile.getInputStream())
            .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                    "https://www.googleapis.com/auth/cloud-platform"));
代码:

        File credentialsFile = new File("credentials.json");
        GoogleCredentials cred = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsFile))
                .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                        "https://www.googleapis.com/auth/cloud-platform"));
        BigQuery bigquery = BigQueryOptions.newBuilder()
                .setCredentials(cred).build()
                .getService();

        QueryJobConfiguration queryConfig =
                QueryJobConfiguration.newBuilder(
                        "SELECT * FROM `my_project.dataset.table1`")
                        // 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();

        QueryResponse response = bigquery.getQueryResults(jobId);

        TableResult result = queryJob.getQueryResults();

        // Print all pages of the results.
        for (FieldValueList row : result.iterateAll()) {
            String date = row.get("Date").getStringValue();
            System.out.printf("Date: %s%n", date);
        }
参考资料:

        File credentialsFile = new File("credentials.json");
        GoogleCredentials cred = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsFile))
                .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                        "https://www.googleapis.com/auth/cloud-platform"));
        BigQuery bigquery = BigQueryOptions.newBuilder()
                .setCredentials(cred).build()
                .getService();

        QueryJobConfiguration queryConfig =
                QueryJobConfiguration.newBuilder(
                        "SELECT * FROM `my_project.dataset.table1`")
                        // 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();

        QueryResponse response = bigquery.getQueryResults(jobId);

        TableResult result = queryJob.getQueryResults();

        // Print all pages of the results.
        for (FieldValueList row : result.iterateAll()) {
            String date = row.get("Date").getStringValue();
            System.out.printf("Date: %s%n", date);
        }

服务帐户有权访问该表吗?我与服务帐户id
服务帐户共享了该表-id@my-project.iam.gserviceaccount.com
。检查创建的服务帐户在GCP上是否具有驱动器访问权限console@FelipeHoffa我该怎么做?这正是我无法理解的。服务帐户有权访问该表吗?我与服务帐户id
服务帐户共享了该表-id@my-project.iam.gserviceaccount.com
。检查创建的服务帐户在GCP上是否具有驱动器访问权限console@FelipeHoffa我该怎么做?这正是我无法理解的。我按照您的说明进行了操作,现在调用
queryJob.waitFor()
时出现
notfound:Job
错误。我调用BigQuery类似于@user10595796。我使用了相同的代码,它工作时没有任何问题。检查更新后的答案中的示例代码。当我在纯BigQuery表上运行这段代码时,它工作得很好。但是,在源格式为
的表上,行
queryJob.waitFor()
仍然会抛出
BigQueryException:Not found:Job
。我按照您的说明操作,现在调用
queryJob.waitFor()时出现
Not found:Job
错误。我调用BigQuery类似于@user10595796。我使用了相同的代码,它工作时没有任何问题。检查更新后的答案中的示例代码。当我在纯BigQuery表上运行这段代码时,它工作得很好。但是,在源格式为
的表中,行
queryJob.waitFor()
仍将抛出
BigQueryException:notfound:Job