【发布时间】:2020-02-07 19:04:47
【问题描述】:
我正在尝试对我的查询结果进行 GZIP 压缩并将其写入 Airflow 中的某个位置。但是我得到了
的错误TypeError: memoryview: a bytes-like object is required, not 'str'
每当我运行我的代码时。
查看我代码中的 fp 变量:
def create_tunnel_postgres():
try:
tunnel = SSHTunnelForwarder((ssh_host, 22),
ssh_username=ssh_username,
ssh_private_key=pkf,
remote_bind_address=(psql_host,
5432))
# local_bind_address=('localhost',6543) # could be any available port
# Start the tunnel
tunnel.start()
except:
print 'connection'
else:
conn = psycopg2.connect(database='my_db', user='user',
password='my_pwd',
host=tunnel.local_bind_host,
port=tunnel.local_bind_port)
cur = conn.cursor()
cur.execute("""
select * from pricing.public.seller_tiers ;
""")
result = cur.fetchall()
# Getting Field Header names
column_names = [i[0] for i in cur.description]
fp = gzip.open(path, 'wb')
myFile = csv.writer(fp, delimiter=',')
myFile.writerow(column_names)
myFile.writerows(result)
fp.close()
conn.close
tunnel.stop
有什么想法或建议吗?我是 python/airflow 的新手,所以任何事情都会有所帮助。
【问题讨论】:
标签: python postgresql csv gzip airflow