Skip to content

Add Managed Kafka Connect Connectors Examples #13522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c928344
Add Managed Kafka Connect code samples for
salmany Jul 24, 2025
8f2224a
Update google-cloud-managedkafka version to 0.1.12
salmany Jul 24, 2025
1feb354
Update managedkafka/snippets/connect/clusters/create_connect_cluster.py
salmany Jul 24, 2025
5840336
Update managedkafka/snippets/connect/clusters/create_connect_cluster.py
salmany Jul 24, 2025
d0df10f
Update managedkafka/snippets/connect/clusters/delete_connect_cluster.py
salmany Jul 24, 2025
505759a
Update managedkafka/snippets/connect/clusters/get_connect_cluster.py
salmany Jul 24, 2025
d3fba2c
Update managedkafka/snippets/connect/clusters/list_connect_clusters.py
salmany Jul 24, 2025
0a85a8b
Update managedkafka/snippets/connect/clusters/update_connect_cluster.py
salmany Jul 24, 2025
dfa07d2
Add timeouts and improve error handling.
salmany Jul 24, 2025
ab697bf
Addressed PR comments.
salmany Jul 28, 2025
8af6e36
Add Managed Kafka Connect Connectors examples
salmany Jul 29, 2025
bc121bd
Fix import statements and add headers.
salmany Jul 29, 2025
b9a15ef
Fix sample configs and update MM2 connector.
salmany Aug 7, 2025
a26f7b9
Remove unwanted local test artifact.
salmany Aug 7, 2025
af7a628
Adds requirements.txt file for samples.
salmany Jul 29, 2025
1ae987f
Remove timeout in update_connect_cluster.py
salmany Jul 30, 2025
3e788a6
Fix CPU and memory values for Cluster creation.
salmany Aug 4, 2025
e908ecd
Fixed comment for minimum vCpu.
salmany Aug 7, 2025
1ede52b
Fix lint by adding space.
salmany Aug 7, 2025
fac49a9
Merge branch 'main' into mkc-connectors-examples
glasnt Aug 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions managedkafka/snippets/connect/connectors/connectors_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest import mock
from unittest.mock import MagicMock

from google.api_core.operation import Operation
from google.cloud import managedkafka_v1
import pytest

import create_bigquery_sink_connector
import create_cloud_storage_sink_connector
import create_mirrormaker_source_connector
import create_pubsub_sink_connector
import create_pubsub_source_connector

PROJECT_ID = "test-project-id"
REGION = "us-central1"
CONNECT_CLUSTER_ID = "test-connect-cluster-id"


@mock.patch(
"google.cloud.managedkafka_v1.services.managed_kafka_connect.ManagedKafkaConnectClient.create_connector"
)
def test_create_mirrormaker2_connector(
mock_method: MagicMock,
capsys: pytest.CaptureFixture[str],
) -> None:
connector_id = "MM2_CONNECTOR_ID"
operation = mock.MagicMock(spec=Operation)
connector = managedkafka_v1.types.Connector()
connector.name = connector_id
operation.result = mock.MagicMock(return_value=connector)
mock_method.return_value = operation

create_mirrormaker_source_connector.create_mirrormaker_source_connector(
PROJECT_ID,
REGION,
CONNECT_CLUSTER_ID,
"3",
connector_id,
"GMK_TOPIC_NAME",
"source",
"target",
"GMK_SOURCE_CLUSTER_DNS",
"GMK_TARGET_CLUSTER_DNS",
)

out, _ = capsys.readouterr()
assert "Created Connector" in out
assert connector_id in out
mock_method.assert_called_once()


@mock.patch(
"google.cloud.managedkafka_v1.services.managed_kafka_connect.ManagedKafkaConnectClient.create_connector"
)
def test_create_pubsub_source_connector(
mock_method: MagicMock,
capsys: pytest.CaptureFixture[str],
) -> None:
connector_id = "CPS_SOURCE_CONNECTOR_ID"
operation = mock.MagicMock(spec=Operation)
connector = managedkafka_v1.types.Connector()
connector.name = connector_id
operation.result = mock.MagicMock(return_value=connector)
mock_method.return_value = operation

create_pubsub_source_connector.create_pubsub_source_connector(
PROJECT_ID,
REGION,
CONNECT_CLUSTER_ID,
connector_id,
"GMK_TOPIC_ID",
"CPS_SUBSCRIPTION_ID",
"GCP_PROJECT_ID",
"3",
"org.apache.kafka.connect.converters.ByteArrayConverter",
"org.apache.kafka.connect.storage.StringConverter",
)

