Created
December 4, 2020 08:35
-
-
Save msumit/1bc995c4a21acb257a39c36f0279e3d6 to your computer and use it in GitHub Desktop.
Apache Airflow CustomXCom Backend for Redis
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 is an example to show how easy to extend the custom XCom backends in Apache Airflow. | |
# File: airflow/providers/redis/xcom/redis_xcom.py | |
# Author: Sumit Maheshwari | |
import json | |
import logging | |
from typing import Any | |
import uuid | |
from airflow.configuration import conf | |
from airflow.models.xcom import BaseXCom | |
from airflow.providers.redis.hooks.redis import RedisHook | |
log = logging.getLogger(__name__) | |
redis_conn_id = conf.get('xcom', 'redis_conn_id', fallback=RedisHook.default_conn_name) | |
redis_hook = RedisHook(redis_conn_id=redis_conn_id) | |
class RedisXCom(BaseXCom): | |
@classmethod | |
def delete(cls, xcom): | |
"""Delete XCom value from Redis""" | |
result = redis_hook.get_conn().delete(xcom.value) | |
log.debug("Result of deleting key to Redis %s", result) | |
@staticmethod | |
def serialize_value(value: Any): | |
"""Serialize the data as JSON, store into Redis & return the key""" | |
val = json.dumps(value).encode('UTF-8') | |
key = uuid.uuid4().hex | |
log.debug('Setting XCom key %s to Redis', key) | |
result = redis_hook.get_conn().set(key, val) | |
log.debug('Result of publishing to Redis %s', result) | |
return BaseXCom.serialize_value(key) | |
@staticmethod | |
def deserialize_value(result: "XCom") -> Any: | |
result = redis_hook.get_conn().get(result.value.decode().strip('"')) | |
return json.loads(result.decode('UTF-8')) if result is not None \ | |
else '** XCom not found in Redis **' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, how to deploy the custom xcom python code in aws mwaa?