-
Notifications
You must be signed in to change notification settings - Fork 17.9k
AIP-82: Add KinesisMessageQueueProvider #73509
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
aaron-y-chen
wants to merge
3
commits into
apache:main
Choose a base branch
from
aaron-y-chen:aip-82-kinesis-common-message-queue
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.
+386
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions
47
providers/amazon/src/airflow/providers/amazon/aws/queues/kinesis.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,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 TYPE_CHECKING | ||
|
|
||
| from airflow.providers.amazon.aws.triggers.kinesis import KinesisTrigger | ||
| from airflow.providers.common.compat.sdk import AirflowOptionalProviderFeatureException | ||
|
|
||
| try: | ||
| from airflow.providers.common.messaging.providers.base_provider import BaseMessageQueueProvider | ||
| except ImportError: | ||
| raise AirflowOptionalProviderFeatureException( | ||
| "This feature requires the 'common.messaging' provider to be installed in version >= 2.0.0." | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.triggers.base import BaseEventTrigger | ||
|
|
||
|
|
||
| class KinesisMessageQueueProvider(BaseMessageQueueProvider): | ||
| """ | ||
| Configuration for Amazon Kinesis Data Streams integration with common-messaging. | ||
|
|
||
| Dispatches ``scheme="kinesis"`` to | ||
| :class:`~airflow.providers.amazon.aws.triggers.kinesis.KinesisTrigger`, which also defines | ||
| the accepted parameters. | ||
| """ | ||
|
|
||
| scheme = "kinesis" | ||
|
|
||
| def trigger_class(self) -> type[BaseEventTrigger]: | ||
| return KinesisTrigger |
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
98 changes: 98 additions & 0 deletions
98
providers/amazon/tests/system/amazon/aws/example_kinesis_message_queue.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,98 @@ | ||
| # 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. | ||
| """ | ||
| Example Dag demonstrating event-driven scheduling with Amazon Kinesis Data Streams. | ||
|
|
||
| NOTE: This file serves as an example Dag and reference for AssetWatcher configuration. | ||
| It is NOT an automated end-to-end integration test: running this file directly (e.g. via | ||
| pytest system-test harness) initiates a manual DagRun where `triggering_asset_events` | ||
| is empty. Validating the complete event-driven chain requires a running Airflow triggerer, | ||
| an active Kinesis stream, and external records producing AssetEvents. | ||
|
|
||
| Pre-requisites: | ||
| 1. An active Amazon Kinesis Data Stream must exist and be accessible by the configured AWS connection. | ||
| 2. The Airflow triggerer must be running with the ``common.messaging`` provider installed. | ||
| 3. This is an event-driven Dag triggered by an AssetWatcher; it does not produce records to itself. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import os | ||
| from datetime import datetime | ||
|
|
||
| from airflow.providers.common.messaging.triggers.msg_queue import MessageQueueTrigger | ||
| from airflow.sdk import DAG, Asset, AssetWatcher, task | ||
|
|
||
| STREAM_NAME = os.getenv("KINESIS_STREAM_NAME", "airflow-kinesis-example-stream") | ||
| AWS_CONN_ID = os.getenv("AWS_CONN_ID", "aws_default") | ||
| AWS_REGION = os.getenv("AWS_REGION", "us-east-1") | ||
|
|
||
| # [START howto_trigger_kinesis_message_queue] | ||
| trigger = MessageQueueTrigger( | ||
| scheme="kinesis", | ||
| stream_name=STREAM_NAME, | ||
| aws_conn_id=AWS_CONN_ID, | ||
| region_name=AWS_REGION, | ||
| shard_iterator_type="TRIM_HORIZON", | ||
| ) | ||
|
|
||
| kinesis_asset = Asset( | ||
| f"kinesis://{STREAM_NAME}", | ||
| watchers=[AssetWatcher(name="kinesis_stream_watcher", trigger=trigger)], | ||
| ) | ||
|
|
||
|
|
||
| @task | ||
| def process_kinesis_records(**context) -> None: | ||
| """Process and decode incoming records triggered from the Amazon Kinesis stream.""" | ||
| events = context["triggering_asset_events"].get(kinesis_asset, []) | ||
| if not events: | ||
| print( | ||
| "No triggering asset events found for this run. " | ||
| "When executed manually or via test runners without an active watcher, " | ||
| "no Kinesis records are delivered. In an event-driven environment, " | ||
| "the Airflow triggerer emits an AssetEvent containing Kinesis records." | ||
| ) | ||
| return | ||
|
|
||
| for event in events: | ||
| message_batch = event.extra.get("payload", {}).get("message_batch", []) | ||
| for record in message_batch: | ||
| raw_data = base64.b64decode(record["Data"]).decode("utf-8") | ||
| print( | ||
| f"Received record: ShardId={record['ShardId']}, " | ||
| f"SequenceNumber={record['SequenceNumber']}, " | ||
| f"Data={raw_data}" | ||
| ) | ||
|
|
||
|
|
||
| with DAG( | ||
| dag_id="example_kinesis_message_queue", | ||
| schedule=[kinesis_asset], | ||
| start_date=datetime(2025, 1, 1), | ||
| catchup=False, | ||
| tags=["example", "kinesis", "message_queue"], | ||
| ) as dag: | ||
| process_kinesis_records() | ||
| # [END howto_trigger_kinesis_message_queue] | ||
|
|
||
|
|
||
| from tests_common.test_utils.system_tests import get_test_run # noqa: E402 | ||
|
|
||
| # Needed to run the example DAG with pytest (see: contributing-docs/testing/system_tests.rst) | ||
| test_run = get_test_run(dag) | ||
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.