List the Kafka clusters
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
Go
Before trying this sample, follow the Go setup instructions in the Managed Service for Apache Kafka quickstart using client libraries. For more information, see the Managed Service for Apache Kafka Go API reference documentation.
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.
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"
)
funclistClusters(wio.Writer,projectID,regionstring,opts...option.ClientOption)error{
// projectID := "my-project-id"
// region := "us-central1"
ctx:=context.Background()
client,err:=managedkafka.NewClient (ctx,opts...)
iferr!=nil{
returnfmt.Errorf("managedkafka.NewClient got err: %w",err)
}
deferclient.Close()
locationPath:=fmt.Sprintf("projects/%s/locations/%s",projectID,region)
req:=&managedkafkapb.ListClustersRequest{
Parent:locationPath,
}
clusterIter:=client.ListClusters(ctx,req)
for{
res,err:=clusterIter.Next()
iferr==iterator.Done{
break
}
iferr!=nil{
returnfmt.Errorf("clusterIter.Next() got err: %w",err)
}
fmt.Fprintf(w,"Got cluster: %v",res)
}
returnnil
}
Java
Before trying this sample, follow the Java setup instructions in the Managed Service for Apache Kafka quickstart using client libraries. For more information, see the Managed Service for Apache Kafka Java API reference documentation.
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.
importcom.google.api.gax.rpc.ApiException ;
importcom.google.cloud.managedkafka.v1.Cluster ;
importcom.google.cloud.managedkafka.v1.LocationName ;
importcom.google.cloud.managedkafka.v1.ManagedKafkaClient ;
importjava.io.IOException;
publicclass ListClusters{
publicstaticvoidmain(String[]args)throwsException{
// TODO(developer): Replace these variables before running the example.
StringprojectId="my-project-id";
Stringregion="my-region";// e.g. us-east1
listClusters(projectId,region);
}
publicstaticvoidlistClusters(StringprojectId,Stringregion)throwsException{
try(ManagedKafkaClient managedKafkaClient=ManagedKafkaClient .create()){
LocationName locationName=LocationName .of(projectId,region);
// This operation is being handled synchronously.
for(Cluster cluster:managedKafkaClient.listClusters(locationName).iterateAll()){
System.out.println(cluster.getAllFields());
}
}catch(IOException|ApiException e){
System.err.printf("managedKafkaClient.listClusters 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. For more information, see the Managed Service for Apache Kafka Python API reference documentation.
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.
fromgoogle.cloudimport managedkafka_v1
# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
client = managedkafka_v1 .ManagedKafkaClient ()
request = managedkafka_v1 .ListClustersRequest (
parent=client.common_location_path (project_id, region),
)
response = client.list_clusters (request=request)
for cluster in response:
print("Got cluster:", cluster)
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.