postgresql - PostgreSQL 复制到程序 gzip 到标准输出
问题描述
以下命令序列运行良好,并生成与 SQL 表匹配的 DataFrame:
copy_sql = "COPY mytable TO STDOUT WITH CSV HEADER"
conn = myengine.raw_connection()
cur = conn.cursor()
store = io.StringIO()
cur.copy_expert(copy_sql, store)
store.seek(0)
# this is for debugging
# it correctly outputs the CSV string from STDOUT
print(store.read())
store.seek(0)
cur.close()
# this works
df = pd.read_csv(store)
但是,我试图将COPY
命令的输出传递给gzip
,然后将gzip
输出传递给STDOUT
. 以下导致pandas.errors.EmptyDataError: No columns to parse from file
错误。
copy_sql = "COPY mytable TO PROGRAM 'gzip -f --stdout' WITH CSV HEADER"
conn = myengine.raw_connection()
cur = conn.cursor()
store = io.StringIO()
cur.copy_expert(copy_sql, store)
store.seek(0)
# this is for debugging
# it should output the compressed string,
# but actually outputs nothing
print(store.read())
store.seek(0)
cur.close()
# this doesn't work as Pandas finds nothing in `store`
df = pd.read_csv(store, compression="gzip")
由于尝试echo "hey" | gzip -f --stdout
在终端中正确地将压缩字符串输出到 STDOUT,我相信这TO PROGRAM 'gzip -f --stdout'
将等同于TO STDOUT
发送到 STDOUT 的输出将被压缩,但显然缺少某些东西。
该conn
连接通过网络连接到远程机器上的 PostgreSQL 数据库。
我真正的目标是在 CSV 输出通过网络之前对其进行压缩,然后read_csv
从压缩字符串中获取 Pandas。任何其他方式来实现这一点将不胜感激。
解决方案
推荐阅读
- mysql - MYSQL 错误:用户 'user1'@'localhost' 的访问被拒绝
- r - 如何在 API 中使用 Plumber 将多个图像上传到子目录?
- python - ResourceNotFoundError:在使用 Azure 虚拟机的 get 函数时,操作返回了无效状态“未找到”?
- php - 从模型访问设置..容器不可用
- validation - 如何在一个验证器上验证操作的多个参数
- java - Jackson 注释将地图数据转换为 JSON 值
- reactjs - 使用 React 和 Flask API 的带有斜杠的 CORS 奇怪行为
- firebase - iOS消息传递上的ReactNative Firebase错误
- azure - DeployIfnotexists azure 策略失败并出现错误
- tcl - 无法读取:变量是数组