4. 环境搭建
在金融特征平台(Feature Store)的实战中,稳定、可复现的本地开发环境是后续所有实验的基础。本章将指导你从零搭建基于 Feast 的完整本地环境,涵盖 Python 虚拟环境配置、核心依赖安装、Docker Compose 编排以及 Feast 项目初始化。我们采用“离线开发 + 在线服务”双模式架构,确保本地调试与生产部署的一致性。
4.1 环境全景与组件依赖
Feast 本地环境由三个核心层组成:离线存储(用于特征回溯与批量计算)、在线存储(用于低延迟特征服务)以及 注册中心(管理特征定义与元数据)。下图展示了各组件的逻辑关系与数据流向。
4.2 前置条件与版本清单
在开始安装前,请确认你的开发机满足以下最低要求。下表列出了关键依赖及其推荐版本,避免因版本不兼容导致环境异常。
| 组件 | 推荐版本 | 用途说明 |
|---|---|---|
| Python | 3.9 ~ 3.11 | Feast SDK 运行环境 |
| Docker | 24.0+ | 容器化在线存储与注册中心 |
| Docker Compose | v2.20+ | 多容器编排 |
| Feast | 0.34+ | 特征平台核心库 |
| PostgreSQL | 14+ | 离线存储(可选) |
| Redis | 7+ | 在线存储(Docker 部署) |
版本兼容提示:Feast 0.34 及以上版本对 Python 3.11 支持良好。若使用 Python 3.12,部分依赖(如 dask、pyarrow)可能需手动升级。建议在虚拟环境中锁定版本。
4.3 创建 Python 虚拟环境与安装 Feast
推荐使用 venv 或 conda 创建隔离环境,避免与系统 Python 包冲突。以下是基于 venv 的完整安装步骤。
# 1. 创建项目目录并进入
mkdir feast-finance-lab && cd feast-finance-lab
# 2. 创建 Python 虚拟环境
python3.10 -m venv .venv
# 3. 激活虚拟环境(Linux/macOS)
source .venv/bin/activate
# 4. 升级 pip 并安装 Feast 核心库
pip install --upgrade pip
pip install feast==0.34.0
# 5. 安装离线/在线存储插件(按需选择)
pip install feast[postgres] # PostgreSQL 离线存储
pip install feast[redis] # Redis 在线存储
pip install feast[parquet] # Parquet 文件离线存储
# 6. 验证安装
feast --version
⚠️ 常见安装问题:若在 macOS ARM 架构下安装
grpcio 失败,请先安装 grpcio-tools 或使用 conda install grpcio。Windows 用户建议使用 WSL2 环境。
4.4 Docker Compose 部署在线与离线服务
为了模拟生产环境,我们使用 Docker Compose 启动 Redis(在线存储)和 PostgreSQL(离线存储)。在项目根目录创建 docker-compose.yml 文件。
version: "3.8"
services:
redis:
image: redis:7-alpine
container_name: feast-redis
ports:
- "6379:6379"
volumes:
- redis-data:/data
restart: unless-stopped
postgres:
image: postgres:14-alpine
container_name: feast-postgres
environment:
POSTGRES_USER: feast
POSTGRES_PASSWORD: feast_pass
POSTGRES_DB: feast_offline
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
redis-data:
postgres-data:
启动容器并验证服务状态:
# 启动所有容器(后台运行)
docker compose up -d
# 查看容器运行状态
docker compose ps
# 测试 Redis 连接
redis-cli -h localhost -p 6379 ping
# 测试 PostgreSQL 连接
psql -h localhost -U feast -d feast_offline -c "SELECT 1;"
4.5 初始化 Feast 项目与配置
使用 Feast CLI 创建项目骨架,并配置离线/在线存储后端。以下命令将在当前目录生成标准 Feast 项目结构。
# 初始化 Feast 项目(项目名:finance_feast)
feast init finance_feast
cd finance_feast
# 查看生成的文件结构
ls -la
# 输出示例:
# feature_store.yaml # 主配置文件
# example_repo.py # 特征定义示例
# .feastignore # 忽略文件列表
编辑 feature_store.yaml,配置指向本地 Docker 服务的连接信息:
project: finance_feast
registry: data/registry.db # 本地 SQLite 注册中心
provider: local
offline_store:
type: postgres
host: localhost
port: 5432
database: feast_offline
user: feast
password: feast_pass
online_store:
type: redis
connection_string: localhost:6379
entity_key_serialization_version: 2
4.6 验证环境:特征注册与检索
编写一个简单的特征定义文件 features.py,并执行注册与检索测试,确认所有组件联通。
# features.py
from datetime import timedelta
from feast import Entity, FeatureView, Field, FileSource
from feast.types import Float32, Int64
# 定义实体
customer = Entity(
name="customer_id",
description="客户唯一标识",
join_keys=["customer_id"],
)
# 定义离线数据源(使用本地 Parquet 文件)
batch_source = FileSource(
path="data/customer_features.parquet",
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
# 定义特征视图
customer_features = FeatureView(
name="customer_transaction_features",
entities=[customer],
ttl=timedelta(days=7),
schema=[
Field(name="avg_transaction_amount", dtype=Float32),
Field(name="transaction_count_7d", dtype=Int64),
],
source=batch_source,
)
# 注册特征定义到注册中心
feast apply
# 查看已注册的特征列表
feast feature-views list
# 从在线存储检索特征(需先执行 materialize)
feast materialize-incremental $(date -u -d "1 hour ago" +"%Y-%m-%dT%H:%M:%S")
# 测试在线检索
feast online-read customer_id 1001
✅ 环境验证清单:
- Feast CLI 可正常执行
feast apply且无报错。 - PostgreSQL 中可查询到
feature_tables相关元数据表。 - Redis 中可通过
keys *看到特征缓存键。 feast online-read返回正确的特征值。
4.7 环境清理与重置
开发过程中如需重置环境,按以下步骤操作:
# 1. 停止并删除 Docker 容器(保留数据卷)
docker compose down
# 2. 删除本地注册数据库
rm -f data/registry.db
# 3. 清空 Redis 数据(可选)
docker compose exec redis redis-cli FLUSHALL
# 4. 重新启动容器并初始化
docker compose up -d
feast apply