dee.ronin
dee.ronin

Reputation: 1038

Google BigQuery - ListProjects only returns 1 record

I want to list all the projects that I have access but it only returns 1 record when I call the ListProjects method from BigQueryClient class.

I verify that I have access for at-least 2 projects by trying to run the code below:

BigQueryClient client = BigQueryClient.Create("project1");
var result = client.ExecuteQuery("SELECT col1 FROM `project1.dataset1.table1`", null) //this returns the data from project1
BigQueryClient client = BigQueryClient.Create("project2");
var result = client.ExecuteQuery("SELECT col1 FROM `project2.dataset2.table2`", null) //this returns the data from project2

Both code above returns the data. But when I try to get all the projects using the ListProjects method, it only returns 1 record:

BigQueryClient client = BigQueryClient.Create(string.Empty);

var projects = client.ListProjects(); //this method only returns 1 record

Is there a missing in my code so that I can see all the projects from google BigQuery?

Upvotes: 1

Views: 433

Answers (2)

dee.ronin
dee.ronin

Reputation: 1038

The string.Empty or irrelevant in BigQueryClient.Create doesn't matter. It needs to have a role on the GCP Project that you want to include in the service account.

In my case, I follow the steps below:

  1. Go to Google Cloud Platform -> IAM & Admin -> Manage Resources
  2. Select the project that you want to include
  3. Click Add Member button
  4. Enter the Service Account
  5. Select a Role. I choose BigQuery Metadata Viewer
  6. Click Save button.

There is a delayed about 2 minutes before it takes effect.

Upvotes: 1

Jin Thakur
Jin Thakur

Reputation: 2773

BigQueryClient client = BigQueryClient.Create(string.Empty);
BigQueryClient client = BigQueryClient.Create("irrelevant");
PagedEnumerable<ProjectList, CloudProject> projects = client.ListProjects();
foreach (CloudProject project in projects)
{
    Console.WriteLine($"{project.ProjectId}: {project.FriendlyName}");
}

try other thing go to your old project and enable big query. BigQuery is automatically enabled in new projects. To activate BigQuery in a pre-existing project, go to Enable the BigQuery API.

https://github.com/GoogleCloudPlatform/dotnet-docs-samples/tree/468bcdc02567a8e337bfa43db3fd523298265225/bigquery/api

I think either you provide right project id or service key. https://cloud.google.com/bigquery/docs/authentication/service-account-file

Upvotes: 0

Related Questions