out, _ = capsys.readouterr()
assert "Created Connector" in out
assert connector_id in out
mock_method.assert_called_once()


@mock.patch(
"google.cloud.managedkafka_v1.services.managed_kafka_connect.ManagedKafkaConnectClient.create_connector"
)
def test_create_pubsub_sink_connector(
mock_method: MagicMock,
capsys: pytest.CaptureFixture[str],
) -> None:
connector_id = "CPS_SINK_CONNECTOR_ID"
operation = mock.MagicMock(spec=Operation)
connector = managedkafka_v1.types.Connector()
connector.name = connector_id
operation.result = mock.MagicMock(return_value=connector)
mock_method.return_value = operation

create_pubsub_sink_connector.create_pubsub_sink_connector(
PROJECT_ID,
REGION,
CONNECT_CLUSTER_ID,
connector_id,
"GMK_TOPIC_ID",
"org.apache.kafka.connect.storage.StringConverter",
"org.apache.kafka.connect.storage.StringConverter",
"CPS_TOPIC_ID",
"GCP_PROJECT_ID",
"3",
)

out, _ = capsys.readouterr()
assert "Created Connector" in out
assert connector_id in out
mock_method.assert_called_once()


@mock.patch(
"google.cloud.managedkafka_v1.services.managed_kafka_connect.ManagedKafkaConnectClient.create_connector"
)
def test_create_cloud_storage_sink_connector(
mock_method: MagicMock,
capsys: pytest.CaptureFixture[str],
) -> None:
connector_id = "GCS_SINK_CONNECTOR_ID"
operation = mock.MagicMock(spec=Operation)
connector = managedkafka_v1.types.Connector()
connector.name = connector_id
operation.result = mock.MagicMock(return_value=connector)
mock_method.return_value = operation

create_cloud_storage_sink_connector.create_cloud_storage_sink_connector(
PROJECT_ID,
REGION,
CONNECT_CLUSTER_ID,
connector_id,
"GMK_TOPIC_ID",
"GCS_BUCKET_NAME",
"3",
"json",
"org.apache.kafka.connect.json.JsonConverter",
"false",
"org.apache.kafka.connect.storage.StringConverter",
)

out, _ = capsys.readouterr()
assert "Created Connector" in out
assert connector_id


@mock.patch(
"google.cloud.managedkafka_v1.services.managed_kafka_connect.ManagedKafkaConnectClient.create_connector"
)
def test_create_bigquery_sink_connector(
mock_method: MagicMock,
capsys: pytest.CaptureFixture[str],
) -> None:
connector_id = "BQ_SINK_CONNECTOR_ID"
operation = mock.MagicMock(spec=Operation)
connector = managedkafka_v1.types.Connector()
connector.name = connector_id
operation.result = mock.MagicMock(return_value=connector)
mock_method.return_value = operation

create_bigquery_sink_connector.create_bigquery_sink_connector(
PROJECT_ID,
REGION,
CONNECT_CLUSTER_ID,
connector_id,
"GMK_TOPIC_ID",
"3",
"org.apache.kafka.connect.storage.StringConverter",
"org.apache.kafka.connect.json.JsonConverter",
"false",
"BQ_DATASET_ID",
)

out, _ = capsys.readouterr()
assert "Created Connector" in out
assert connector_id in out
mock_method.assert_called_once()
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def create_bigquery_sink_connector(
project_id: str,
region: str,
connect_cluster_id: str,
connector_id: str,
topics: str,
tasks_max: str,
key_converter: str,
value_converter: str,
value_converter_schemas_enable: str,
default_dataset: str,
) -> None:
"""
Create a BigQuery Sink connector.

Args:
project_id: Google Cloud project ID.
region: Cloud region.
connect_cluster_id: ID of the Kafka Connect cluster.
connector_id: Name of the connector.
topics: Kafka topics to read from.
tasks_max: Maximum number of tasks.
key_converter: Key converter class.
value_converter: Value converter class.
value_converter_schemas_enable: Enable schemas for value converter.
default_dataset: BigQuery dataset ID.

Raises:
This method will raise the GoogleAPICallError exception if the operation errors or
the timeout before the operation completes is reached.
"""
# TODO(developer): Update with your config values. Here is a sample configuration:
# project_id = "my-project-id"
# region = "us-central1"
# connect_cluster_id = "my-connect-cluster"
# connector_id = "BQ_SINK_CONNECTOR_ID"
# topics = "GMK_TOPIC_ID"
# tasks_max = "3"
# key_converter = "org.apache.kafka.connect.storage.StringConverter"
# value_converter = "org.apache.kafka.connect.json.JsonConverter"
# value_converter_schemas_enable = "false"
# default_dataset = "BQ_DATASET_ID"

