staran 0.2.3__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: staran
3
- Version: 0.2.3
3
+ Version: 0.3.0
4
4
  Summary: staran - 高性能Python工具库
5
5
  Home-page: https://github.com/starlxa/staran
6
6
  Author: StarAn
@@ -30,12 +30,15 @@ Dynamic: summary
30
30
 
31
31
  Staran是一个强大的特征工程和数据处理工具包,提供从数据到模型的完整解决方案。特别针对工银图灵平台优化,让特征工程和模型训练变得前所未有的简单。
32
32
 
33
- ## ✨ v1.2.0 新特性
33
+ ## ✨ v0.3.0 新特性
34
34
 
35
- - 🏦 **图灵平台完整集成** - 无缝集成turingPythonLib,简化95%代码
36
- - 📥 **智能数据下载** - 一键从Hive/Hadoop下载特征数据
37
- - 🔄 **批量特征管理** - 自动化特征表创建、命名和下载
38
- - 🎯 **端到端ML流程** - 从特征工程到模型训练数据的完整自动化
35
+ - �️ **模块化引擎架构** - 独立的引擎模块,支持Spark、Hive、图灵平台
36
+ - 🔧 **统一接口设计** - 所有引擎提供一致的SQL生成、执行和下载接口
37
+ - 🎯 **继承复用架构** - TuringEngine继承SparkEngine,复用SQL生成逻辑
38
+ - 📦 **清晰代码分离** - SQL生成与平台特定执行逻辑完全分离
39
+ - � **易于扩展** - 新增数据库支持只需实现BaseEngine接口
40
+ - � **独立引擎存储** - engines/文件夹专门存放所有数据库引擎
41
+ - 🔄 **向后兼容** - 保持对原有API的完全兼容
39
42
 
40
43
  ## 🚀 快速开始
41
44
 
@@ -59,10 +62,30 @@ date3 = Date(2025, 4, 15) # 输出: 2025-04-15
59
62
  new_date = date1.add_months(2) # 输出: 202506 (保持YYYYMM格式)
60
63
  ```
61
64
 
65
+ ### 引擎架构 - 多平台支持
66
+
67
+ ```python
68
+ from staran.engines import create_engine, create_turing_engine
69
+
70
+ # 1. 使用Spark引擎
71
+ spark_engine = create_engine('spark', 'analytics_db')
72
+
73
+ # 2. 使用Hive引擎
74
+ hive_engine = create_engine('hive', 'warehouse_db')
75
+
76
+ # 3. 使用图灵平台引擎 (继承Spark + turingPythonLib)
77
+ turing_engine = create_turing_engine('analytics_db')
78
+
79
+ # 统一接口 - 所有引擎都支持相同方法
80
+ sql = spark_engine.generate_aggregation_sql(schema, 2025, 7, ['sum', 'avg'])
81
+ result = turing_engine.create_table('my_table', sql, execute=True)
82
+ download = turing_engine.download_table_data('my_table', 'file:///nfsHome/data.parquet')
83
+ ```
84
+
62
85
  ### 特征工程 - SQL自动生成
63
86
 
64
87
  ```python
65
- from staran import TableSchema, FeatureGenerator, SQLManager
88
+ from staran import TableSchema, FeatureGenerator, FeatureManager
66
89
 
67
90
  # 1. 定义表结构
68
91
  schema = TableSchema('user_behavior')
@@ -72,11 +95,11 @@ schema.add_field('amount', 'decimal', aggregatable=True)
72
95
  schema.add_field('category', 'string')
73
96
  schema.set_monthly_unique(True)
74
97
 
75
- # 2. 创建SQL管理器
76
- manager = SQLManager('analytics_db')
98
+ # 2. 创建特征管理器 (基于引擎架构)
99
+ manager = FeatureManager('analytics_db', engine_type='spark')
77
100
 
78
101
  # 3. 生成特征SQL
79
- generator = FeatureGenerator(schema)
102
+ generator = FeatureGenerator(schema, manager)
80
103
  result = generator.generate_feature_by_type('aggregation', 2025, 7)
81
104
  print(result['sql']) # 自动生成的聚合特征SQL
82
105
  ```
@@ -84,40 +107,54 @@ print(result['sql']) # 自动生成的聚合特征SQL
84
107
  ### 🏦 图灵平台集成 - 一键ML流程
85
108
 
86
109
  ```python
87
- from staran.sql.turing_integration import create_turing_integration
110
+ from staran.engines import create_turing_engine
88
111
 
89
- # 1. 创建图灵平台集成实例
90
- turing = create_turing_integration("ml_analytics")
112
+ # 1. 创建图灵引擎
113
+ turing = create_turing_engine("ml_analytics")
91
114
 
