列出您的 Google Cloud Managed Service for Apache Kafka 消费者群组

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

列出消费群组所需的角色和权限

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

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

所需权限

您需要拥有以下权限才能列出您的消费群组:

  • 列出使用方群组: managedkafka.consumerGroups.list
  • 获取使用方群组详细信息: managedkafka.consumerGroups.get

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

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

列出您的使用方群组

如需列出特定集群的所有消费者群组,请按以下步骤操作:

控制台

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

    转到集群

  2. 点击要查看其消费群组的集群。

    系统会显示集群详情页面。在集群详情页面中,资源标签页下列出了消费群组。

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-service-for-apache-kafka consumer-groups list 命令:

    gcloud managed-kafka consumer-groups list CLUSTER_ID \
        --location=LOCATION \
        --limit=LIMIT

    此命令会列出指定集群和位置中的所有 Managed Service for Apache Kafka 消费者群组。

    替换以下内容:

    • CLUSTER_ID:集群的 ID 或名称。

    • LOCATION:集群的位置。

    • LIMIT:要列出的消费群组数量上限。

  3. 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 listConsumerGroups(w io.Writer, projectID, region, clusterID string, opts ...option.ClientOption) error {
    	// projectID := "my-project-id"
    	// region := "us-central1"
    	// clusterID := "my-cluster"
    	ctx := context.Background()
    	client, err := managedkafka.NewClient(ctx, opts...)
    	if err != nil {
    		return fmt.Errorf("managedkafka.NewClient got err: %w", err)
    	}
    	defer client.Close()
    
    	clusterPath := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", projectID, region, clusterID)
    	req := &managedkafkapb.ListConsumerGroupsRequest{
    		Parent: clusterPath,
    	}
    	consumerGroupIter := client.ListConsumerGroups(ctx, req)
    	for {
    		res, err := consumerGroupIter.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return fmt.Errorf("consumerGroupIter.Next() got err: %w", err)
    		}
    		fmt.Fprintf(w, "Got consumer group: %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.ClusterName;
    import com.google.cloud.managedkafka.v1.ConsumerGroup;
    import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
    import java.io.IOException;
    
    public class ListConsumerGroups {
    
      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
        String clusterId = "my-cluster";
        listConsumerGroups(projectId, region, clusterId);
      }
    
      public static void listConsumerGroups(String projectId, String region, String clusterId)
          throws Exception {
        try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
          ClusterName clusterName = ClusterName.of(projectId, region, clusterId);
          // This operation is being handled synchronously.
          for (ConsumerGroup consumerGroup :
              managedKafkaClient.listConsumerGroups(clusterName).iterateAll()) {
            System.out.println(consumerGroup.getAllFields());
          }
        } catch (IOException | ApiException e) {
          System.err.printf("managedKafkaClient.listConsumerGroups got err: %s", e.getMessage());
        }
      }
    }
    

    Python

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

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

    from google.cloud import managedkafka_v1
    
    # TODO(developer)
    # project_id = "my-project-id"
    # region = "us-central1"
    # cluster_id = "my-cluster"
    
    client = managedkafka_v1.ManagedKafkaClient()
    
    request = managedkafka_v1.ListConsumerGroupsRequest(
        parent=client.cluster_path(project_id, region, cluster_id),
    )
    
    response = client.list_consumer_groups(request=request)
    for consumer_group in response:
        print("Got consumer group:", consumer_group)
    

后续步骤

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