我在 Vertica 7.1 上的 python 中使用此代码进行'COPY LOCAL':
conn_info = {'host': '192.168.1.1', 'port': 5433, 'user': 'dbadmin', 'password': 'xxxxxx', 'database': 'db'}
connection = vertica_python.connect(**conn_info)
cur = connection.cursor()
file_name="/tmp/tmp_file"
temp_file = open(file_name,"w")
temp_file .write(records)
temp_file.close()
os.system('gzip -cvf9 %s > %s.gz'%(file_name,file_name))
qr="copy tmp_table(int_id, int_timestamp, ... ) from local '%s' GZIP delimiter ';' RECORD TERMINATOR E'\\r' NULL '\\N';"%(file_name+'.gz')
cur.execute(qr)
但我现在想在 Vertica 9.0.1 上做同样的事情,但我得到了这个错误:
Traceback (most recent call last):
File "collector_as.py", line 264, in
cur.execute(qr)
File "/usr/local/lib/python2.7/dist-packages/vertica_python/vertica/cursor.py", line 126, in execute
self.connection.process_message(self._message)
File "/usr/local/lib/python2.7/dist-packages/vertica_python/vertica/connection.py", line 232, in process_message
raise errors.MessageError("Unhandled message: {0}".format(message))
MessageError: Unhandled message:
我的vertica-python版本:
pip freeze | grep vertica --> vertica-python==0.7.3
------------------------------------
此外我尝试了来自VERTICA的新vertica-db-client (vertica-client-9.0.1-4.x86_64.tar.gz)
我的 vertica-db-client 版本:
pip freeze | grep vertica --> vertica-db-client==9.0.1.4
我得到了这个错误:
Traceback (most recent call last):
File "collector_as.py", line 265, in
cur.execute(qr)
NotSupportedError: COPY LOCAL is not supported
最佳答案
使用 vertica-python 执行 COPY 命令时,可以使用 cur.copy(...)
方法。
游标的复制方法需要两个参数
- 复制命令
- 要复制的文件
此外,您可以使用 FROM STDIN
,而不是 FROM LOCAL
。
qr="copy tmp_table(int_id, int_timestamp, ... ) from STDIN GZIP delimiter ';' RECORD TERMINATOR E'\\r' NULL '\\N';"
cur.copy(qr, file_name+'.gz')
例如,如果您使用的是 vsql,那么使用 FROM LOCAL
的语法将是正确的,但 vertica-python 基本上将给定的文件作为第二个参数并将其通过管道传输到复制命令中标准输入。
关于python - vertica-python 复制本地时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51061816/