List the Kafka clusters
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
Get a list of the Kafka clusters
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# List the Kafka clusters\n\nGet a list of the Kafka clusters\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [List your Google Cloud Managed Service for Apache Kafka clusters](/managed-service-for-apache-kafka/docs/list-clusters)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Go API\nreference documentation](https://cloud.google.com/go/docs/reference/cloud.google.com/go/managedkafka/latest/apiv1).\n\n\nTo authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/managedkafka/apiv1/managedkafkapb\"\n \t\"google.golang.org/api/iterator\"\n \t\"google.golang.org/api/option\"\n\n \tmanagedkafka \"cloud.google.com/go/managedkafka/apiv1\"\n )\n\n func listClusters(w io.Writer, projectID, region string, opts ...option.ClientOption) error {\n \t// projectID := \"my-project-id\"\n \t// region := \"us-central1\"\n \tctx := context.Background()\n \tclient, err := managedkafka.https://cloud.google.com/go/docs/reference/cloud.google.com/go/managedkafka/latest/apiv1.html#cloud_google_com_go_managedkafka_apiv1_Client_NewClient(ctx, opts...)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"managedkafka.NewClient got err: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tlocationPath := fmt.Sprintf(\"projects/%s/locations/%s\", projectID, region)\n \treq := &managedkafkapb.ListClustersRequest{\n \t\tParent: locationPath,\n \t}\n \tclusterIter := client.ListClusters(ctx, req)\n \tfor {\n \t\tres, err := clusterIter.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"clusterIter.Next() got err: %w\", err)\n \t\t}\n \t\tfmt.Fprintf(w, \"Got cluster: %v\", res)\n \t}\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/overview).\n\n\nTo authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;\n import com.google.cloud.managedkafka.v1.https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.Cluster.html;\n import com.google.cloud.managedkafka.v1.https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.LocationName.html;\n import com.google.cloud.managedkafka.v1.https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html;\n import java.io.IOException;\n\n public class ListClusters {\n\n public static void main(String[] args) throws Exception {\n // TODO(developer): Replace these variables before running the example.\n String projectId = \"my-project-id\";\n String region = \"my-region\"; // e.g. us-east1\n listClusters(projectId, region);\n }\n\n public static void listClusters(String projectId, String region) throws Exception {\n try (https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html managedKafkaClient = https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.ManagedKafkaClient.html.create()) {\n https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.LocationName.html locationName = https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.LocationName.html.of(projectId, region);\n // This operation is being handled synchronously.\n for (https://cloud.google.com/java/docs/reference/google-cloud-managedkafka/latest/com.google.cloud.managedkafka.v1.Cluster.html cluster : managedKafkaClient.listClusters(locationName).iterateAll()) {\n System.out.println(cluster.getAllFields());\n }\n } catch (IOException | https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html e) {\n System.err.printf(\"managedKafkaClient.listClusters got err: %s\", e.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ErrorDetails.html#com_google_api_gax_rpc_ErrorDetails__T_getMessage_java_lang_Class_T__());\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Managed Service for Apache Kafka quickstart using\nclient libraries](/managed-service-for-apache-kafka/docs/reference/libraries#install).\n\n\nFor more information, see the\n[Managed Service for Apache Kafka Python API\nreference documentation](https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest).\n\n\nTo authenticate to Managed Service for Apache Kafka, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/\n\n # TODO(developer)\n # project_id = \"my-project-id\"\n # region = \"us-central1\"\n\n client = https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html()\n\n request = https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.types.ListClustersRequest.html(\n parent=client.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html#google_cloud_managedkafka_v1_services_managed_kafka_ManagedKafkaClient_common_location_path(project_id, region),\n )\n\n response = client.https://cloud.google.com/python/docs/reference/google-cloud-managedkafka/latest/google.cloud.managedkafka_v1.services.managed_kafka.ManagedKafkaClient.html#google_cloud_managedkafka_v1_services_managed_kafka_ManagedKafkaClient_list_clusters(request=request)\n for cluster in response:\n print(\"Got cluster:\", cluster)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=managedkafka)."]]