Elenca i job in tutte le regioni (v1beta3)

Elenca i job Dataflow in tutte le regioni di un progetto.

Esempio di codice

Go

Per eseguire l'autenticazione in Dataflow, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.


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

Per eseguire l'autenticazione in Dataflow, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

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

Per eseguire l'autenticazione in Dataflow, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

'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

Per eseguire l'autenticazione in Dataflow, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

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}")

Passaggi successivi

Per cercare e filtrare gli esempi di codice per altri prodotti Google Cloud , consulta il browser degli esempi diGoogle Cloud .