92
- # 2. 一键特征工程 + 数据下载
93
- result = turing.create_and_download_features(
94
- feature_sqls=[
95
- "SELECT user_id, sum(amount) as total_amount FROM user_behavior GROUP BY user_id",
96
- "SELECT user_id, count(*) as behavior_count FROM user_behavior GROUP BY user_id"
97
- ],
98
- base_table="user_features",
99
- output_dir="file:///nfsHome/ml_features/",
100
- mode="cluster" # 使用集群模式处理大数据
115
+ # 2. 创建特征表
116
+ create_result = turing.create_table(
117
+ table_name="user_features_2025_07_raw",
118
+ select_sql="SELECT user_id, sum(amount) as total_amount FROM user_behavior GROUP BY user_id",
119
+ execute=True,
120
+ mode="cluster"
101
121
  )
102
122
 
103
- print(f"成功创建 {result['summary']['created_successfully']} 个特征表")
104
- print(f"成功下载 {result['summary']['downloaded_successfully']} 个数据集")
123
+ # 3. 下载特征数据
124
+ download_result = turing.download_table_data(
125
+ table_name="user_features_2025_07_raw",
126
+ output_path="file:///nfsHome/ml_features/user_features.parquet",
127
+ mode="cluster"
128
+ )
105
129
 
106
- # 3. 下载标签数据
107
- labels = turing.download_with_turinglib(
130
+ # 4. 批量下载查询结果
131
+ query_result = turing.download_query_result(
108
132
  sql="SELECT user_id, label FROM ml.training_labels WHERE dt='2025-07-28'",
109
- output_path="file:///nfsHome/ml_labels/",
110
- mode="cluster"
133
+ output_path="file:///nfsHome/ml_labels/labels.parquet"
111
134
  )
112
135
 
113
- # 4. 现在可以开始模型训练了!
136
+ print(f"特征表创建: {create_result['status']}")
137
+ print(f"数据下载: {download_result['status']}")
114
138
  ```
115
139
 
116
140
  ## 📖 核心功能
117
141
 
118
- ### 🏦 图灵平台集成 - 终极ML解决方案
142
+ ### �️ 引擎架构设计
119
143
 
120
- **专为工银图灵平台设计,大幅简化turingPythonLib使用:**
144
+ **模块化引擎架构,清晰分离关注点:**
145
+
146
+ ```
147
+ BaseEngine (抽象基类)
148
+ ├── SparkEngine (Spark SQL实现)
149
+ │ └── TuringEngine (继承Spark + turingPythonLib)
150
+ └── HiveEngine (Hive SQL实现)
151
+ ```
152
+
153
+ | 引擎类型 | SQL生成 | 执行方式 | 下载方式 | 适用场景 |
154
+ |---------|---------|---------|---------|---------|
155
+ | SparkEngine | Spark SQL | 本地执行器 | DataFrame保存 | 本地开发、测试 |
156
+ | HiveEngine | Hive SQL | 本地执行器 | 目录导出 | 传统Hive环境 |
157
+ | TuringEngine | Spark SQL | turingPythonLib | tp.download() | 工银图灵平台 |
121
158
 
122
159
  | 功能对比 | 原生turingPythonLib | Staran集成 |
123
160
  |---------|-------------------|------------|
@@ -278,68 +315,69 @@ tomorrow = date.add_days(1) # 202504 (智能处理)
278
315
 
279
316
  ```
280
317
  staran/
281
- ├── __init__.py # 主包入口,v1.2.0功能导出
318
+ ├── __init__.py # 主包入口,v0.3.0功能导出
319
+ ├── engines/ # 🆕 模块化引擎架构
320
+ │ ├── __init__.py # 引擎模块入口
321
+ │ ├── base.py # BaseEngine抽象基类
322
+ │ ├── spark.py # SparkEngine实现
323
+ │ ├── hive.py # HiveEngine实现
324
+ │ └── turing.py # TuringEngine (继承SparkEngine)
325
+ ├── features/ # 🆕 特征工程模块
326
+ │ ├── __init__.py # 特征模块入口
327
+ │ ├── manager.py # FeatureManager (使用引擎架构)
328
+ │ ├── schema.py # 表结构定义
329
+ │ └── generator.py # 特征生成器
330
+ ├── examples/ # 🆕 完整示例模块
331
+ │ ├── __init__.py # 示例模块入口
332
+ │ └── aum_longtail.py # AUM代发长尾模型示例
282
333
  ├── tools/
283
334
  │ ├── __init__.py # 工具模块
284
335
  │ └── date.py # Date类实现
285
- ├── sql/
286
- ├── __init__.py # SQL模块
287
- │ ├── manager.py # 🆕 SQL中央管理器 + 下载功能
288
- │ ├── turing_integration.py # 🆕 图灵平台完整集成
289
- │ ├── schema.py # 表结构定义
290
- │ ├── generator.py # 特征生成器 (已增强)
291
- │ └── engines.py # SQL引擎(Spark等)
292
- ├── example_download.py # 🆕 下载功能演示
293
- ├── example_turing_platform.py # 🆕 图灵平台使用指南
294
- ├── setup.py # 安装配置
295
- ├── README.md # 本文档 (已更新)
296
- └── DOWNLOAD_FEATURES_SUMMARY.py # 🆕 新功能详细说明
336
+ ├── setup.py # 安装配置
337
+ ├── README.md # 本文档 v0.3.0
338
+ └── quick-upload.sh # 快速部署脚本
297
339
  ```
298
340
 
299
341
  ## 🧪 快速测试
300
342
 
301
- ### 图灵平台集成测试
343
+ ### 引擎架构测试
302
344
  ```python
303
- from staran.sql.turing_integration import create_turing_integration
345
+ from staran import create_engine, create_turing_engine
304
346
 
305
- # 测试图灵平台环境
306
- turing = create_turing_integration("test_analytics")
307
- platform_info = turing.get_platform_info()
347
+ # 测试SparkEngine
348
+ spark = create_engine('spark')
349
+ print(f"Spark引擎: {spark.__class__.__name__}")
308
350
 
309
- print(f"turingPythonLib可用: {platform_info['turinglib_available']}")
310
- print(f"图灵环境检测: {platform_info['nfs_home_exists']}")
351
+ # 测试TuringEngine继承
352
+ turing = create_turing_engine("test_analytics")
353
+ print(f"Turing引擎父类: {turing.__class__.__bases__[0].__name__}")
354
+ print(f"是否为SparkEngine子类: {isinstance(turing, spark.__class__)}")
311
355
 
312
- # 测试快速下载
313
- from staran.sql.turing_integration import quick_download
314
- result = quick_download(
315
- sql="SELECT 1 as test_col",
316
- output_path="file:///nfsHome/test_data/"
317
- )
318
- print(f"快速下载测试: {result['status']}")
356
+ # 测试引擎功能
357
+ sql = turing.generate_sql("SELECT user_id, amount FROM users", {"table": "test"})
358
+ print(f"SQL生成测试: {'success' if sql else 'failed'}")
319
359
  ```
320
360
 
321
- ### 特征工程测试
361
+ ### AUM示例测试
322
362
  ```python
323
- from staran import TableSchema, FeatureGenerator, SQLManager
363
+ from staran import create_aum_example
324
364
 
325
- # 定义表结构
326
- schema = TableSchema('user_stats')
327
- schema.add_primary_key('user_id', 'string')
328
- schema.add_date_field('date', 'date')
329
- schema.add_field('amount', 'decimal', aggregatable=True)
330
- schema.set_monthly_unique(True)
365
+ # 创建示例并查看摘要
366
+ example = create_aum_example("dwegdata03000")
367
+ example.print_summary()
331
368
 
332
- # 创建管理器和生成器
333
- manager = SQLManager('analytics_db')
334
- generator = FeatureGenerator(schema)
369
+ # 快速运行(测试模式,不执行实际SQL)
370
+ print("🎯 AUM长尾模型示例已准备就绪")
371
+ print("📊 包含4张业务表的完整特征工程流程")
372
+ ```
335
373
 
336
- # 生成特征并查看摘要
337
- summary = generator.get_feature_summary()
338
- print(f"生成特征数: {summary['total']}")
374
+ ### 一键运行示例
375
+ ```python
376
+ from staran import run_aum_example
339
377
 
340
- # 生成聚合特征SQL
341
- result = generator.generate_feature_by_type('aggregation', 2025, 7)
342
- print("SQL长度:", len(result['sql']))
378
+ # 最简单的使用方式
379
+ results = run_aum_example("202507") # 指定特征月份
380
+ print(f" 处理完成: {len(results)} 个表")
343
381
  ```
344
382
 
345
383
  ### Date工具测试
@@ -364,23 +402,46 @@ print(f"完整: {date.format_full()}") # 2025-04-01
364
402
  import sys
365
403
  sys.path.append("/nfsHome/staran") # 假设已上传staran包
366
404
 
367
- # 检查环境
368
- from staran.sql.turing_integration import create_turing_integration
369
- turing = create_turing_integration("your_analytics_db")
370
- print("环境就绪!开始特征工程之旅 🚀")
405
+ # 检查新引擎架构
406
+ from staran import create_turing_engine
407
+ turing = create_turing_engine("your_analytics_db")
408
+ print(f" 引擎类型: {turing.__class__.__name__}")
409
+ print(f"✅ 继承关系: 继承自{turing.__class__.__bases__[0].__name__}")
410
+ print("🚀 环境就绪!开始特征工程之旅")
371
411
  ```
372
412
 
373
- ### 2. 完整ML流程
413
+ ### 2. 运行AUM示例
374
414
  ```python
375
- # 一键完成特征工程到模型训练数据准备
376
- result = turing.create_and_download_features(
377
- feature_sqls=your_feature_sqls,
378
- base_table="production_features",
379
- output_dir="file:///nfsHome/ml_pipeline/",
380
- mode="cluster"
415
+ # 最简单的方式 - 一行代码完成复杂特征工程
416
+ from staran import run_aum_example
417
+
418
+ results = run_aum_example(
419
+ feature_date="202507", # 特征月份
420
+ database="dwegdata03000", # 数据库名
421
+ output_path="file:///nfsHome/aum_features" # 输出路径
381
422
  )
382
423
 
383
- print(f"✅ 成功!{result['summary']['downloaded_successfully']} 个数据集已准备就绪")
424
+ print(f"✅ 成功!处理了4张表,生成了完整的特征数据集")
425
+ print("📂 数据已保存到 /nfsHome/aum_features/ 目录")
426
+ ```
427
+
428
+ ### 3. 自定义特征工程
429
+ ```python
430
+ # 如需更多控制,使用详细API
431
+ from staran import create_aum_example
432
+
433
+ example = create_aum_example("dwegdata03000")
434
+
435
+ # 查看会生成哪些特征
436
+ example.print_summary()
437
+
438
+ # 运行特征工程
439
+ results = example.run("202507")
440
+
441
+ # 查看结果
442
+ for table_type, result in results.items():
443
+ if 'table_name' in result:
444
+ print(f"{table_type}: {result['table_name']}")
384
445
  ```
385
446
 
386
447
  ## 📊 性能优势
@@ -395,10 +456,58 @@ print(f"✅ 成功!{result['summary']['downloaded_successfully']} 个数据集
395
456
  - **批量处理**: 并行下载和增量处理
396
457
  - **错误恢复**: 自动重试和断点续传
397
458
 
459
+ ## 🎯 完整示例
460
+
461
+ ### AUM代发长尾模型 - 简化API
462
+ 位置:`staran.examples` 模块
463
+
464
+ 基于真实金融业务场景的完整特征工程示例,展示了新的引擎架构优势:
465
+
466
+ ```python
467
+ # 最简单的使用方式
468
+ from staran import run_aum_example
469
+
470
+ # 一键运行完整特征工程流程
471
+ results = run_aum_example(
472
+ feature_date="202507", # 可选,默认当前月
473
+ database="dwegdata03000",
474
+ output_path="file:///nfsHome/aum_longtail"
475
+ )
476
+
477
+ print(f"✅ 特征工程完成!处理了 {len(results)} 个表")
478
+ ```
479
+
480
+ **更多控制的方式:**
481
+ ```python
482
+ from staran import create_aum_example
483
+
484
+ # 创建示例实例
485
+ example = create_aum_example("dwegdata03000")
486
+
487
+ # 查看特征摘要
488
+ example.print_summary()
489
+
490
+ # 运行特征工程
491
+ results = example.run("202507")
492
+ ```
493
+
494
+ **示例特点:**
495
+ - 🏦 **真实业务场景** - 4张银行核心业务表的完整处理
496
+ - 🔧 **智能特征配置** - A表(源表+聚合),其他表(全特征:环比5个月+同比1年)
497
+ - 📊 **多维度特征** - 客户行为、资产配置、交易统计、境外交易等
498
+ - 🚀 **简化API** - 一行代码完成复杂特征工程
499
+ - 📋 **完整文档** - 每个字段都有详细的业务含义说明
500
+
501
+ **数据表说明:**
502
+ - **A表** (`bi_hlwj_dfcw_f1_f4_wy`): 客户行为特征 → 仅生成原始拷贝+聚合特征
503
+ - **B表** (`bi_hlwj_zi_chan_avg_wy`): 资产平均余额 → 生成全部特征(聚合+环比5个月+同比1年)
504
+ - **C表** (`bi_hlwj_zi_chang_month_total_zb`): 月度资产配置 → 生成全部特征
505
+ - **D表** (`bi_hlwj_realy_month_stat_wy`): 月度实际统计 → 生成全部特征
506
+
398
507
  ## 📄 许可证
399
508
 
400
509
  MIT License
401
510
 
402
511
  ---
403
512
 
404
- **Staran v1.2.0** - 让机器学习特征工程变得前所未有的简单 🌟
513
+ **Staran v0.3.0** - 模块化引擎架构,让机器学习特征工程变得前所未有的简单 🌟