|
| 1 | +import os |
| 2 | +from airflow.decorators import task |
| 3 | +from airflow.models.dag import DAG |
| 4 | +from airflow.operators.bash import BashOperator |
| 5 | +from feast import RepoConfig, FeatureStore |
| 6 | +from feast.infra.offline_stores.contrib.spark_offline_store.spark import ( |
| 7 | + SparkOfflineStoreConfig, |
| 8 | +) |
| 9 | +from feast.repo_config import RegistryConfig |
| 10 | +from feast.infra.online_stores.dynamodb import DynamoDBOnlineStoreConfig |
| 11 | +import pendulum |
| 12 | + |
| 13 | +with DAG( |
| 14 | + dag_id="feature_dag", |
| 15 | + start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), |
| 16 | + description="A dbt + Feast DAG", |
| 17 | + schedule="@daily", |
| 18 | + catchup=False, |
| 19 | + tags=["feast"], |
| 20 | +) as dag: |
| 21 | + dbt_test = BashOperator( |
| 22 | + task_id="dbt_test", |
| 23 | + bash_command=""" |
| 24 | + cd ${AIRFLOW_HOME}; dbt test --models "aggregate_transaction_features" |
| 25 | + """, |
| 26 | + dag=dag, |
| 27 | + ) |
| 28 | + |
| 29 | + # Generate new transformed feature values |
| 30 | + dbt_run = BashOperator( |
| 31 | + task_id="dbt_run", |
| 32 | + bash_command=""" |
| 33 | + cd ${AIRFLOW_HOME}; dbt run --models "aggregate_transaction_features" |
| 34 | + """, |
| 35 | + dag=dag, |
| 36 | + ) |
| 37 | + |
| 38 | + # Use Feast to make these feature values available in a low latency store |
| 39 | + @task() |
| 40 | + def materialize(data_interval_start=None, data_interval_end=None): |
| 41 | + repo_config = RepoConfig( |
| 42 | + registry=RegistryConfig( |
| 43 | + registry_type="sql", |
| 44 | + path="postgresql://postgres:mysecretpassword@[YOUR-RDS-ENDPOINT:PORT]/feast", |
| 45 | + ), |
| 46 | + project="feast_demo", |
| 47 | + provider="local", |
| 48 | + offline_store=SparkOfflineStoreConfig( |
| 49 | + spark_conf={ |
| 50 | + "spark.ui.enabled": "false", |
| 51 | + "spark.eventLog.enabled": "false", |
| 52 | + "spark.sql.catalogImplementation": "hive", |
| 53 | + "spark.sql.parser.quotedRegexColumnNames": "true", |
| 54 | + "spark.sql.session.timeZone": "UTC", |
| 55 | + } |
| 56 | + ), |
| 57 | + online_store=DynamoDBOnlineStoreConfig(region="us-west-1"), |
| 58 | + entity_key_serialization_version=2, |
| 59 | + ) |
| 60 | + # Needed for Mac OS users because of a seg fault in requests for standalone Airflow (not needed in prod) |
| 61 | + os.environ["NO_PROXY"] = "*" |
| 62 | + os.environ["SPARK_LOCAL_IP"] = "127.0.0.1" |
| 63 | + store = FeatureStore(config=repo_config) |
| 64 | + # Add 1 hr overlap to account for late data |
| 65 | + # Note: normally, you'll probably have different feature views with different freshness requirements, instead |
| 66 | + # of materializing all feature views every day. |
| 67 | + store.materialize(data_interval_start.subtract(hours=1), data_interval_end) |
| 68 | + |
| 69 | + # Setup DAG |
| 70 | + dbt_test >> dbt_run >> materialize() |
0 commit comments