# [START managedkafka_create_bigquery_sink_connector]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud.managedkafka_v1.services.managed_kafka_connect import (
ManagedKafkaConnectClient,
)
from google.cloud.managedkafka_v1.types import Connector, CreateConnectorRequest

connect_client = ManagedKafkaConnectClient()
parent = connect_client.connect_cluster_path(project_id, region, connect_cluster_id)

configs = {
"name": connector_id,
"project": project_id,
"topics": topics,
"tasks.max": tasks_max,
"connector.class": "com.wepay.kafka.connect.bigquery.BigQuerySinkConnector",
"key.converter": key_converter,
"value.converter": value_converter,
"value.converter.schemas.enable": value_converter_schemas_enable,
"defaultDataset": default_dataset,
}

connector = Connector()
connector.name = connector_id
connector.configs = configs

request = CreateConnectorRequest(
parent=parent,
connector_id=connector_id,
connector=connector,
)

try:
operation = connect_client.create_connector(request=request)
print(f"Waiting for operation {operation.operation.name} to complete...")
response = operation.result()
print("Created Connector:", response)
except GoogleAPICallError as e:
print(f"The operation failed with error: {e}")
# [END managedkafka_create_bigquery_sink_connector]
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def create_cloud_storage_sink_connector(
project_id: str,
region: str,
connect_cluster_id: str,
connector_id: str,
topics: str,
gcs_bucket_name: str,
tasks_max: str,
format_output_type: str,
value_converter: str,
value_converter_schemas_enable: str,
key_converter: str,
) -> None:
"""
Create a Cloud Storage Sink connector.

Args:
project_id: Google Cloud project ID.
region: Cloud region.
connect_cluster_id: ID of the Kafka Connect cluster.
connector_id: Name of the connector.
topics: Kafka topics to read from.
gcs_bucket_name: Google Cloud Storage bucket name.
tasks_max: Maximum number of tasks.
format_output_type: Output format type.
value_converter: Value converter class.
value_converter_schemas_enable: Enable schemas for value converter.
key_converter: Key converter class.

Raises:
This method will raise the GoogleAPICallError exception if the operation errors or
the timeout before the operation completes is reached.
"""
# TODO(developer): Update with your config values. Here is a sample configuration:
# project_id = "my-project-id"
# region = "us-central1"
# connect_cluster_id = "my-connect-cluster"
# connector_id = "GCS_SINK_CONNECTOR_ID"
# topics = "GMK_TOPIC_ID"
# gcs_bucket_name = "GCS_BUCKET_NAME"
# tasks_max = "3"
# format_output_type = "json"
# value_converter = "org.apache.kafka.connect.json.JsonConverter"
# value_converter_schemas_enable = "false"
# key_converter = "org.apache.kafka.connect.storage.StringConverter"

# [START managedkafka_create_cloud_storage_sink_connector]
from google.api_core.exceptions import GoogleAPICallError
from google.cloud.managedkafka_v1.services.managed_kafka_connect import (
ManagedKafkaConnectClient,
)
from google.cloud.managedkafka_v1.types import Connector, CreateConnectorRequest

connect_client = ManagedKafkaConnectClient()
parent = connect_client.connect_cluster_path(project_id, region, connect_cluster_id)

configs = {
"connector.class": "io.aiven.kafka.connect.gcs.GcsSinkConnector",
"tasks.max": tasks_max,
"topics": topics,
"gcs.bucket.name": gcs_bucket_name,
"gcs.credentials.default": "true",
"format.output.type": format_output_type,
"name": connector_id,
"value.converter": value_converter,
"value.converter.schemas.enable": value_converter_schemas_enable,
"key.converter": key_converter,
}

connector = Connector()
connector.name = connector_id
connector.configs = configs

request = CreateConnectorRequest(
parent=parent,
connector_id=connector_id,
connector=connector,
)

try:
operation = connect_client.create_connector(request=request)
print(f"Waiting for operation {operation.operation.name} to complete...")
response = operation.result()
print("Created Connector:", response)
except GoogleAPICallError as e:
print(f"The operation failed with error: {e}")
# [END managedkafka_create_cloud_storage_sink_connector]
Loading