Menghapus topik Google Cloud Managed Service for Apache Kafka

Untuk menghapus satu topik, Anda dapat menggunakan Google Cloud konsol, Google Cloud CLI, library klien, Managed Kafka API, atau Apache Kafka API open source.

Peran dan izin yang diperlukan untuk menghapus topik

Untuk mendapatkan izin yang diperlukan guna menghapus topik, minta administrator untuk memberi Anda peran IAM Managed Kafka Topic Editor(roles/managedkafka.topicEditor) di project Anda. Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran bawaan ini berisi izin yang diperlukan untuk menghapus topik. Untuk melihat izin yang benar-benar diperlukan, perluas bagian Izin yang diperlukan:

Izin yang diperlukan

Izin berikut diperlukan untuk menghapus topik:

  • Menghapus topik: managedkafka.topics.delete

Anda mungkin juga bisa mendapatkan izin ini dengan peran khusus atau peran bawaan lainnya.

Untuk mengetahui informasi selengkapnya tentang peran ini, lihat Peran bawaan Managed Service for Apache Kafka.

Menghapus topik

Menghapus topik bersifat tidak dapat diurungkan dan akan mengakibatkan hilangnya data secara permanen yang disimpan dalam topik tersebut. Pastikan Anda memiliki cadangan yang sesuai atau telah mengekspor data yang diperlukan sebelum melanjutkan.

Pastikan Anda menghentikan atau mengonfigurasi ulang semua konsumen yang berlangganan topik untuk menggunakan topik lain sebelum menghapus topik.

Untuk menghapus topik, ikuti langkah-langkah berikut:

Konsol

  1. Di konsol Google Cloud , buka halaman Clusters.

    Buka Cluster

  2. Dari daftar cluster, klik cluster tempat topik yang ingin Anda hapus berada.

    Halaman Cluster details akan terbuka. Di halaman detail cluster, untuk tab Resources, topik akan dicantumkan.

  3. Klik topik yang ingin Anda hapus.

    Halaman Topic details akan terbuka.

  4. Klik Hapus dan konfirmasi operasinya.

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. Jalankan perintah gcloud managed-kafka topics delete:

    gcloud managed-kafka topics delete TOPIC_ID \
        --cluster=CLUSTER_ID \
        --location=LOCATION_ID
    

    Perintah ini menghapus topik yang ditentukan dari cluster Google Cloud Managed Service for Apache Kafka yang ditetapkan. Semua data yang terkait dengan topik akan dihapus, dan topik tidak lagi dapat diakses oleh produsen atau konsumen.

    Ganti kode berikut:

    • TOPIC_ID: ID topik yang akan dihapus.
    • CLUSTER_ID: ID cluster tempat topik berada.
    • LOCATION_ID: Lokasi cluster.
  3. REST

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • PROJECT_ID: Project ID Google Cloud Anda
    • LOCATION: lokasi cluster
    • CLUSTER_ID: ID cluster
    • TOPIC_ID: ID topik

    Metode HTTP dan URL:

    DELETE https://managedkafka.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/clusters/CLUSTER_ID/topics/TOPIC_ID

    Untuk mengirim permintaan Anda, perluas salah satu opsi berikut:

    Anda akan melihat respons JSON seperti berikut:

    {}
    

    Go

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Go API.

    Untuk melakukan autentikasi ke Managed Service untuk Apache Kafka, siapkan Kredensial Default Aplikasi(ADC). Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    import (
    	"context"
    	"fmt"
    	"io"
    
    	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
    	"google.golang.org/api/option"
    
    	managedkafka "cloud.google.com/go/managedkafka/apiv1"
    )
    
    func deleteTopic(w io.Writer, projectID, region, clusterID, topicID string, opts ...option.ClientOption) error {
    	// projectID := "my-project-id"
    	// region := "us-central1"
    	// clusterID := "my-cluster"
    	// topicID := "my-topic"
    	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)
    	topicPath := fmt.Sprintf("%s/topics/%s", clusterPath, topicID)
    	req := &managedkafkapb.DeleteTopicRequest{
    		Name: topicPath,
    	}
    	if err := client.DeleteTopic(ctx, req); err != nil {
    		return fmt.Errorf("client.DeleteTopic got err: %w", err)
    	}
    	fmt.Fprint(w, "Deleted topic\n")
    	return nil
    }
    

    Java

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Java API.

    Untuk melakukan autentikasi ke Managed Service for Apache Kafka, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    import com.google.api.gax.rpc.ApiException;
    import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
    import com.google.cloud.managedkafka.v1.TopicName;
    import java.io.IOException;
    
    public class DeleteTopic {
    
      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";
        String topicId = "my-topic";
        deleteTopic(projectId, region, clusterId, topicId);
      }
    
      public static void deleteTopic(String projectId, String region, String clusterId, String topicId)
          throws Exception {
        try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
          // This operation is being handled synchronously.
          managedKafkaClient.deleteTopic(TopicName.of(projectId, region, clusterId, topicId));
          System.out.println("Deleted topic");
        } catch (IOException | ApiException e) {
          System.err.printf("managedKafkaClient.deleteTopic got err: %s", e.getMessage());
        }
      }
    }
    

    Python

    Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Menginstal library klien. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Managed Service for Apache Kafka Python API.

    Untuk melakukan autentikasi ke Managed Service for Apache Kafka, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan ADC untuk lingkungan pengembangan lokal.

    from google.api_core.exceptions import NotFound
    from google.cloud import managedkafka_v1
    
    # TODO(developer)
    # project_id = "my-project-id"
    # region = "us-central1"
    # cluster_id = "my-cluster"
    # topic_id = "my-topic"
    
    client = managedkafka_v1.ManagedKafkaClient()
    
    topic_path = client.topic_path(project_id, region, cluster_id, topic_id)
    request = managedkafka_v1.DeleteTopicRequest(name=topic_path)
    
    try:
        client.delete_topic(request=request)
        print("Deleted topic")
    except NotFound as e:
        print(f"Failed to delete topic {topic_id} with error: {e.message}")
    

Apa langkah selanjutnya?