V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wuwukai007
V2EX  ›  Python

为了练习打包到 pypi,写了一个多线程版的快速读 sql 到 DataFrame(),写 sql 到 csv 以及快速迁移表的 pip 包。

  •  
  •   wuwukai007 · 2020-01-30 20:01:37 +08:00 · 2018 次点击
    这是一个创建于 1518 天前的主题,其中的信息可能已经有所发展或是发生改变。

    带有进度条显示的 多线程快读快写 sql,快速迁移表的 python 包

    项目地址:https://github.com/tosmart01/fastsql

    一、安装
    • pip install fast_sql
    二、依赖环境
    • python3.6+
    • mysql | oracle
    二、示例
    • 快读读取 sql 生成 DataFrame
    import fast_sql
    from sqlalchemy import create_engine
    sql = "select * from test where id <1000000"
    con = create_engine("oracle+cx_oracle://wuwukai:test@localhost:1521/helowin")
    res = fast_sql.read_sql(sql,con,show_progress=True,thread_num=8)
    res.head()
    

    image:

    快读示例

    • 快速读 sql 生成 csv

      sql = "select * from test where id <1000000"
      path = '/home/test.csv'
      con = create_engine("oracle+cx_oracle://wuwukai:wuwukai@localhost:1521/helowin")
      res = fast_sql.to_csv(sql,con,path_or_buf=path,
                           show_progress=True,thread_num=8,index=None)
      
      
    • 快速表迁移

      #1.在线迁移
      sql = "select * from student where id <1000000"
      con = create_engine("oracle+cx_oracle://wuwukai:test@localhost:1521/helowin")
      to_db = create_engine("mysql+pymysql://root:123456@localhost:3306/aps_2")
      res = fast_sql.to_sql(sql,from_db=con,to_db=to_db,if_exists='delete',
             			    mode='rw',to_table='stu',delete_cache=True,show_progress=True,)
      

      # 2.离线迁移,先序列化到本地,在迁移
      sql = "select * from student where id <1000000"
      con = create_engine("oracle+cx_oracle://wuwukai:test@localhost:1521/helowin")
      to_db = create_engine("mysql+pymysql://root:123456@localhost:3306/aps_2")
      # 写入本地,保存在当前工作目录
      res = fast_sql.to_sql(sql,from_db=con,
             				  mode='r',to_table='stu',show_progress=True,)
      # 入库,file_path 为保存的目录
      res = fast_sql.to_sql(sql,to_db=con,file_path='/home/test'
            				  mode='w',to_table='stu',show_progress=True,)
      
      
    三、参数说明
    其他参数兼容 pandas read_sql,to_csv
    sql: sql 语句
    con: 数据库连接 sqlalchemy 连接对象 | sqlalchemy 连接字符串 | pymysql 连接对象
    thread_num: 开启的线程数量
    encoding: 编码
    show_progress: 是否显示进度条
    from_db: 数据源库
    to_db: 目标库
    if_exists: 目标库相同数据是否删除 delete append orthe(不做处理)
    to_table: 目标库表名,默认与原始 sql 表名相同
    mode: r > 读取 sql 序列化到本地
          w > 将序列化的文件入库
          rw > 从源库读取写入到目标库
    file_path: 读取数据序列化路径
    delete_cache: 是否删除迁移过程中缓存的序列化文件
    to_columns: 指定目标库的列名,默认与原列名相同
        
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3405 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.