【问题标题】:pandas dataframe to vertica table insertion faster waypandas dataframe 到 vertica table 插入更快的方式
【发布时间】:2018-06-12 17:48:33
【问题描述】:

我有这样的代码..它工作正常,但将数据加载到vertica需要太多时间。 1000 行大约需要 10 分钟。是否有任何替代/更快的方法可以在 vertica 中插入数据。

import pandas as pd
import vertica_python

conn_info = {'host': '127.0.0.1',
         'user': 'some_user',
         'password': 'some_password',
         'database': 'a_database'}

connection = vertica_python.connect(**conn_info)

df = pd.DataFrame({'User':['101','101','101','102','102','101','101','102','102','102'],'Country':['India','Japan','India','Brazil','Japan','UK','Austria','Japan','Singapore','UK']})

lists= df.values.tolist()

with connection.cursor() as cursor:
    for x in lists:
        cursor.execute("insert into test values (%s,%s)" , x)
        connection.commit()

谢谢

【问题讨论】:

    标签: python pandas vertica


    【解决方案1】:

    您应该使用cursor.copy 选项而不是cursor.execute

    例如:

    # add new import:
    import cStringIO
    ...
    # temporary buffer
    buff = cStringIO.StringIO()
    
    # convert data frame to csv type
    for row in df.values.tolist():
        buff.write('{}|{}\n'.format(*row))
    
    # now insert data
    with connection.cursor() as cursor:
        cursor.copy('COPY test (Country, "User") FROM STDIN COMMIT' , buff.getvalue())
    

    在我的测试系统上跟踪结果

    你的实现:

    $ time ./so.py
    real    0m4.175s
    user    0m0.523s
    sys 0m0.101s
    

    我的实现:

    $ time ./so.py
    real    0m0.814s
    user    0m0.530s
    sys 0m0.078s
    

    快 5 倍(4.175 秒对 0.814 秒)。

    【讨论】:

    • @sKwa..谢谢它工作得很好。我明智地插入数据用户我不知道为什么在某些用户数据未加载之后。你知道为什么吗?
    • @KumarAK,我无法回答,没有足够的细节。
    • 关于这个问题.... df.to_sql() 不适用于 Vertica?谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多