Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQuery listTables(DatasetId) does not use project overrided in DatasetId #1123

Closed
tswast opened this issue Jul 21, 2016 · 5 comments · Fixed by #1217
Closed

BigQuery listTables(DatasetId) does not use project overrided in DatasetId #1123

tswast opened this issue Jul 21, 2016 · 5 comments · Fixed by #1217
Assignees
Labels
api: bigquery Issues related to the BigQuery API.

Comments

@tswast
Copy link
Contributor

tswast commented Jul 21, 2016

My Google Application Default Credentials are set to the swast-scratch project, but I wish to list the tables in the publicdata project's samples dataset. This is a public dataset.

$ bq ls publicdata:samples
      tableId       Type
 ----------------- -------
  github_nested     TABLE
  github_timeline   TABLE
  gsod              TABLE
  natality          TABLE
  shakespeare       TABLE
  trigrams          TABLE
  wikipedia         TABLE

Example code that displays this issue:

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.Table;
import java.io.PrintWriter;
import java.util.Iterator;

public class TableLister {
  public static void listTables(PrintWriter out) {
    BigQuery bigquery = BigQueryOptions.defaultInstance().service();
    Iterator<Table> tables = bigquery.listTables(DatasetId.of("publicdata", "samples")).iterateAll();
    while (tables.hasNext()) {
      out.println(tables.next());
    }
  }

  public static void main(String[] args) {
    listTables(new PrintWriter(System.out));
  }
}

It fails with the error: "message" : "Not found: Dataset swast-scratch:samples", so it appears that it is not using the project that I set in the DatasetId.

http://googlecloudplatform.github.io/gcloud-java/0.2.5/apidocs/com/google/cloud/bigquery/DatasetId.html#of-java.lang.String-java.lang.String-

@tswast tswast added the api: bigquery Issues related to the BigQuery API. label Jul 21, 2016
@mziccard
Copy link
Contributor

Hi Tim!

Actually this behavior is intended. All service operations (create/delete/get/list) are tied to the project id you provided to BigQueryOptions. The reason for which DatasetId and TableId also expose factory methods that take a projectId is that jobs (for instance a copy job) can be created in your bigqueryOptions.projectId() project and operate on tables and datasets of a different project.

Do you have a specific use case for creating BigQueryOptions with a project id and list tables that reside in a dataset of a different project?

@tswast
Copy link
Contributor Author

tswast commented Jul 21, 2016

Well for public datasets I don't have permission to access the BigQuery API
through them, but I do want to access those tables. It seems weird to say
that to read from a public dataset I need to connect to that project when I
create the service object.

@tswast
Copy link
Contributor Author

tswast commented Jul 22, 2016

So, it looks like I can work around this issue using BigQueryOptions, but it does feel wrong.

BigQuery bigquery = BigQueryOptions.builder().projectId("publicdata").build().service();
Iterator<Table> tables = bigquery.listTables(DatasetId.of("samples")).iterateAll();
while (tables.hasNext()) {
  out.println(tables.next());
}

I'm not a member of the publicdata project, so it feels strange to connect to it like this.

@tswast
Copy link
Contributor Author

tswast commented Jul 22, 2016

At the very least, we should raise an exception if the project ID is populated in the DatasetId.

It should not be silently ignored.

@mziccard
Copy link
Contributor

mziccard commented Aug 8, 2016

I gave some thoughts on this and I think you are right. I'll try to wrap up a PR, I'll CC you so you can give it a try as soon as it's ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API.
Projects
None yet
2 participants