How beiqisu10/crypto_data_engineering works
Purpose and Entry Points
This repository implements a batch crypto analytics pipeline that joins Binance BTC/USDT market data, CoinGecko prices, and Fear & Greed sentiment to evaluate sentiment-versus-return behavior. Primary operational entry points are Makefile (make setup, infrastructure, Airflow, and dbt commands), Airflow DAGs, and standalone ingestion scripts in scripts/.
Infrastructure Boundary: GCP Resources
Terraform provisions the cloud storage and warehouse layer consumed by ingestion and transformation workloads: GCS is the raw landing/data-lake boundary, while BigQuery is the modeled analytics boundary. Infrastructure definitions are concentrated in:
• terraform/main.tf
• terraform/providers.tf
• terraform/variables.tf
The runtime requires a GCP project, enabled BigQuery/GCS APIs, and service-account credentials; Airflow uses a google_cloud_default connection and dbt uses its BigQuery profile.
Orchestration Layer
Airflow runs containerized workflows and establishes ordering between independently deployable ingestion pipelines:
• airflow/docker-compose.yaml
• airflow/dags/binance_ingestion_dag.py
• airflow/dags/market_data_dag.py
binance_spark_ingestion_v1 is the source-of-truth market-data workflow. The market metadata/sentiment DAG waits on its successful completion through an ExternalTaskSensor, preventing downstream sentiment processing against absent or stale price data. DAG tasks retry transient failures twice with five-minute delays and include failure-callback hooks.
Ingestion and Raw Data Flow
Python ingestion jobs retrieve external API data and deliver it into the GCS/BigQuery landing path:
• scripts/ingest_binance.py
• scripts/ingest_coingecko.py
• scripts/ingest_sentiment.py
The intended flow is API extraction → raw CSV landing in GCS → Spark/PySpark cleaning and Parquet conversion → BigQuery external or native raw/staging tables. External dependencies are Binance, CoinGecko, and Alternative.me Fear & Greed data; their rate limits and available historical windows constrain current coverage.
Transformation and Semantic Modeling
dbt is the warehouse transformation boundary. It converts landing sources into normalized staging models, then produces an analytics-ready fact table:
• dbt_crypto/models/staging/stg_crypto__btcusdt.sql
• dbt_crypto/models/staging/stg_crypto__prices.sql
• dbt_crypto/models/staging/stg_crypto__sentiment.sql
• dbt_crypto/models/marts/fct_crypto_daily_metrics.sql
The three staging streams converge by date in fct_crypto_daily_metrics, where SQL window logic derives volatility, moving measures, and forward-return-oriented metrics for sentiment analysis.
Serving and Warehouse Optimization
The final BigQuery fact model is designed for dashboard consumption, including Looker Studio visualizations of fear/return relationships and price-versus-sentiment trends:
• dbt_crypto/models/marts/fct_crypto_daily_metrics.sql
• dbt_crypto/models/marts/fct_crypto_daily_metrics.yml
It is partitioned daily on date_day to constrain time-range scans and clustered by sentiment_label for efficient sentiment-segment queries. dbt schema metadata defines core fact-table integrity expectations, including unique and non-null daily keys.
Runtime Topology
Airflow runs in Docker via the repository-specific image and Compose environment:
• airflow/Dockerfile
• airflow/docker-compose.yaml
• pyproject.toml
main.py is the top-level Python module, while Airflow is the primary scheduled execution surface. dbt execution is intentionally run from the Airflow container so warehouse permissions and dependencies align with orchestration runtime configuration.
Main entry points: market_data_dag.py Metadata & sentiment DAG · docker-compose.yaml Airflow Compose runtime · fct_crypto_daily_metrics.sql Daily crypto metrics fact · binance_ingestion_dag.py Binance ingestion DAG
Components 17
GCP Infrastructure
- Terraform resources · Provisions GCS and BigQuery boundaries.
terraform/main.tf - GCS raw landing · Raw CSV landing boundary.
- BigQuery warehouse · Raw, staging, and analytics tables.
Orchestration Runtime
- Airflow Compose runtime
airflow/docker-compose.yaml - Airflow image
airflow/Dockerfile - Binance ingestion DAG · Source-of-truth market-data workflow.
airflow/dags/binance_ingestion_dag.py - Metadata & sentiment DAG · Waits for successful market-data completion.
airflow/dags/market_data_dag.py
Ingestion Pipeline
- Binance API
- CoinGecko API
- Fear & Greed API
- Binance extractor
scripts/ingest_binance.py - CoinGecko extractor
scripts/ingest_coingecko.py - Sentiment extractor
scripts/ingest_sentiment.py - Spark clean & Parquet · Cleans landed CSV and converts it to Parquet.
Warehouse Modeling
- dbt staging models · Normalizes BTCUSDT, prices, and sentiment streams.
dbt_crypto/models/staging/stg_crypto__btcusdt.sql - Daily crypto metrics fact · Joins streams by date and derives volatility and forward returns.
dbt_crypto/models/marts/fct_crypto_daily_metrics.sql
Other components
- Looker Studio dashboards · Analyzes fear/return and price/sentiment trends.
Connections 21
- Terraform resources → GCS raw landing: provisions
- Terraform resources → BigQuery warehouse: provisions
- Airflow image → Airflow Compose runtime: builds runtime for
- Airflow Compose runtime → Binance ingestion DAG: runs
- Airflow Compose runtime → Metadata & sentiment DAG: runs
- Metadata & sentiment DAG → Binance ingestion DAG: waits for success · ExternalTaskSensor dependency.
- Binance ingestion DAG → Binance extractor: orchestrates
- Metadata & sentiment DAG → CoinGecko extractor: orchestrates
- Metadata & sentiment DAG → Sentiment extractor: orchestrates
- Binance API → Binance extractor: market data
- CoinGecko API → CoinGecko extractor: prices
- Fear & Greed API → Sentiment extractor: sentiment
- Binance extractor → GCS raw landing: lands raw CSV
- CoinGecko extractor → GCS raw landing: lands raw CSV
- Sentiment extractor → GCS raw landing: lands raw CSV
- GCS raw landing → Spark clean & Parquet: raw CSV
- Spark clean & Parquet → BigQuery warehouse: raw/staging data
- BigQuery warehouse → dbt staging models: landing sources
- dbt staging models → Daily crypto metrics fact: three dated streams
- Daily crypto metrics fact → BigQuery warehouse: materializes partitioned fact
- Daily crypto metrics fact → Looker Studio dashboards: serves analytics