-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bigquery): add internal listProjects API to core client #13429
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import com.google.api.gax.paging.Page; | ||
| import com.google.api.services.bigquery.model.ErrorProto; | ||
| import com.google.api.services.bigquery.model.GetQueryResultsResponse; | ||
| import com.google.api.services.bigquery.model.ProjectList; | ||
| import com.google.api.services.bigquery.model.QueryRequest; | ||
| import com.google.api.services.bigquery.model.TableDataInsertAllRequest; | ||
| import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows; | ||
|
|
@@ -65,6 +66,25 @@ | |
|
|
||
| final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery { | ||
|
|
||
| private static class ProjectPageFetcher implements NextPageFetcher<Project> { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
| private final Map<BigQueryRpc.Option, ?> requestOptions; | ||
| private final BigQueryOptions serviceOptions; | ||
|
|
||
| ProjectPageFetcher( | ||
| BigQueryOptions serviceOptions, String cursor, Map<BigQueryRpc.Option, ?> optionMap) { | ||
| this.requestOptions = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it automatically populate maxResults/pageSize?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, The |
||
| PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); | ||
| this.serviceOptions = serviceOptions; | ||
| } | ||
|
|
||
| @Override | ||
| public Page<Project> getNextPage() { | ||
| return listProjects(serviceOptions, requestOptions); | ||
| } | ||
| } | ||
|
|
||
| private static class DatasetPageFetcher implements NextPageFetcher<Dataset> { | ||
|
|
||
| private static final long serialVersionUID = -3057564042439021278L; | ||
|
|
@@ -307,6 +327,49 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Page<Project> listProjects(ProjectListOption... options) { | ||
| Span projectsList = null; | ||
| if (getOptions().isOpenTelemetryTracingEnabled() | ||
| && getOptions().getOpenTelemetryTracer() != null) { | ||
| projectsList = | ||
| getOptions() | ||
| .getOpenTelemetryTracer() | ||
| .spanBuilder("com.google.cloud.bigquery.BigQuery.listProjects") | ||
| .setAllAttributes(otelAttributesFromOptions(options)) | ||
| .startSpan(); | ||
| } | ||
| try (Scope projectsListScope = projectsList != null ? projectsList.makeCurrent() : null) { | ||
| return listProjects(getOptions(), optionMap(options)); | ||
| } finally { | ||
| if (projectsList != null) { | ||
| projectsList.end(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static Page<Project> listProjects( | ||
| final BigQueryOptions serviceOptions, final Map<BigQueryRpc.Option, ?> optionsMap) { | ||
| Tuple<String, Iterable<ProjectList.Projects>> result = | ||
| serviceOptions.getBigQueryRpcV2().listProjects(optionsMap); | ||
| String nextPageToken = result.x(); | ||
| Iterable<Project> projects = | ||
| Iterables.transform( | ||
| result.y() != null ? result.y() : ImmutableList.<ProjectList.Projects>of(), | ||
| projectPb -> | ||
| new Project( | ||
| projectPb.getId(), | ||
| projectPb.getNumericId() != null | ||
| ? String.valueOf(projectPb.getNumericId()) | ||
| : null, | ||
| projectPb.getProjectReference() != null | ||
| ? projectPb.getProjectReference().getProjectId() | ||
| : null, | ||
| projectPb.getFriendlyName())); | ||
| return new PageImpl<>( | ||
| new ProjectPageFetcher(serviceOptions, nextPageToken, optionsMap), nextPageToken, projects); | ||
| } | ||
|
|
||
| @Override | ||
| public Table create(TableInfo tableInfo, TableOption... options) { | ||
| final com.google.api.services.bigquery.model.Table tablePb = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| @BetaApi | ||
| public class Project implements Serializable { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final String id; | ||
| private final String numericId; | ||
| private final String projectId; | ||
| private final String friendlyName; | ||
|
|
||
| public Project(String id, String numericId, String projectId, String friendlyName) { | ||
| this.id = id; | ||
| this.numericId = numericId; | ||
| this.projectId = projectId; | ||
| this.friendlyName = friendlyName; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getNumericId() { | ||
| return numericId; | ||
| } | ||
|
|
||
| public String getProjectId() { | ||
| return projectId; | ||
| } | ||
|
|
||
| public String getFriendlyName() { | ||
| return friendlyName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Project project = (Project) o; | ||
| return Objects.equals(id, project.id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use String.equals there?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since some of these fields (like |
||
| && Objects.equals(numericId, project.numericId) | ||
| && Objects.equals(projectId, project.projectId) | ||
| && Objects.equals(friendlyName, project.friendlyName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, numericId, projectId, friendlyName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Project{" | ||
| + "id='" | ||
| + id | ||
| + '\'' | ||
| + ", numericId='" | ||
| + numericId | ||
| + '\'' | ||
| + ", projectId='" | ||
| + projectId | ||
| + '\'' | ||
| + ", friendlyName='" | ||
| + friendlyName | ||
| + '\'' | ||
| + '}'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep name consistent with API?
maxResults?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain consistency with the rest of the API, I used
pageSize(). ThroughoutBigQuery.java, all paginated APIs (likeDatasetListOption,TableListOption, andJobListOption) usepageSize()