列出工作訊息

示範如何要求工作訊息清單。

程式碼範例

C#

如要向 Dataflow 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

using Google.Api.Gax;
using Google.Cloud.Dataflow.V1Beta3;
using Google.Protobuf.WellKnownTypes;
using System;

public sealed partial class GeneratedMessagesV1Beta3ClientSnippets
{
    /// <summary>Snippet for ListJobMessages</summary>
    /// <remarks>
    /// 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/dotnet/docs/reference/help/client-configuration#endpoint.
    /// </remarks>
    public void ListJobMessagesRequestObject()
    {
        // Create client
        MessagesV1Beta3Client messagesV1Beta3Client = MessagesV1Beta3Client.Create();
        // Initialize request argument(s)
        ListJobMessagesRequest request = new ListJobMessagesRequest
        {
            ProjectId = "",
            JobId = "",
            MinimumImportance = JobMessageImportance.Unknown,
            StartTime = new Timestamp(),
            EndTime = new Timestamp(),
            Location = "",
        };
        // Make the request
        PagedEnumerable<ListJobMessagesResponse, JobMessage> response = messagesV1Beta3Client.ListJobMessages(request);

        // Iterate over all response items, lazily performing RPCs as required
        foreach (JobMessage item in response)
        {
            // Do something with each item
            Console.WriteLine(item);
        }

        // Or iterate over pages (of server-defined size), performing one RPC per page
        foreach (ListJobMessagesResponse page in response.AsRawResponses())
        {
            // Do something with each page of items
            Console.WriteLine("A page of results:");
            foreach (JobMessage item in page)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
        }

        // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
        int pageSize = 10;
        Page<JobMessage> singlePage = response.ReadPage(pageSize);
        // Do something with the page of items
        Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
        foreach (JobMessage item in singlePage)
        {
            // Do something with each item
            Console.WriteLine(item);
        }
        // Store the pageToken, for when the next page is required.
        string nextPageToken = singlePage.NextPageToken;
    }
}

Go

如要向 Dataflow 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


import (
	"context"
	"fmt"
	"io"

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

// listJobMessages retrieves and prints messages for a Dataflow job.
func listJobMessages(ctx context.Context, w io.Writer, projectID string, locationID string, jobID string) error {
	client, err := dataflow.NewMessagesV1Beta3Client(ctx)
	if err != nil {
		return fmt.Errorf("NewMessagesV1Beta3Client failed: %w", err)
	}
	defer client.Close()

	req := &dataflowpb.ListJobMessagesRequest{
		ProjectId: projectID,
		Location:  locationID,
		JobId:     jobID,
	}

	it := client.ListJobMessages(ctx, req)
	for {
		msg, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to iterate job messages: %w", err)
		}
		fmt.Fprintf(w, "Message ID: %s, Importance: %s, Text: %s\n", msg.GetId(), msg.GetMessageImportance(), msg.GetMessageText())
	}

	return nil
}

Java

如要向 Dataflow 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import com.google.dataflow.v1beta3.JobMessage;
import com.google.dataflow.v1beta3.ListJobMessagesRequest;
import com.google.dataflow.v1beta3.MessagesV1Beta3Client;
import java.io.IOException;

public class ListJobMessages {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "us-central1";
    String jobId = "example-job-id";
    listJobMessages(projectId, locationId, jobId);
  }

  public static void listJobMessages(String projectId, String locationId, String jobId)
      throws IOException {
    // Initializes client using Application Default Credentials for authentication.
    try (MessagesV1Beta3Client client = MessagesV1Beta3Client.create()) {
      ListJobMessagesRequest request =
          ListJobMessagesRequest.newBuilder()
              .setProjectId(projectId)
              .setLocation(locationId)
              .setJobId(jobId)
              .build();

      for (JobMessage message : client.listJobMessages(request).iterateAll()) {
        System.out.printf("Message ID: %s%n", message.getId());
        System.out.printf("Importance: %s%n", message.getMessageImportance());
        System.out.printf("Text: %s%n", message.getMessageText());
      }
    }
  }
}

Python

如要向 Dataflow 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

# 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://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import dataflow_v1beta3


def sample_list_job_messages():
    # Create a client
    client = dataflow_v1beta3.MessagesV1Beta3Client()

    # Initialize request argument(s)
    request = dataflow_v1beta3.ListJobMessagesRequest()

    # Make the request
    page_result = client.list_job_messages(request=request)

    # Handle the response
    for response in page_result:
        print(response)

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具。