用户One ID Mapping系统 - 详细实现方案
1. 系统架构设计
1.1 总体架构
数据源 → 数据预处理 → 用户合并 → 属性聚合 → 属性分发 → 存储与查询
1.2 关键组件
- 数据预处理:Spark/Flink ETL
- 用户合并:分布式去重算法
- 属性聚合:规则引擎
- 存储:多模态数据库
- 查询:缓存+索引优化
2. 详细数据模型
2.1 用户主表 (user_master)
CREATE TABLE user_master (
user_id UUID PRIMARY KEY,
primary_email VARCHAR(255) UNIQUE,
primary_phone VARCHAR(50),
merge_accounts JSONB,
created_at TIMESTAMP,
updated_at TIMESTAMP
);
2.2 账号映射表 (account_mapping)
CREATE TABLE account_mapping (
user_id UUID,
account_id VARCHAR(100),
product_id VARCHAR(50),
email VARCHAR(255),
merge_strategy VARCHAR(50),
PRIMARY KEY (user_id, account_id)
);
2.3 用户属性表 (user_attributes)
CREATE TABLE user_attributes (
user_id UUID,
attribute_key VARCHAR(100),
attribute_value JSONB,
merge_rule VARCHAR(50),
source_accounts JSONB,
updated_at TIMESTAMP,
PRIMARY KEY (user_id, attribute_key)
);
3. 用户合并算法
3.1 合并策略
def merge_user_accounts(accounts):
# 基于email/phone聚类
email_clusters = cluster_by_email(accounts)
# 生成唯一user_id
for cluster in email_clusters:
user_id = generate_unique_user_id()
# 记录合并信息
user_master.insert({
'user_id': user_id,
'primary_email': cluster.primary_email,
'merge_accounts': cluster.accounts
})
# 更新账号映射
for account in cluster.accounts:
account_mapping.insert({
'user_id': user_id,
'account_id': account.id,
'email': account.email
})
4. 属性合并规则引擎
4.1 属性合并策略定义
MERGE_STRATEGIES = {
'max': lambda values: max(values),
'sum': lambda values: sum(values),
'latest': lambda values: max(values, key=lambda x: x['timestamp']),
'priority': lambda values: next(
(v for v in values if v['is_verified']),
values[0]
)
}
def merge_user_attributes(user_id, attribute_key):
# 获取所有账号的属性
account_attributes = get_account_attributes(user_id, attribute_key)
# 根据预定义规则合并
merge_rule = get_merge_rule(attribute_key)
merged_value = MERGE_STRATEGIES[merge_rule](account_attributes)
# 存储合并后属性
user_attributes.insert({
'user_id': user_id,
'attribute_key': attribute_key,
'attribute_value': merged_value,
'source_accounts': account_attributes
})
5. 属性分发机制
5.1 属性反向同步
def distribute_user_attributes(user_id):
# 获取用户统一属性
user_attrs = user_attributes.get(user_id)
# 获取关联账号
accounts = account_mapping.get_accounts(user_id)
# 属性分发
for account in accounts:
account_attributes.update({
'account_id': account.id,
'attributes': {
key: calculate_account_specific_value(user_attrs[key], account)
for key in user_attrs
}
})
6. 查询优化
6.1 缓存策略
# Redis缓存用户属性
def get_user_attributes(user_id):
cache_key = f"user_attrs:{user_id}"
# 优先读取缓存
cached_attrs = redis.get(cache_key)
if cached_attrs:
return cached_attrs
# 缓存未命中,从数据库读取
attrs = user_attributes.get(user_id)
# 设置缓存
redis.setex(cache_key, 3600, attrs)
return attrs
7. 技术选型
| 组件 | 推荐技术 | 原因 |
|---|
| 数据处理 | Apache Spark | 分布式大规模数据处理 |
| 实时同步 | Apache Flink | 流式数据处理 |
| 存储 | PostgreSQL + TimescaleDB | 时序属性支持 |
| 缓存 | Redis Cluster | 高性能读取 |
| 消息队列 | Apache Kafka | 异步解耦 |
8. 性能与扩展性考虑
-
水平扩展:
-
性能优化:
-
数据一致性:
9. 安全与合规
- 数据脱敏
- 加密存储敏感信息
- 遵守GDPR等隐私法规
10. 监控与运维
- 链路追踪
- 性能监控
- 异常告警
- 审计日志
总结与建议
- 灵活的用户合并策略
- 可配置的属性合并规则
- 高性能查询
- 可水平扩展
- 安全合规
建议分阶段实施:
- 第一阶段:基础数据模型
- 第二阶段:合并算法
- 第三阶段:性能优化
- 第四阶段:安全与合规
参考实践
- Uber的用户统一
- Facebook的社交图谱
- Google的用户画像系统