Répertorier les jobs dans toutes les régions (v1beta3)

Répertorier les jobs Dataflow dans toutes les régions d'un projet.

Exemple de code

Go

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.


import (
	"context"
	"fmt"
	"io"

	dataflow "cloud.google.com/go/dataflow/apiv1beta3"
	dataflowpb "cloud.google.com/go/dataflow/apiv1beta3/dataflowpb"
	"google.golang.org/api/iterator"
)

// aggregatedListJobs lists Dataflow jobs across all regions for a project.
func aggregatedListJobs(ctx context.Context, w io.Writer, projectID string) error {
	client, err := dataflow.NewJobsV1Beta3Client(ctx)
	if err != nil {
		return fmt.Errorf("NewJobsV1Beta3Client: %w", err)
	}
	defer client.Close()

	req := &dataflowpb.ListJobsRequest{
		ProjectId: projectID,
	}

	it := client.AggregatedListJobs(ctx, req)
	for {
		job, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("Next: %w", err)
		}

		fmt.Fprintf(w, "Found job: %s (ID: %s, State: %s, Location: %s)\n",
			job.GetName(),
			job.GetId(),
			job.GetCurrentState(),
			job.GetLocation(),
		)
	}

	return nil
}

Java

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import com.google.dataflow.v1beta3.Job;
import com.google.dataflow.v1beta3.JobsV1Beta3Client;
import com.google.dataflow.v1beta3.ListJobsRequest;
import java.io.IOException;

public class AggregatedListJobs {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    aggregatedListJobs(projectId);
  }

  public static void aggregatedListJobs(String projectId) throws IOException {
    try (JobsV1Beta3Client client = JobsV1Beta3Client.create()) {
      ListJobsRequest request = ListJobsRequest.newBuilder()
          .setProjectId(projectId)
          .build();

      for (Job job : client.aggregatedListJobs(request).iterateAll()) {
        System.out.printf("Job ID: %s, Name: %s, State: %s%n",
            job.getId(), job.getName(), job.getCurrentState());
      }
    }
  }
}

Node.js

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

'use strict';

const {JobsV1Beta3Client} = require('@google-cloud/dataflow');
const {status} = require('@grpc/grpc-js');

const client = new JobsV1Beta3Client();

/**
 * Use the aggregatedListJobs method
 *
 * @param {string} projectId - The ID of the Google Cloud project that owns the Dataflow jobs.
 */
async function aggregatedListJobs(projectId) {
  try {
    const request = {
      projectId,
    };

    // Retrieve Dataflow jobs across all regions for the specified project
    const [jobs] = await client.aggregatedListJobs(request);

    for (const job of jobs) {
      console.log(`Job ID: ${job.id}`);
      console.log(`Name: ${job.name}`);
      console.log(`Type: ${job.type}`);
      console.log(`Current State: ${job.currentState}`);
      console.log(`Location: ${job.location}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found for project: ${projectId}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied for project: ${projectId}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      console.error(`RPC failed: ${err.message}`);
    }
  }
}

module.exports = { aggregatedListJobs };

Python

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import google.api_core.exceptions
from google.cloud import dataflow_v1beta3

client = dataflow_v1beta3.JobsV1Beta3Client()


def aggregated_list_jobs(project_id: str) -> None:
    """Use the aggregated_list_jobs method

    Args:
        project_id: The ID of the Google Cloud project that owns the jobs.
    """
    request = dataflow_v1beta3.ListJobsRequest(
        project_id=project_id,
    )

    try:
        page_result = client.aggregated_list_jobs(request=request)
        for job in page_result:
            print(f"Job ID: {job.id}, Name: {job.name}, Location: {job.location}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"API call failed: {error}")

Étapes suivantes

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud , consultez l'explorateur d'exemplesGoogle Cloud .