Get a list of the Kafka consumer groups

## Explore further


For detailed documentation that includes this code sample, see the following:

- [List your Google Cloud Managed Service for Apache Kafka consumer groups](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/list-consumer-groups.md.txt)

## Code sample

### Go


Before trying this sample, follow the Go setup instructions in the
[Managed Service for Apache Kafka quickstart using
client libraries](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/reference/libraries#install).


For more information, see the
[Managed Service for Apache Kafka Go API
reference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/managedkafka/latest/apiv1).


To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```go
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


Before trying this sample, follow the Java setup instructions in the
[Managed Service for Apache Kafka quickstart using
client libraries](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/reference/libraries#install).


For more information, see the
[Managed Service for Apache Kafka Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/overview).


To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```java
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


Before trying this sample, follow the Python setup instructions in the
[Managed Service for Apache Kafka quickstart using
client libraries](https://docs.cloud.google.com/managed-service-for-apache-kafka/docs/reference/libraries#install).


For more information, see the
[Managed Service for Apache Kafka Python API
reference documentation](https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest).


To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment.md.txt).

```python
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)
```

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples.md.txt?product=apachekafkaforbigquery).