|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package job |
| 16 | + |
| 17 | +// [START bigquery_list_jobs_preview] |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + "cloud.google.com/go/bigquery/v2/apiv2/bigquerypb" |
| 24 | + "cloud.google.com/go/bigquery/v2/apiv2_client" |
| 25 | + |
| 26 | + "google.golang.org/api/iterator" |
| 27 | +) |
| 28 | + |
| 29 | +// listJobs demonstrates iterating through job metadata. |
| 30 | +func listJobs(client *apiv2_client.Client, w io.Writer, projectID string) error { |
| 31 | + // client can be instantiated per-RPC service, or use cloud.google.com/go/bigquery/v2/apiv2_client to create |
| 32 | + // an aggregate client. |
| 33 | + // |
| 34 | + // projectID := "my-project-id" |
| 35 | + ctx := context.Background() |
| 36 | + |
| 37 | + req := &bigquerypb.ListJobsRequest{ |
| 38 | + ProjectId: projectID, |
| 39 | + // Only list pending or running jobs. |
| 40 | + StateFilter: []bigquerypb.ListJobsRequest_StateFilter{ |
| 41 | + bigquerypb.ListJobsRequest_PENDING, |
| 42 | + bigquerypb.ListJobsRequest_RUNNING, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + // ListJobs returns an iterator so users don't have to manage pagination when processing |
| 47 | + // the results. |
| 48 | + it := client.ListJobs(ctx, req) |
| 49 | + |
| 50 | + // Process data from the iterator one result at a time, and stop after we |
| 51 | + // process a fixed number of jobs. While the number of inflight |
| 52 | + // (pending or running) jobs may be more reasonable, listing all jobs can yield |
| 53 | + // a potentially very large number of results. |
| 54 | + maxJobs := 10 |
| 55 | + for numJobs := 0; numJobs < maxJobs; numJobs++ { |
| 56 | + job, err := it.Next() |
| 57 | + if err == iterator.Done { |
| 58 | + // We're reached the end of the iteration, break the loop. |
| 59 | + break |
| 60 | + } |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("iterator errored: %w", err) |
| 63 | + } |
| 64 | + // Print basic information to the provided writer. |
| 65 | + fmt.Fprintf(w, "job %q in location %q is in state %q\n", |
| 66 | + job.GetJobReference().GetJobId(), |
| 67 | + job.GetJobReference().GetLocation(), |
| 68 | + job.GetState()) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// [END bigquery_list_jobs_preview] |
0 commit comments