列出您的 Connect 集群

列出 Connect 集群后,您可以在一个窗格中查看所有 Connect 集群的详细信息,包括集群的运行状况、位置、正常运行时间、计算规模、关联的 Google Cloud Managed Service for Apache Kafka 集群和标签等详细信息。

如需列出您的 Connect 集群,您可以使用 Google Cloud 控制台、gcloud CLI、客户端库或 Managed Kafka API。您无法使用开源 Apache Kafka API 列出 Connect 集群。

列出您的 Connect 集群所需的角色和权限

如需获得列出 Connect 集群所需的权限,请让您的管理员为您授予项目的 Managed Kafka Viewer (roles/managedkafka.viewer) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色包含列出 Connect 集群所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

您需要具备以下权限才能列出您的 Connect 集群:

  • 在指定位置授予列出集群的权限: managedkafka.connectClusters.list
  • 在指定位置授予获取集群详情的权限: managedkafka.connectClusters.get

您也可以使用自定义角色或其他预定义角色来获取这些权限。

如需详细了解 Managed Kafka Viewer 角色,请参阅 Managed Service for Apache Kafka 预定义角色

查看您的 Connect 集群

控制台

  1. 在 Google Cloud 控制台中,前往连接集群页面。

    前往“关联集群”

  2. 系统会列出您在项目中创建的集群。您可以在该页面上查看以下集群属性:

    • 名称:Connect 集群的唯一标识符。您可以使用它在各种操作中引用 Connect 集群。
    • 状态:表示 Connect 集群的当前运行状态,例如 Active
    • 位置:Connect 集群的托管地理位置。
    • 更新时间:上次更新关联集群的时间。
    • vCPUs:分配给 Connect 集群的 vCPU 数量。
    • 内存:分配给 Connect 集群的内存总量。
    • 主 Kafka 集群:与 Connect 集群关联的 Managed Service for Apache Kafka 集群。
    • 标签:可附加到 Connect 集群的键值对,用于组织、过滤和自动化。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 使用 gcloud managed-kafka connect-clusters list 命令列出 Connect 集群:

    gcloud managed-kafka connect-clusters list \
        --location=LOCATION
    
  3. 如需进一步优化 Connect 集群列表,您可以使用其他标志:

    gcloud managed-kafka connect-clusters list \
        --location=LOCATION \
        [--filter=EXPRESSION] \
        [--limit=LIMIT] \
        [--page-size=PAGE_SIZE] \
        [--sort-by=SORT_BY]
    

    替换以下内容:

    • LOCATION:必填。您要列出的 Connect 集群的位置。

    • EXPRESSION:(可选)要应用于列表的布尔值过滤条件表达式。如果表达式的计算结果为 True,则相应项会包含在列表中。如需了解详情和示例,请运行 gcloud topic filters

    • LIMIT:(可选)要显示的最大 Connect 集群数。如果未指定,则列出所有 Connect 集群。

    • PAGE_SIZE:(可选)每页显示的结果数。如果未指定,服务将确定合适的网页大小。

    • SORT_BY:(可选)以英文逗号分隔的排序依据字段列表。默认排序顺序为升序。如需按降序排序,请在相应字段前面加上 ~

  4. 输出示例:

    NAME                   VCPU  MEMORY_BYTES  STATE   NETWORK
    connect-cluster-alpha  3     3221225472    ACTIVE
    connect-cluster-beta   3     3221225472    ACTIVE
    
    

    Go

    在尝试此示例之前,请按照 安装客户端库中的 Go 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Go API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据(ADC)。 如需了解详情,请参阅为本地开发环境设置 ADC

    import (
    	"context"
    	"fmt"
    	"io"
    
    	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
    	"google.golang.org/api/iterator"
    	"google.golang.org/api/option"
    
    	managedkafka "cloud.google.com/go/managedkafka/apiv1"
    )
    
    func listConnectClusters(w io.Writer, projectID, region string, opts ...option.ClientOption) error {
    	// projectID := "my-project-id"
    	// region := "us-central1"
    	ctx := context.Background()
    	client, err := managedkafka.NewManagedKafkaConnectClient(ctx, opts...)
    	if err != nil {
    		return fmt.Errorf("managedkafka.NewManagedKafkaConnectClient got err: %w", err)
    	}
    	defer client.Close()
    
    	locationPath := fmt.Sprintf("projects/%s/locations/%s", projectID, region)
    	req := &managedkafkapb.ListConnectClustersRequest{
    		Parent: locationPath,
    	}
    	clusterIter := client.ListConnectClusters(ctx, req)
    	for {
    		res, err := clusterIter.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return fmt.Errorf("clusterIter.Next() got err: %w", err)
    		}
    		fmt.Fprintf(w, "Got connect cluster: %v", res)
    	}
    	return nil
    }
    

    Java

    在尝试此示例之前,请按照 安装客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Java API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置 ADC

    import com.google.api.gax.rpc.ApiException;
    import com.google.cloud.managedkafka.v1.ConnectCluster;
    import com.google.cloud.managedkafka.v1.LocationName;
    import com.google.cloud.managedkafka.v1.ManagedKafkaConnectClient;
    import java.io.IOException;
    
    public class ListConnectClusters {
    
      public static void main(String[] args) throws Exception {
        // TODO(developer): Replace these variables before running the example.
        String projectId = "my-project-id";
        String region = "my-region"; // e.g. us-east1
        listConnectClusters(projectId, region);
      }
    
      public static void listConnectClusters(String projectId, String region) throws Exception {
        try (ManagedKafkaConnectClient managedKafkaConnectClient = 
            ManagedKafkaConnectClient.create()) {
          LocationName locationName = LocationName.of(projectId, region);
          // This operation is being handled synchronously.
          for (ConnectCluster connectCluster : managedKafkaConnectClient
              .listConnectClusters(locationName).iterateAll()) {
            System.out.println(connectCluster.getAllFields());
          }
        } catch (IOException | ApiException e) {
          System.err.printf("managedKafkaConnectClient.listConnectClusters got err: %s\n", 
              e.getMessage());
        }
      }
    }
    

    Python

    在尝试此示例之前,请按照 安装客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Python API 参考文档

    如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置 ADC

    from google.cloud import managedkafka_v1
    from google.cloud.managedkafka_v1.services.managed_kafka_connect import (
        ManagedKafkaConnectClient,
    )
    from google.api_core.exceptions import GoogleAPICallError
    
    # TODO(developer)
    # project_id = "my-project-id"
    # region = "us-central1"
    
    connect_client = ManagedKafkaConnectClient()
    
    request = managedkafka_v1.ListConnectClustersRequest(
        parent=connect_client.common_location_path(project_id, region),
    )
    
    response = connect_client.list_connect_clusters(request=request)
    for cluster in response:
        try:
            print("Got Connect cluster:", cluster)
        except GoogleAPICallError as e:
            print(f"Failed to list Connect clusters with error: {e}")
    

后续步骤

Apache Kafka® 是 Apache Software Foundation 或其关联公司在美国和/或其他国家/地区的注册商标。