【发布时间】:2018-06-13 14:05:55
【问题描述】:
我需要从 Oracle 读取大量数据(大约 100 万和 450 列)并在 Greenplum 中进行批量加载。我正在使用以下方法:
import pandas as pd
from psycopg2 import *
from sqlalchemy import create_engine
import cx_Oracle
import sqlalchemy
import psycopg2 as pg
import io
engineor = create_engine('oracle+cx_oracle://xxxx:xxxx@xxxxx:xxxx/?service_name=xxxxx')
sql = "select * from xxxxxx"
enginegp = create_engine('xxxxx@xxxxx:xxxx/xxxx')
connection = enginegp.raw_connection()
output = io.StringIO()
for df in pd.read_sql(sql, engineor, chunksize=10000):
df.to_csv(output, header=False, index=False,mode='a')
output.seek(0)
cur = connection.cursor()
cur.copy_expert("COPY test FROM STDIN WITH CSV NULL '' ", output)
connection.commit()
cur.close()
我一直在分块读取数据:
for df in pd.read_sql(sql, engineor, chunksize=10000):
df.to_csv(output, header=False, index=False,mode='a')
是否有一种更快、更无缝的方式从 Oracle 读取大表作为数据框?这种方法可以正常工作,并且由于与 Oracle 的连接有时会超时或被 DBA 终止,而且有时会成功运行,因此这种方法似乎并不无缝。考虑到表的大小,似乎不太可靠。我需要这个作为数据框,因为我需要稍后使用复制方法将它加载到 Greenplum。
【问题讨论】:
-
Greenplum是否支持数据库链接,然后可以直接连接Oracle和Greenplum,绕过Python加载数据?
标签: python oracle postgresql greenplum bulk-load