3

I queried a federated table with data in Google spreadsheet. Following recommendations in issue 720 https://code.google.com/p/google-bigquery/issues/detail?id=720 I've created following code:

    Set<String> scopes = new HashSet<>();
    scopes.add(BigqueryScopes.BIGQUERY);
    scopes.add("https://www.googleapis.com/auth/drive");
    scopes.add("https://www.googleapis.com/auth/spreadsheets");
    final HttpTransport transport= new NetHttpTransport();
    final JsonFactory jsonFactory= new JacksonFactory();
    GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(transport).setJsonFactory(jsonFactory)
                .setServiceAccountId(GC_CREDENTIALS_ACCOUNT_EMAIL)
                .setServiceAccountScopes(scopes)
                .setServiceAccountPrivateKey(getPrivateKey())       
                .build();
    String omgsql = "SELECT * FROM [<myproject>:<mydataset>.failures] LIMIT 1000";
    JobReference jobIdomg = startQuery(bigquery, "<myproject>", omgsql);

    // Poll for Query Results, return result output
    Job completedJobomg = checkQueryResults(bigquery, "<myproject>", jobIdomg);
    GetQueryResultsResponse queryResultomg = bigquery.jobs()
        .getQueryResults(
            "<myproject>", completedJobomg
                .getJobReference()
                .getJobId()
        ).execute();
    List<TableRow> rowsomg = queryResultomg.getRows();

Without https://www.googleapis.com/auth/drive scope job fails immediately after inserting, with it - fails on completion.

Inserting Query Job: SELECT * FROM [<myproject>:<mydataset>.failures] LIMIT 1000
Job ID of Query Job is: job_S3-fY5jrb4P3UhVgNGeRkDYQofg
Job status (194ms) job_S3-fY5jrb4P3UhVgNGeRkDYQofg: RUNNING
Job status (1493ms) job_S3-fY5jrb4P3UhVgNGeRkDYQofg: RUNNING
Job status (2686ms) job_S3-fY5jrb4P3UhVgNGeRkDYQofg: RUNNING
...
Job status (29881ms) job_S3-fY5jrb4P3UhVgNGeRkDYQofg: DONE
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "/gdrive/id/1T4qNgi9vFJF4blK4jddYf8XlfT6uDiqNpTExWf1NMyY",
    "locationType" : "other",
    "message" : "Encountered an error while globbing file pattern.",
    "reason" : "invalid"
  } ],
  "message" : "Encountered an error while globbing file pattern."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)

So the question here - what else am I missing? Or is it just a bigquery bug?

Dan Mandel
  • 637
  • 1
  • 8
  • 26
Alexey
  • 2,388
  • 1
  • 16
  • 32

1 Answers1

8

Ok, after a day of experiments - the account you are using to obtain Google Credential should have access to the file(s) on which external table is created. Hope this will help someone.

Alexey
  • 2,388
  • 1
  • 16
  • 32
  • Hey Alexey. Could you please tell how to add that access? I'm trying to query big query tables from python. Some tables I was able to query seamlessly. However, for a couple of table I'm getting the same error that you've got. Thanks for your help. – Tukki Oct 10 '21 at 07:54
  • 1
    For those searching in the future: the GCS buckets and/or objects need the IAM roles `Storage Legacy Bucket Reader` and/or `Storage Object Viewer`. So simply grant these roles to the `Principal` -- the service account email. – Joe - GMapsBook.com May 09 '22 at 16:27