forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add provider for Apache Kafka (apache#30175)
* Add provider for Apache Kafka Pulls in a series of integrations to Kafka from airflow-provider-kafka (https://pypi.org/project/airflow-provider-kafka/) to core airflow. --------- Co-authored-by: Tamara Janina Fingerlin <[email protected]> Co-authored-by: Josh Fell <[email protected]>
- Loading branch information
1 parent
7e01c09
commit 522661b
Showing
80 changed files
with
3,621 additions
and
307 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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): | ||
"""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." |
This file contains 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]], | ||
) -> 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 |
This file contains 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.