删除连接器

删除连接器会将其从 Connect 集群中永久移除。此操作会停止源系统与目标系统之间的数据流动。在删除连接器之前,请务必了解其对数据流水线的影响。

如需删除 Connect 集群中的连接器,您可以使用 Google Cloud 控制台、gcloud CLI、Managed Service for Apache Kafka 客户端库或 Managed Kafka API。您无法使用开源 Apache Kafka API 删除连接器。

删除连接器所需的角色和权限

如需获得删除连接器所需的权限,请让您的管理员为您授予包含 Connect 集群的项目的 Managed Kafka Connector Editor (roles/managedkafka.connectorEditor) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色包含删除连接器所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

您需要具备以下权限才能删除连接器:

  • 授予对所请求连接器的删除连接器权限: managedkafka.connectors.delete
  • 在包含连接器的 Connect 集群上授予列出连接器权限。仅当使用控制台删除时才需要此权限: managedkafka.connectors.list

您也可以使用自定义角色或其他预定义角色来获取这些权限。

删除连接器

控制台

  1. 在 Google Cloud 控制台中,前往连接集群页面。

    前往“关联集群”

  2. 点击要删除的连接器所在的连接集群。

    系统会显示连接集群详情页面。

  3. 资源标签页中,找到列表中的连接器,然后点击其名称。

    系统会将您重定向到连接器详情页面。

  4. 点击删除

gcloud

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  2. 使用 gcloud managed-kafka connectors delete 命令删除连接器:

    gcloud managed-kafka connectors delete CONNECTOR_ID \
        --location=LOCATION \
        --connect-cluster=CONNECT_CLUSTER_ID
    

    替换以下内容:

    • CONNECTOR_ID:必填。要删除的连接器的 ID。
    • LOCATION:必填。包含连接器的 Connect 集群的位置。
    • CONNECT_CLUSTER_ID:必填。包含连接器的 Connect 集群的 ID。

    示例命令:

    gcloud managed-kafka connectors delete test-connector \
        --location=us-central1 \
        --connect-cluster=test-connect-cluster
    

Go

在尝试此示例之前,请按照 安装客户端库中的 Go 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Go API 参考文档

如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据(ADC)。 如需了解详情,请参阅为本地开发环境设置 ADC

import (
	"context"
	"fmt"
	"io"

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

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

func deleteConnector(w io.Writer, projectID, region, connectClusterID, connectorID string, opts ...option.ClientOption) error {
	// projectID := "my-project-id"
	// region := "us-central1"
	// connectClusterID := "my-connect-cluster"
	// connectorID := "my-connector"
	ctx := context.Background()
	client, err := managedkafka.NewManagedKafkaConnectClient(ctx, opts...)
	if err != nil {
		return fmt.Errorf("managedkafka.NewManagedKafkaConnectClient got err: %w", err)
	}
	defer client.Close()

	connectorPath := fmt.Sprintf("projects/%s/locations/%s/connectClusters/%s/connectors/%s", projectID, region, connectClusterID, connectorID)
	req := &managedkafkapb.DeleteConnectorRequest{
		Name: connectorPath,
	}
	err = client.DeleteConnector(ctx, req)
	if err != nil {
		return fmt.Errorf("client.DeleteConnector got err: %w", err)
	}
	fmt.Fprint(w, "Deleted connector\n")
	return nil
}

Java

在尝试此示例之前,请按照 安装客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Java API 参考文档

如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置 ADC

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.managedkafka.v1.ConnectorName;
import com.google.cloud.managedkafka.v1.ManagedKafkaConnectClient;
import java.io.IOException;

public class DeleteConnector {

  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-connect-cluster";
    String connectorId = "my-connector";
    deleteConnector(projectId, region, clusterId, connectorId);
  }

  public static void deleteConnector(
      String projectId, String region, String clusterId, String connectorId) throws IOException {
    try (ManagedKafkaConnectClient managedKafkaConnectClient = ManagedKafkaConnectClient.create()) {
      ConnectorName name = ConnectorName.of(projectId, region, clusterId, connectorId);
      // This operation is handled synchronously.
      managedKafkaConnectClient.deleteConnector(name);
      System.out.printf("Deleted connector: %s\n", name);
    } catch (IOException | ApiException e) {
      System.err.printf("managedKafkaConnectClient.deleteConnector got err: %s\n", e.getMessage());
    }
  }
}

Python

在尝试此示例之前,请按照 安装客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 Managed Service for Apache Kafka Python API 参考文档

如需向 Managed Service for Apache Kafka 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置 ADC

from google.api_core.exceptions import GoogleAPICallError
from google.cloud.managedkafka_v1.services.managed_kafka_connect import (
    ManagedKafkaConnectClient,
)
from google.cloud import managedkafka_v1

# TODO(developer)
# project_id = "my-project-id"
# region = "us-central1"
# connect_cluster_id = "my-connect-cluster"
# connector_id = "my-connector"

connect_client = ManagedKafkaConnectClient()

request = managedkafka_v1.DeleteConnectorRequest(
    name=connect_client.connector_path(project_id, region, connect_cluster_id, connector_id),
)

try:
    operation = connect_client.delete_connector(request=request)
    print(f"Waiting for operation {operation.operation.name} to complete...")
    operation.result()
    print("Deleted connector")
except GoogleAPICallError as e:
    print(f"The operation failed with error: {e}")

Apache Kafka® 是 Apache Software Foundation 或其关联公司在美国和/或其他国家/地区的注册商标。