简体   繁体   中英

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. 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. 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. accessDenied: Access Denied .(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. Then it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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