简体   繁体   English

Bigquery:如何在Bigquery JAVA API中访问另一个项目?

[英]Bigquery: how to access another project in Bigquery JAVA API?

Below is a working code sample which we are using to get a bigquery client. 下面是一个工作代码示例,我们正在使用该示例来获取bigquery客户端。 We want to tweak it so that we can do queries across different projects. 我们想对其进行调整,以便我们可以在不同项目中进行查询。 For example, running queries on project1 but save the result to a table in project2. 例如,在project1上运行查询,但将结果保存到project2中的表中。 What I did was to get the authentication for project2 but offer a full path reference to tables in project1 in queries, but it didn't work with Job failed while writing to Bigquery. 我所做的是获得对project2的身份验证,但在查询中提供了对project1中表的完整路径引用,但是在写入Bigquery时Job失败时,它不起作用。 accessDenied: Access Denied .(job_id: job_queryToTable_c43f8d51-01a5-492c-b648-e15171bb65db_266866506361660). accessDenied:访问被拒绝 。(job_id:job_queryToTable_c43f8d51-01a5-492c-b648-e15171bb65db_266866506361660)。 What should I do? 我该怎么办?

private void getBigQuery() {
    if (mBigquery == null) {
        HttpTransport transport = new NetHttpTransport();
        List<String> lScope = new ArrayList<String>();
        lScope.add(ConfigConstants.SCOPE);
        GoogleCredential credential;
        try {
            credential = new GoogleCredential.Builder()
                    .setTransport(transport)
                    .setJsonFactory(ConfigConstants.JSON_FACTORY)
                    .setServiceAccountId(ConfigConstants.SERVICE_CLIENT_ID)
                    .setServiceAccountScopes(lScope)                
                    .setServiceAccountPrivateKeyFromP12File(new File(ConfigConstants.KEY_FILE)).build();
            mBigquery = new Bigquery.Builder(transport,
                    ConfigConstants.JSON_FACTORY, credential)
                    .setApplicationName("BigQuery-Service-Accounts/0.1")
                    .setHttpRequestInitializer(credential).build();
        } catch (GeneralSecurityException e) {
            //TODO e.printStackTrace();
        } catch (IOException e) {
            //TODO e.printStackTrace();
        }

    }
}

the invalid grant came up when I tried to get table info from a table in another project 当我尝试从另一个项目的表中获取表信息时,出现了无效拨款

Table table = mBigquery.tables().get(project2,dataset,tableId).execute();//mBigquery is with credential for project1 and get table is for project2

Figured it out: Go to Google Developer Console --> permission of project1 -->add member --> add the service account of project2 using the account's client email id. 弄清楚了:转到Google Developer Console-> project1的权限-> add成员->使用该帐户的客户电子邮件ID添加project2的服务帐户。 Then it works 然后就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM