Просмотр исходного кода

添加表结构查看工具 desc_table.py

- 支持查看 ODPS 表的字段、类型、注释、分区信息
- 输出保存到 tables/project/table.txt,便于追踪表结构变化
- 更新 README 添加脚本说明

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
yangxiaohui 2 месяцев назад
Родитель
Сommit
53b88393a3
3 измененных файлов с 189 добавлено и 0 удалено
  1. 7 0
      README.md
  2. 83 0
      desc_table.py
  3. 99 0
      tables/loghubods/opengid_base_data.txt

+ 7 - 0
README.md

@@ -2,6 +2,13 @@
 
 从阿里云 ODPS (MaxCompute) 执行 SQL 并导出结果到 CSV。
 
+## 脚本说明
+
+| 脚本 | 功能 |
+|------|------|
+| `run_sql.py` | 执行 SQL 文件,导出结果 |
+| `desc_table.py` | 查看表结构 |
+
 ## 安装依赖
 
 ```bash

+ 83 - 0
desc_table.py

@@ -0,0 +1,83 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""
+查看 ODPS 表结构
+
+使用示例:
+    python desc_table.py loghubods.opengid_base_data
+    python desc_table.py videoods.wx_user_wechar_detail
+"""
+import argparse
+from pathlib import Path
+
+from lib.odps_module import ODPSClient
+
+PROJECT_ROOT = Path(__file__).parent
+
+
+def desc_table(table_name: str):
+    """查看表结构,返回格式化的字符串"""
+    # 解析 project.table 格式
+    if '.' in table_name:
+        project, table = table_name.split('.', 1)
+    else:
+        project = 'loghubods'
+        table = table_name
+
+    client = ODPSClient(project=project)
+    t = client.odps.get_table(table)
+
+    lines = []
+    lines.append(f"表名: {project}.{table}")
+    lines.append(f"注释: {t.comment or '(无)'}")
+    lines.append(f"创建时间: {t.creation_time}")
+    lines.append(f"最后修改: {t.last_data_modified_time}")
+    lines.append("")
+    lines.append("=" * 60)
+    lines.append(f"{'字段名':<30} {'类型':<15} {'注释'}")
+    lines.append("=" * 60)
+
+    for col in t.table_schema.columns:
+        comment = col.comment or ''
+        lines.append(f"{col.name:<30} {col.type.name:<15} {comment}")
+
+    if t.table_schema.partitions:
+        lines.append("")
+        lines.append("分区字段:")
+        lines.append("-" * 60)
+        for p in t.table_schema.partitions:
+            comment = p.comment or ''
+            lines.append(f"{p.name:<30} {p.type.name:<15} {comment}")
+
+    return "\n".join(lines), project, table
+
+
+def save_and_print(table_name: str):
+    """查看表结构并保存到文件"""
+    content, project, table = desc_table(table_name)
+
+    # 输出目录: tables/project/table.txt
+    output_dir = PROJECT_ROOT / "tables" / project
+    output_dir.mkdir(parents=True, exist_ok=True)
+    output_file = output_dir / f"{table}.txt"
+
+    # 保存文件
+    with open(output_file, 'w', encoding='utf-8') as f:
+        f.write(content)
+
+    # 打印到终端
+    print(content)
+    print()
+    print(f"已保存到: {output_file}")
+
+
+def main():
+    parser = argparse.ArgumentParser(description='查看 ODPS 表结构')
+    parser.add_argument('table', type=str, help='表名,如 loghubods.table_name')
+    args = parser.parse_args()
+
+    save_and_print(args.table)
+
+
+if __name__ == "__main__":
+    main()

+ 99 - 0
tables/loghubods/opengid_base_data.txt

@@ -0,0 +1,99 @@
+表名: loghubods.opengid_base_data
+注释: (无)
+创建时间: 2025-10-28 16:34:36
+最后修改: 2026-01-05 05:09:17
+
+============================================================
+字段名                            类型              注释
+============================================================
+apptype                        string          应用类型
+uid                            string          登录用户ID
+mid                            string          设备编码
+opengid_cnt                    bigint          opengid数量
+rootsourceid                   string          根来源ID
+channel                        string          渠道
+群类型                            string          群类型
+opengid                        string          opengid标识
+群历史人数                          bigint          群历史人数
+群当日活跃人数                        bigint          群当日活跃人数
+群当日活跃人数排名                      bigint          群当日活跃人数排名
+sencetype                      string          场景类型
+hotsencetype                   string          热门场景类型
+sessionid                      string          会话ID
+subsessionid                   string          子会话ID
+click_province                 string          点击行为所在省份
+click_city                     string          点击行为所在城市
+行为                             string          行为主题
+shareid                        string          分享ID
+点击时间                           datetime        点击行为发生时间
+分享者mid                         string          分享者设备编码
+分享者uid                         string          分享者用户ID
+分享日期                           string          分享日期(建议格式yyyy-MM-dd)
+分享时间                           string          分享时间(建议格式HH:mm:ss)
+share_province                 string          分享行为所在省份
+share_city                     string          分享行为所在城市
+videoid                        string          视频ID
+merge一级品类                      string          合并后的一级品类
+merge二级品类                      string          合并后的二级品类
+title                          string          标题内容
+recommend_status               bigint          推荐状态标识(1/-6为推荐,其他为非推荐)
+推荐状态                           string          推荐状态描述(推荐/非推荐)
+usersharedepth                 string          
+pagesource                     string          
+页面                             string          
+层级                             bigint          
+再分享shareid                     string          
+再分享videoid                     string          
+是否原视频                          string          
+再分享pagesource                  string          
+再分享页面                          string          
+再分享title                       string          
+再分享merge一级品类                   string          
+再分享merge二级品类                   string          
+再分享群聊有效分享                      bigint          
+再分享单聊有效分享                      bigint          
+再分享群聊回流uv                      bigint          
+再分享单聊回流uv                      bigint          
+总分享次数                          bigint          
+单聊人数                           bigint          
+分享到单聊分享数                       bigint          
+分享到单聊人数                        bigint          
+分享到群聊分享数                       bigint          
+分享到群聊个数                        bigint          
+广告人群                           string          
+再分享点击id                        string          
+再分享点击场景                        string          
+是否分享群                          bigint          
+是否分享人                          bigint          
+合作方名                           string          
+公众号名                           string          
+分享目的地                          string          
+原始渠道前缀                         string          
+总分享次数_当日                       bigint          
+单聊人数_当日                        bigint          
+分享到单聊分享数_当日                    bigint          
+分享到单聊人数_当日                     bigint          
+分享到群聊分享数_当日                    bigint          
+分享到群聊个数_当日                     bigint          
+分享标题id                         string          
+分享标题                           string          
+分享封面                           string          
+账号名                            string          
+阅读量                            bigint          
+点赞量                            bigint          
+发布时间                           string          
+文章标题                           string          
+文章描述                           string          
+粉丝数                            bigint          
+是否进入推荐                         string          
+wx_sn                          string          
+contenturl                     string          
+新增粉丝数                          bigint          
+t0裂变人数                         bigint          
+t0分发裂变人数                       bigint          
+t0头部裂变人数                       bigint          
+dt                             string          分区
+
+分区字段:
+------------------------------------------------------------
+dt                             string          分区