A production-grade feature engineering pipeline for recommender systems, built on Apache Spark. It processes raw user-item interaction data through an ETL pipeline, extracts rich features (user profiles, item attributes, context signals, multi-window behavior sequences), and outputs featurized training samples in TFRecord and Parquet formats — ready for TensorFlow deep learning models.
Currently supports three datasets with a modular, extensible architecture:
| Dataset | Domain | Scale (interactions) | Label |
|---|---|---|---|
| MovieLens 1M (ML-1M) | Movie ratings | 1M | rating >= 4 → binary / multi-class / regression |
| MobileRec | App recommendation | 19.3M | rating >= 4 → binary |
| Ali_Display_Ad_Click | Display ad CTR | 26.6M | native click (0/1) |
-
Data Cleaning and Feature Extraction: Transforms raw interaction logs into structured training samples — the foundation of recommender system feature engineering. Handles deduplication, anomaly filtering, and multi-table feature joining through Spark SQL, with column-level data quality checks at every stage to eliminate "garbage in, garbage out". Extracts user profiles, item attributes, context signals, and behavior sequences with configurable time windows — covering the full spectrum of features needed for recommendation models. Supports multiple prediction targets: multi-class classification, binary classification, and regression. Supports multiple datasets: ML-1M, MobileRec, Ali_Display_Ad_Click.
-
Negative Sampling Strategies: For each positive instance, generates unobserved items as negative samples — a critical component for ranking model training. Supports uniform random, popularity-biased sampling, and hybrid strategies, preventing popular items from dominating training gradients and effectively mitigating the "Matthew effect", improving model generalization on long-tail items. (Currently implemented for ML-1M.)
-
High-Order Cross Features: Supports second-order and higher-order feature combinations to capture deeper patterns in data. All features (60 raw + 17 cross for ML-1M) are encoded through a type-safe generic
Featurizer[T]architecture — producing the standard embedding lookup schema for DeepFM, DIN, Wide&Deep, and similar models. Cross features are configurable per dataset via YAML. -
Vocabulary Management: Builds embedding vocabularies through frequency thresholding, assigning dedicated slots for each feature. Feature position maps are persisted in JSON (human-readable) and binary (with mean/std for online normalization). Supports incremental vocabulary updates across training runs and shared vocabularies across features via
field_index. -
Feature Configuration: YAML-driven feature registry. Adding or disabling features requires editing a single config file — no code changes, no recompilation. Supports classpath and external file loading. Each dataset has its own YAML config with mode-specific variants (binary/multi).
-
Multi-Format Output: Outputs training samples in TFRecord (TensorFlow Example protobuf) and Parquet (columnar storage) formats, with time-based train/val/test split for standard recommendation evaluation.
-
Data Quality Monitoring: Guards against the two silent killers of production recommender systems — training-serving skew and data drift. Automatically detects column-level metrics (null ratios, cardinality, numeric distributions) across ETL stages; tracks parse success rates and target distribution during featurization. Cross-run drift detection compares against historical baselines and alerts when volume, null ratios, or means exceed preset thresholds.
-
Pipeline Orchestration and Scheduling: Modular
Pipeline[T]abstract class orchestrated viasplitSamples→generateVocabulary→generateSample. Airflow DAG for production scheduling with automatic retries and monitoring; standalone Python runner for local development and CI. Topological sort ensures stage dependencies are honored.--dry-runmode supports execution plan preview. -
C++ Online Inference: A bit-exact C++ reimplementation of the Scala featurizer for latency-critical serving scenarios. Loads the identical vocabulary binary and executes the same MurmurHash3 with matching key concatenation — fundamentally eliminating training-serving skew in production systems. Correctness is verified by golden data diff across tens of thousands of rows.
-
AUC / GAUC Evaluation: Built-in ranking metrics for offline model evaluation. Pure Scala
RankingMetrics(no Spark dependency) implements AUC via the Mann-Whitney U statistic and GAUC with sample-weighted group averaging.SparkRankingMetricswraps it with DataFrame-based CLI support for evaluating model predictions in Parquet, CSV, or TFRecord format.
gerbil-data/
├── .devcontainer/ # DevContainer reproducible dev environment
├── assets/ # Project assets (logo, etc.)
├── bash/ # Shell scripts for running pipeline steps
│ ├── conf/ # Environment configuration
│ ├── pipeline/ # Training sample generation scripts
│ │ └── eval/ # Offline evaluation (AUC / GAUC)
│ ├── processing/ # Data preprocessing scripts
│ │ ├── clean/ # Data cleaning
│ │ ├── feature/ # Feature extraction
│ │ ├── join/ # Feature joining
│ │ ├── sampling/ # Negative sampling
│ │ ├── proto/ # Protobuf compilation
│ │ └── tools/ # Utility scripts
│ └── pipeline/ # Pipeline shell scripts
├── dag/ # Pipeline DAG (Airflow + standalone)
│ ├── ml1m_pipeline_dag.py # Airflow DAG definition
│ └── run_pipeline.py # Standalone runner (no Airflow required)
├── docs/ # Documentation
│ └── dataset/
│ ├── ml_1m/ # ML-1M dataset documentation
│ └── mobile_rec/ # MobileRec dataset documentation
├── proto/ # TensorFlow Example protobuf definitions
├── sql/ # Hive/Spark SQL scripts
├── src/
│ ├── main/
│ │ ├── java/ # Java utilities (TensorFlow Hadoop I/O)
│ │ ├── resources/ # Configuration files
│ │ │ ├── ml1m/ # ML-1M YAML configs
│ │ │ ├── mobilerec/ # MobileRec YAML configs
│ │ │ └── alictr/ # Ali_Display_Ad_Click YAML configs
│ │ └── scala/
│ │ ├── config/ # Config loading & parsing
│ │ ├── processing/ # ETL: raw data → flat intermediate tables
│ │ │ ├── clean/ # Data cleaning (ML1M/MobileRec/AliCtr)
│ │ │ ├── feature/ # Feature derivation (all datasets)
│ │ │ ├── join/ # Multi-table joining (all datasets)
│ │ │ └── sampling/ # Negative sampling
│ │ ├── featurizer/ # ML encoding: features → embedding indices
│ │ │ ├── Featurizer.scala # Generic abstract featurization framework
│ │ │ ├── RawFeature.scala # Base class for all feature types
│ │ │ ├── RawTarget.scala # Target feature base class
│ │ │ ├── CategoricalFeature.scala # Hash-based categorical feature encoder
│ │ │ ├── ContinuousFeature.scala # Identity-mapping continuous feature encoder
│ │ │ ├── CrossFeature.scala # Combinatory cross feature encoder
│ │ │ ├── ml1m/ # ML-1M feature implementations
│ │ │ ├── mobilerec/ # MobileRec feature implementations
│ │ │ └── alictr/ # AliCtr feature implementations
│ │ ├── pipeline/ # Orchestration & training sample generation
│ │ │ ├── Pipeline.scala # Abstract pipeline base class
│ │ │ ├── ML1MPipeline.scala # ML-1M pipeline driver
│ │ │ ├── MobileRecPipeline.scala # MobileRec pipeline driver
│ │ │ ├── AliCtrPipeline.scala # Ali_Display_Ad_Click pipeline driver
│ │ │ ├── serde/ # Serialization (TFRecord, Parquet, pos-map)
│ │ │ │ ├── BaseRecord.scala # Abstract writer base class
│ │ │ │ ├── TFRecord.scala # TFRecord (Example) writer
│ │ │ │ ├── ParquetRecord.scala # Parquet columnar writer + data model
│ │ │ │ └── Vocabulary.scala # Pos-map / target-map persistence
│ │ │ ├── stats/ # Online statistics
│ │ │ └── eval/ # AUC / GAUC evaluation
│ │ ├── tfrecords/ # Custom Spark SQL TFRecord data source
│ │ └── utils/ # Utility functions
│ └── test/ # Unit tests (mirroring main structure)
├── tools/ # C++ Online Inference Featurizer
│ └── cpp_featurizer/ # Bit-exact C++ reimplementation
├── Dockerfile # Docker build
├── pom.xml # Maven build configuration
└── requirements.txt # Python dependencies
flowchart LR
subgraph Raw["Raw Data"]
direction TB
R1["ratings.dat /<br/>mobilerec_final.csv /<br/>raw_sample.csv"]
end
subgraph ETL["ETL Processing"]
C["CleanSample<br/>Filter · Dedup · Validate"]
S["ItemStatFeature<br/>Item stats: avg, count, price…"]
B["UserBehaviorSequence<br/>Behavior sequences<br/>1d / 3d / 7d / 15d / 30d / all"]
P["UserProfile<br/>Parse user attributes"]
J["JoinSample<br/>Join all features"]
end
subgraph Sampling["Negative Sampling"]
N["NegativeSampler<br/>Random / Popular / Mixed"]
end
subgraph Encoding["Feature Encoding"]
F["Featurizer<br/>YAML config<br/>Categorical + Continuous"]
H["Hash → Embedding Index<br/>MurmurHash3 x64_128<br/>f_index || value as key"]
V["Vocabulary<br/>Frequency threshold<br/>Pos-map / Target-map"]
end
subgraph Train["Training Data"]
T["TFRecord<br/>Example"]
Pq["Parquet<br/>Columnar format"]
Pm["Pos-map<br/>JSON + Binary"]
end
subgraph Serve["Serving"]
Cp["C++ Featurizer<br/>Bit-exact reproduction"]
end
Raw --> C --> S & B & P --> J
J --> N & F
F --> H --> V
V --> T & Pq & Pm
Pm --> Cp
flowchart TD
subgraph Config["Configuration Layer"]
YAML["features.yaml<br/>Feature registry<br/>field_name · field_index · field_type · class_name"]
FC["FeatureConfig<br/>Case class model"]
CL["FeatureConfigLoader<br/>YAML → FeatureDef"]
end
subgraph Core["Featurizer Core"]
FE["Featurizer[T]<br/>Generic abstract framework"]
CF["CategoricalFeature[T]<br/>Hash-based embedding<br/>(raw, _index, _value)"]
COF["ContinuousFeature[T]<br/>Identity mapping"]
XF["CrossFeature[T]<br/>Combinatory enumeration"]
RT["RawTarget[T]"]
end
subgraph DS["Dataset Implementations"]
M1["ML-1M<br/>ML1MFeaturizer<br/>ML1MPipeline"]
MR["MobileRec<br/>MobileRecFeaturizer<br/>MobileRecPipeline"]
AL["Ali_Display_Ad_Click<br/>AliCtrFeaturizer<br/>AliCtrPipeline"]
end
subgraph Pipe["Pipeline"]
PL["Pipeline[T]<br/>splitSamples<br/>generateVocabulary<br/>generateSample"]
BR["BaseRecord[T]<br/>TFRecord / ParquetRecord"]
VS["Vocabulary<br/>Save / Restore"]
QT["DataQualityTracker<br/>Parse rate · Target dist"]
end
subgraph Eval["Evaluation"]
RM["RankingMetrics<br/>AUC / GAUC"]
SR["SparkRankingMetrics<br/>Spark DataFrame wrapper"]
end
YAML --> FC --> CL --> FE
FE --> CF & COF & XF & RT
FE --> DS
DS --> PL
PL --> BR & VS & QT
FE --> RM --> SR
style FE fill:#e1f5fe
style PL fill:#e1f5fe
style YAML fill:#fff3e0
Movie rating dataset with 1M interactions, 6040 users, 3706 movies. Features include user demographics (gender, age, occupation), item attributes (title, genres, release year), multi-window behavior sequences, and context signals.
ETL Pipeline (in order):
| Step | Script | Class | Description |
|---|---|---|---|
| 1 | bash/processing/clean/ML1MCleanSample.sh |
processing.clean.ML1MCleanSample |
Clean & deduplicate ratings |
| 2 | bash/processing/feature/ML1MUserMovieRateSequence.sh |
processing.feature.ML1MUserMovieRateSequence |
Build user behavior sequences |
| 3 | bash/processing/feature/ML1MMovieStatFeature.sh |
processing.feature.ML1MMovieStatFeature |
Compute movie statistics |
| 4 | bash/processing/join/ML1MJoinSample.sh |
processing.join.ML1MJoinSample |
Join all features |
| 5 | bash/pipeline/ML1MPipelineBinary.sh |
pipeline.ML1MPipeline |
Generate TFRecord / Parquet |
Large-scale app recommendation dataset with 19.3M interactions, 700K users, 10K apps. Rating-based with app store metadata (category, price, reviews, content rating).
ETL Pipeline:
| Step | Script | Class |
|---|---|---|
| 1 | bash/processing/clean/MobileRecCleanSample.sh |
processing.clean.MobileRecCleanSample |
| 2 | bash/processing/feature/MobileRecAppStatFeature.sh |
processing.feature.MobileRecAppStatFeature |
| 3 | bash/processing/feature/MobileRecUserBehaviorSequence.sh |
processing.feature.MobileRecUserBehaviorSequence |
| 4 | bash/processing/join/MobileRecJoinSample.sh |
processing.join.MobileRecJoinSample |
| 5 | bash/pipeline/MobileRecPipelineBinary.sh |
pipeline.MobileRecPipeline |
Real-world display advertising CTR dataset with 26.6M impressions, 1.14M users, 847K ad groups. Native click labels (0/1), user profiles (gender, age, shopping level), and ad hierarchy (campaign, customer, brand, category).
ETL Pipeline:
| Step | Script | Class |
|---|---|---|
| 1 | bash/processing/clean/AliCtrCleanSample.sh |
processing.clean.AliCtrCleanSample |
| 2 | bash/processing/feature/AliCtrItemStatFeature.sh |
processing.feature.AliCtrItemStatFeature |
| 3 | bash/processing/feature/AliCtrUserProfileFeature.sh |
processing.feature.AliCtrUserProfileFeature |
| 4 | bash/processing/feature/AliCtrUserBehaviorSequence.sh |
processing.feature.AliCtrUserBehaviorSequence |
| 5 | bash/processing/feature/AliCtrJoinSample.sh |
processing.feature.AliCtrJoinSample |
| 6 | bash/pipeline/AliCtrPipelineBinary.sh |
pipeline.AliCtrPipeline |
Ranking metrics are provided in the pipeline.eval package:
| Class | Description |
|---|---|
RankingMetrics |
Pure Scala AUC / GAUC computation, no Spark dependency |
SparkRankingMetrics |
Spark DataFrame wrapper with CLI entry point |
# Evaluate model predictions (Parquet with target + score columns)
bash bash/pipeline/eval/RankingMetrics.shmvn clean package -DskipTests
# ML-1M curl -O https://files.grouplens.org/datasets/movielens/ml-1m.zip && unzip ml-1m.zip # or MobileRec / Ali_Display_Ad_Click (see docs/dataset/ for instructions)
# Edit bash/conf/env.sh with your paths source bash/conf/env.sh # ML-1M bash bash/processing/clean/ML1MCleanSample.sh bash bash/processing/feature/ML1MUserMovieRateSequence.sh bash bash/processing/feature/ML1MMovieStatFeature.sh bash bash/processing/join/ML1MJoinSample.sh bash bash/pipeline/ML1MPipelineBinary.sh # MobileRec bash bash/processing/clean/MobileRecCleanSample.sh bash bash/processing/feature/MobileRecAppStatFeature.sh bash bash/processing/feature/MobileRecUserBehaviorSequence.sh bash bash/processing/join/MobileRecJoinSample.sh bash bash/pipeline/MobileRecPipelineBinary.sh # AliCtr bash bash/processing/clean/AliCtrCleanSample.sh bash bash/processing/feature/AliCtrItemStatFeature.sh bash bash/processing/feature/AliCtrUserProfileFeature.sh bash bash/processing/feature/AliCtrUserBehaviorSequence.sh bash bash/processing/feature/AliCtrJoinSample.sh bash bash/pipeline/AliCtrPipelineBinary.sh
| Category | ML-1M | MobileRec | AliCtr |
|---|---|---|---|
| User | gender, age, occupation, zip code, rating stats | user behavior stats (active days, avg rating, std) | cms_segid, gender, age_level, pvalue_level, shopping_level, occupation |
| Item | title, genres, rating count, avg rating, hot rank, release year | app package, category, price, avg rating, review count, content rating | adgroup_id, cate_id, campaign_id, customer, brand, price |
| Context | time hour, time area, week day | time hour, time area, week day | pid (position), time hour, time area, week day |
| Behavior | movie rating sequences (multi-window), genre rating sequences | app rating sequences (multi-window), category rating sequences | ad impression/click history sequence |
Each dataset supports cross features like category_xx_user_category, gender_xx_age, etc., defined in its YAML config.
Select the prediction target with --target_mode when running the pipeline:
| Mode | CLI Value | ML-1M | MobileRec / AliCtr |
|---|---|---|---|
| Binary | binary |
rating >= 3 → positive | rating >= 4 → positive / native clk |
| Multi-class | multi |
rating (1-5) as classes | app/adgroup_id as classes |
| Regression | rating |
raw rating value | N/A |
The pipeline supports two train/val/test split strategies, selectable by overriding useLeaveOneOut in the Pipeline subclass (default: false, time-ratio split).
Samples are sorted by timestamp globally, then split by ratio:
train = first 80% (by count, earliest in time)
val = next 10%
test = last 10% (latest in time)
When to use:
| Task | Reason |
|---|---|
| CTR prediction (binary) | Evaluates model on future data — mimics online serving where the model faces unseen future samples. Prevents cross-user temporal leakage (a sample from User B's future does not appear in User A's training set). |
| Rating regression | Same as CTR — pointwise prediction tasks require a strict time wall between train and test. |
| Any IID evaluation | When temporal order matters and the evaluation goal is generalization to future time windows. |
Pros/Cons:
- ✅ Strict time wall between train and test — no future data leaks into training
- ✅ Simple and deterministic
- ❌ Users with only late-interaction data may not appear in the training set
- ❌ Does not evaluate per-user ranking performance
For each user, interactions are sorted by timestamp. The last interaction becomes test, the second-to-last becomes val, and all earlier interactions become train:
User A: [t1, t2, t3, t4, t5] → train: t1-t3, val: t4, test: t5
User B: [t1, t2] → val: t1, test: t2 (no train)
User C: [t1] → test: t1 (no train, no val)
When to use:
| Task | Reason |
|---|---|
| Item recommendation (multi-class) | Evaluates top-N ranking performance per user — the standard evaluation protocol for recommender systems papers. |
| User-level generalization | Each user contributes one test interaction, enabling per-user metrics like HitRate@K, NDCG@K. |
| Cold-start simulation | Users with few interactions test the model's ability to recommend from limited history. |
Pros/Cons:
- ✅ Every user appears in the test set — enables per-user ranking evaluation
- ✅ Widely adopted in recommender system literature
- ❌ Cross-user temporal leakage — User A's test sample (timestamp t5) may be earlier than User B's training samples (timestamp t6..tn), meaning information from the "future" (User B) leaks into the training data. This is acceptable for ranking evaluation but not for CTR/regression.
- ❌ Users with 1-2 interactions have no meaningful training set
- ❌ Not suitable for pointwise tasks (CTR, regression) where temporal isolation is critical
| Time-Ratio | Leave-One-Out | |
|---|---|---|
| Temporal isolation | ✅ Strict | ❌ Cross-user leakage |
| Per-user evaluation | ❌ | ✅ Every user in test |
| CTR / Regression | ✅ Recommended | ❌ |
| Top-N Recommendation | Acceptable | ✅ Recommended |
| Citation | — | Standard in RecSys papers [1] |
The split method is selected by overriding useLeaveOneOut in your Pipeline subclass. When enabled, you must also implement parseUserId(sample) to return a unique user identifier for each sample.
// ML1MPipeline.scala override def useLeaveOneOut: Boolean = true // enable LOO override def parseUserId(sample: ML1MSample): String = sample.user_id
// Pipeline.scala (default — time-ratio split) def useLeaveOneOut: Boolean = false def parseUserId(sample: T): String
Binary protobuf records in TensorFlow Example format, optimized for TensorFlow model training.
Columnar storage format compatible with Spark and many big data tools.
pos_map.json— Human-readable structured feature position mappingpos_map.bin— Binary feature mapping with mean/std for online normalizationpos_map.txt— Field dimension summary in plain text
Features are registered in YAML (src/main/resources/{dataset}/*.yaml). Each feature entry specifies:
| Key | Description |
|---|---|
field_name |
Globally unique feature name (used as prefix for TFRecord fields) |
field_index |
Numeric index; features sharing the same field_index share an embedding vocabulary |
field_type |
1 for categorical (hash-based), 0 for continuous (identity mapping) |
class_name |
Scala class that implements the feature extraction logic |
enabled |
Whether the feature is active (true/false) |
features: - {field_name: user_id, field_index: 1, field_type: 1, class_name: UserID, enabled: true} - {field_name: user_age, field_index: 2, field_type: 1, class_name: UserAge, enabled: true} - {field_name: movie_id, field_index: 101, field_type: 1, class_name: MovieID, enabled: true} # Behavior sequences share field_index 101 (same vocabulary as movie_id) - {field_name: user_movie_rate, field_index: 101, field_type: 1, class_name: UserMovieRate, enabled: true}
Features sharing the same field_index share a single embedding vocabulary (pos-map). The position counter is unified across all features using that field_index, ensuring each unique feature value gets a unique embedding slot — even when the value appears in multiple related features.
For example, adgroup_id, user_history_ad_seq all share field_index=101 in AliCtr. The ad "12345" maps to the same embedding position whether it appears as the target ad or in the user's history sequence.
Each feature produces three TFRecord fields:
{field_name}_raw— string representation{field_name}_index— embedding position (pos-map lookup or hashed){field_name}_value— embedding weight
| Module | Description |
|---|---|
processing |
ETL pipeline: data cleaning, feature derivation, multi-table joining |
featurizer |
ML feature encoding: categorical/continuous/cross featurizers, hash/PosMap embedding |
pipeline |
Orchestration: sample generation, vocabulary management, TFRecord/Parquet output |
pipeline.eval |
Ranking metrics: AUC / GAUC computation (pure Scala + Spark wrappers) |
config |
YAML-driven feature configuration (SnakeYAML → Scala case classes) |
tfrecords |
Custom Spark SQL data source for TFRecord format |
utils |
Logging, MurmurHash3, date utilities |
dag |
Pipeline orchestration: Airflow DAG (production) + standalone Python runner (CI/dev) |
bash |
Spark-submit wrapper scripts with environment configuration |
tools |
C++ online inference featurizer + golden data generators |
- Java 8+
- Scala 2.12
- Maven 3.x
- Apache Spark 3.4.0
- protoc 3.6.0 (for protobuf compilation, optional)
cd $PROJECT_HOME python3.11 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
docker build -t gerbil-data . docker run -it --rm -v "$PWD":/workspace gerbil-data bash
- Apache Spark 3.4.0 (core, sql, mllib, hive)
- Scala 2.12.17
- Protobuf 3.6.0
- Hadoop 3.3.4
- TensorFlow Hadoop (for TFRecord I/O, embedded)
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
This project is licensed under the MIT License — see the LICENSE file for details.
