Google Cloud Managed Kafka

0.4.0 · active · verified Thu Apr 09

The `google-cloud-managedkafka` client library for Python allows developers to interact with the Google Cloud Managed Service for Apache Kafka API. This library, currently at version `0.4.0`, provides programmatic access to manage Kafka clusters, topics, and consumers within Google Cloud. It follows Google Cloud's frequent release cadence, with updates often tied to underlying API changes.

Warnings

Install

Imports

Quickstart

Initializes the ManagedKafkaClient and attempts to list existing Managed Kafka clusters in a specified Google Cloud project and location. This example demonstrates basic client instantiation and API interaction.

import os
from google.cloud.managedkafka_v1 import ManagedKafkaClient

# Set your Google Cloud project ID and location
# Ensure these environment variables are set or replace with actual values
project_id = os.environ.get("GCP_PROJECT_ID", "your-gcp-project-id")
location_id = os.environ.get("GCP_LOCATION_ID", "us-central1") # e.g., us-central1

# Construct the parent resource name
parent = f"projects/{project_id}/locations/{location_id}"

client = ManagedKafkaClient()

try:
    print(f"Listing Managed Kafka clusters in {parent}...")
    # The 'parent' parameter expects a string in the format 'projects/PROJECT_ID/locations/LOCATION_ID'
    clusters = client.list_clusters(parent=parent)
    found_clusters = False
    for cluster in clusters:
        print(f"  - Cluster name: {cluster.name}, Capacity: {cluster.capacity_config.vcpu_count} vCPUs")
        found_clusters = True
    if not found_clusters:
        print("  No clusters found. Ensure you have clusters deployed in this location.")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have authenticated (e.g., `gcloud auth application-default login`)")
    print("and have sufficient IAM permissions (e.g., `roles/managedkafka.viewer`).")

view raw JSON →