javagoogle-cloud-dataflow

How can I list all running GCP Dataflow jobs using the Java client SDK


I am wondering if it is possible for me to list all running Dataflow jobs using Java client SDK.

I think I might need to use:

JobsV1Beta3Client jobsV1Beta3Client = JobsV1Beta3Client.create();
jobsV1Beta3Client.listJobs(...)

But I don't know what I should put inside the listJobs method.


Solution

  • You can reference these Dataflow code samples as it demonstrates how to list jobs.

    import com.google.dataflow.v1beta3.Job;
    import com.google.dataflow.v1beta3.JobView;
    import com.google.dataflow.v1beta3.JobsV1Beta3Client;
    import com.google.dataflow.v1beta3.ListJobsRequest;
    
    public class SyncListJobs {
    
      public static void main(String[] args) throws Exception {
        syncListJobs();
      }
    
      public static void syncListJobs() throws Exception {
        // This snippet has been automatically generated and should be regarded as a code template only.
        // It will require modifications to work:
        // - It may require correct/in-range values for request initialization.
        // - It may require specifying regional endpoints when creating the service client as shown in
        // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
        try (JobsV1Beta3Client jobsV1Beta3Client = JobsV1Beta3Client.create()) {
          ListJobsRequest request =
              ListJobsRequest.newBuilder()
                  .setProjectId("projectId-894832108")
                  .setView(JobView.forNumber(0))
                  .setPageSize(883849137)
                  .setPageToken("pageToken873572522")
                  .setLocation("location1901043637")
                  .build();
          for (Job element : jobsV1Beta3Client.listJobs(request).iterateAll()) {
            // doThingsWith(element);
          }
        }
      }
    }
    
    import com.google.api.core.ApiFuture;
    import com.google.dataflow.v1beta3.Job;
    import com.google.dataflow.v1beta3.JobView;
    import com.google.dataflow.v1beta3.JobsV1Beta3Client;
    import com.google.dataflow.v1beta3.ListJobsRequest;
    
    public class AsyncListJobs {
    
      public static void main(String[] args) throws Exception {
        asyncListJobs();
      }
    
      public static void asyncListJobs() throws Exception {
        // This snippet has been automatically generated and should be regarded as a code template only.
        // It will require modifications to work:
        // - It may require correct/in-range values for request initialization.
        // - It may require specifying regional endpoints when creating the service client as shown in
        // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
        try (JobsV1Beta3Client jobsV1Beta3Client = JobsV1Beta3Client.create()) {
          ListJobsRequest request =
              ListJobsRequest.newBuilder()
                  .setProjectId("projectId-894832108")
                  .setView(JobView.forNumber(0))
                  .setPageSize(883849137)
                  .setPageToken("pageToken873572522")
                  .setLocation("location1901043637")
                  .build();
          ApiFuture<Job> future = jobsV1Beta3Client.listJobsPagedCallable().futureCall(request);
          // Do something.
          for (Job element : future.get().iterateAll()) {
            // doThingsWith(element);
          }
        }
      }
    }
    

    I also found some old posts that can be of help: