-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add provider for Apache Kafka #30175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
98d06d8
Add provider for Apache Kafka
dylanbstorey 4dc71f1
Draft PR for Docs and additional unit tests (#2)
TJaniF 979b43b
linting from tamara's massive update
dylanbstorey abcf4b6
squashing
dylanbstorey 1fce5a3
regenerate hashes
dylanbstorey 46d5f60
squashing
dylanbstorey 7644298
Merge branch 'main' of github.com:dylanbstorey/airflow into main
dylanbstorey 6c79e13
Update airflow/providers/apache/kafka/sensors/kafka.py
dylanbstorey 64f6ca5
Add provider for Apache Kafka
dylanbstorey 72a5ac8
Draft PR for Docs and additional unit tests (#2)
TJaniF 7cabfe7
linting from tamara's massive update
dylanbstorey ac62b94
squashing
dylanbstorey beb8fb8
squashing
dylanbstorey 24823f7
squashing
dylanbstorey 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
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -78,6 +78,7 @@ | |
| "gcp", | ||
| "gmp", | ||
| "google", | ||
| "kafka", | ||
| "protocol", | ||
| "service", | ||
| "software", | ||
|
|
||
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,25 @@ | ||
| .. Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you 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. | ||
| Changelog | ||
| --------- | ||
|
|
||
| 1.0.0 | ||
| ..... | ||
|
|
||
| Initial version of the provider. |
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,17 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. |
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,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. |
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,79 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from confluent_kafka.admin import AdminClient | ||
|
|
||
| from airflow.compat.functools import cached_property | ||
| from airflow.hooks.base import BaseHook | ||
|
|
||
|
|
||
| class KafkaBaseHook(BaseHook): | ||
| """ | ||
| A base hook for interacting with Apache Kafka | ||
|
|
||
| :param kafka_config_id: The connection object to use, defaults to "kafka_default" | ||
| """ | ||
|
|
||
| conn_name_attr = "kafka_config_id" | ||
| default_conn_name = "kafka_default" | ||
| conn_type = "kafka" | ||
| hook_name = "Apache Kafka" | ||
|
|
||
| def __init__(self, kafka_config_id=default_conn_name, *args, **kwargs): | ||
dylanbstorey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Initialize our Base""" | ||
| super().__init__() | ||
| self.kafka_config_id = kafka_config_id | ||
| self.get_conn | ||
|
|
||
| @staticmethod | ||
| def get_ui_field_behaviour() -> dict[str, Any]: | ||
| """Returns custom field behaviour""" | ||
| return { | ||
| "hidden_fields": ["schema", "login", "password", "port", "host"], | ||
| "relabeling": {"extra": "Config Dict"}, | ||
| "placeholders": { | ||
| "extra": '{"bootstrap.servers": "localhost:9092"}', | ||
| }, | ||
| } | ||
|
|
||
| def _get_client(self, config): | ||
| raise NotImplementedError | ||
|
|
||
| @cached_property | ||
| def get_conn(self) -> Any: | ||
| """get the configuration object""" | ||
| config = self.get_connection(self.kafka_config_id).extra_dejson | ||
|
|
||
| if not (config.get("bootstrap.servers", None)): | ||
| raise ValueError("config['bootstrap.servers'] must be provided.") | ||
|
|
||
| return self._get_client(config) | ||
|
|
||
| def test_connection(self) -> tuple[bool, str]: | ||
| """Test Connectivity from the UI""" | ||
| try: | ||
| config = self.get_connection(self.kafka_config_id).extra_dejson | ||
| t = AdminClient(config, timeout=10).list_topics() | ||
| if t: | ||
| return True, "Connection successful." | ||
| except Exception as e: | ||
| False, str(e) | ||
|
|
||
| return False, "Failed to establish connection." | ||
dylanbstorey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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,63 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 __future__ import annotations | ||
|
|
||
| from typing import Any, Sequence | ||
|
|
||
| from confluent_kafka import KafkaException | ||
| from confluent_kafka.admin import AdminClient, NewTopic | ||
|
|
||
| from airflow.providers.apache.kafka.hooks.base import KafkaBaseHook | ||
|
|
||
|
|
||
| class KafkaAdminClientHook(KafkaBaseHook): | ||
| """ | ||
| A hook for interacting with the Kafka Cluster | ||
|
|
||
| :param kafka_config_id: The connection object to use, defaults to "kafka_default" | ||
| """ | ||
|
|
||
| def __init__(self, kafka_config_id=KafkaBaseHook.default_conn_name) -> None: | ||
| super().__init__(kafka_config_id=kafka_config_id) | ||
|
|
||
| def _get_client(self, config) -> AdminClient: | ||
| return AdminClient(config) | ||
|
|
||
| def create_topic( | ||
| self, | ||
| topics: Sequence[Sequence[Any]], | ||
dylanbstorey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> None: | ||
| """creates a topic | ||
|
|
||
| :param topics: a list of topics to create including the number of partitions for the topic | ||
| and the replication factor. Format: [ ("topic_name", number of partitions, replication factor)] | ||
| """ | ||
| admin_client = self.get_conn | ||
|
|
||
| new_topics = [NewTopic(t[0], num_partitions=t[1], replication_factor=t[2]) for t in topics] | ||
|
|
||
| futures = admin_client.create_topics(new_topics) | ||
|
|
||
| for t, f in futures.items(): | ||
| try: | ||
| f.result() | ||
| self.log.info("The topic %s has been created.", t) | ||
| except KafkaException as e: | ||
| if e.args[0].name == "TOPIC_ALREADY_EXISTS": | ||
| self.log.warning("The topic %s already exists.", t) | ||
| else: | ||
| raise | ||
dylanbstorey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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,47 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 __future__ import annotations | ||
|
|
||
| from typing import Sequence | ||
|
|
||
| from confluent_kafka import Consumer | ||
|
|
||
| from airflow.providers.apache.kafka.hooks.base import KafkaBaseHook | ||
|
|
||
|
|
||
| class KafkaConsumerHook(KafkaBaseHook): | ||
| """ | ||
| A hook for creating a Kafka Consumer | ||
|
|
||
| :param kafka_config_id: The connection object to use, defaults to "kafka_default" | ||
| :param topics: A list of topics to subscribe to. | ||
| """ | ||
|
|
||
| def __init__(self, topics: Sequence[str], kafka_config_id=KafkaBaseHook.default_conn_name) -> None: | ||
|
|
||
| super().__init__(kafka_config_id=kafka_config_id) | ||
| self.topics = topics | ||
|
|
||
| def _get_client(self, config) -> Consumer: | ||
| return Consumer(config) | ||
|
|
||
| def get_consumer(self) -> Consumer: | ||
| """Returns a Consumer that has been subscribed to topics.""" | ||
| consumer = self.get_conn | ||
| consumer.subscribe(self.topics) | ||
|
|
||
| return consumer |
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.