Jobs in allen Regionen auflisten (v1beta3)

Dataflow-Jobs in allen Regionen für ein Projekt auflisten.

Codebeispiel

Go

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Dataflow zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.


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

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Dataflow zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Dataflow zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

'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

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Dataflow zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

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

Nächste Schritte

Wenn Sie nach Codebeispielen für andere Produkte von Google Cloud suchen und filtern möchten, können Sie den Beispielbrowser fürGoogle Cloud verwenden.