Kafka 클러스터 가져오기

Kafka 클러스터 정보 가져오기

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

Go

이 샘플을 사용해 보기 전에 Go Managed Service for Apache Kafka 빠른 시작: 클라이언트 라이브러리 사용의 설정 안내를 따르세요. 자세한 내용은 Managed Service for Apache Kafka Go API 참조 문서를 참고하세요.

Managed Service for Apache Kafka를 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import (
	"context"
	"fmt"
	"io"

	"cloud.google.com/go/managedkafka/apiv1/managedkafkapb"
	"google.golang.org/api/option"

	managedkafka "cloud.google.com/go/managedkafka/apiv1"
)

func getCluster(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.GetClusterRequest{
		Name: clusterPath,
	}
	cluster, err := client.GetCluster(ctx, req)
	if err != nil {
		return fmt.Errorf("client.GetCluster got err: %w", err)
	}
	fmt.Fprintf(w, "Got cluster: %#v\n", cluster)
	return nil
}

Java

이 샘플을 사용해 보기 전에 Java Managed Service for Apache Kafka 빠른 시작: 클라이언트 라이브러리 사용의 설정 안내를 따르세요. 자세한 내용은 Managed Service for Apache Kafka Java API 참조 문서를 참고하세요.

Managed Service for Apache Kafka를 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.managedkafka.v1.Cluster;
import com.google.cloud.managedkafka.v1.ClusterName;
import com.google.cloud.managedkafka.v1.ManagedKafkaClient;
import java.io.IOException;

public class GetCluster {

  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";
    getCluster(projectId, region, clusterId);
  }

  public static void getCluster(String projectId, String region, String clusterId)
      throws Exception {
    try (ManagedKafkaClient managedKafkaClient = ManagedKafkaClient.create()) {
      // This operation is being handled synchronously.
      Cluster cluster = managedKafkaClient.getCluster(ClusterName.of(projectId, region, clusterId));
      System.out.println(cluster.getAllFields());
    } catch (IOException | ApiException e) {
      System.err.printf("managedKafkaClient.getCluster got err: %s", e.getMessage());
    }
  }
}

Python

이 샘플을 사용해 보기 전에 Python Managed Service for Apache Kafka 빠른 시작: 클라이언트 라이브러리 사용의 설정 안내를 따르세요. 자세한 내용은 Managed Service for Apache Kafka Python API 참조 문서를 참고하세요.

Managed Service for Apache Kafka를 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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"

client = managedkafka_v1.ManagedKafkaClient()

cluster_path = client.cluster_path(project_id, region, cluster_id)
request = managedkafka_v1.GetClusterRequest(
    name=cluster_path,
)

try:
    cluster = client.get_cluster(request=request)
    print("Got cluster:", cluster)
except NotFound as e:
    print(f"Failed to get cluster {cluster_id} with error: {e.message}")

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기