-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
salmany
wants to merge
20
commits into
GoogleCloudPlatform:main
Choose a base branch
from
salmany:mkc-connectors-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 8f2224a
Update google-cloud-managedkafka version to 0.1.12
salmany 1feb354
Update managedkafka/snippets/connect/clusters/create_connect_cluster.py
salmany 5840336
Update managedkafka/snippets/connect/clusters/create_connect_cluster.py
salmany d0df10f
Update managedkafka/snippets/connect/clusters/delete_connect_cluster.py
salmany 505759a
Update managedkafka/snippets/connect/clusters/get_connect_cluster.py
salmany d3fba2c
Update managedkafka/snippets/connect/clusters/list_connect_clusters.py
salmany 0a85a8b
Update managedkafka/snippets/connect/clusters/update_connect_cluster.py
salmany dfa07d2
Add timeouts and improve error handling.
salmany ab697bf
Addressed PR comments.
salmany 8af6e36
Add Managed Kafka Connect Connectors examples
salmany bc121bd
Fix import statements and add headers.
salmany b9a15ef
Fix sample configs and update MM2 connector.
salmany a26f7b9
Remove unwanted local test artifact.
salmany af7a628
Adds requirements.txt file for samples.
salmany 1ae987f
Remove timeout in update_connect_cluster.py
salmany 3e788a6
Fix CPU and memory values for Cluster creation.
salmany e908ecd
Fixed comment for minimum vCpu.
salmany 1ede52b
Fix lint by adding space.
salmany fac49a9
Merge branch 'main' into mkc-connectors-examples
glasnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
195 changes: 195 additions & 0 deletions
195
managedkafka/snippets/connect/connectors/connectors_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
97 changes: 97 additions & 0 deletions
97
managedkafka/snippets/connect/connectors/create_bigquery_sink_connector.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
101 changes: 101 additions & 0 deletions
101
managedkafka/snippets/connect/connectors/create_cloud_storage_sink_connector.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.