使用python将数据复制到Vertica

时间:2015-11-14 16:32:47

标签: python vertica

我将pythonvertica-python库用于COPY数据到Vertica数据库

connection = vertica_python.connect(**conn_info)
vsql_cur = connection.cursor()

with open("/tmp/vertica-test-insert", "rb") as fs:
    vsql_cur.copy( "COPY table FROM STDIN DELIMITER ',' ", fs, buffer_size=65536)
    connection.commit()

它插入数据,但只插入5行,尽管文件包含更多。这可能与数据库设置有关,还是与客户端问题有关?

2 个答案:

答案 0 :(得分:1)

很可能你的行被拒绝了。假设您使用的是7.x,则可以添加:

[ REJECTED DATA {'path' [ ON nodename ] [, ...] | AS TABLE 'reject_table'} ]

您也可以在复制执行后查询此信息,以查看结果摘要:

SELECT GET_NUM_ACCEPTED_ROWS() , GET_NUM_REJECTED_ROWS() ;

答案 1 :(得分:1)

此代码适用于我:

对于JSON

# for json file
with open("D:/SampleCSVFile_2kb/tweets.json", "rb") as fs:
    my_file = fs.read().decode('utf-8')
    cur.copy( "COPY STG.unstruc_data FROM STDIN parser fjsonparser()", my_file)
    connection.commit()

对于CSV

# for csv file
with open("D:/SampleCSVFile_2kb/SampleCSVFile_2kb.csv", "rb") as fs:
    my_file = fs.read().decode('utf-8','ignore')
    cur.copy( "COPY STG.unstruc_data FROM STDIN PARSER FDELIMITEDPARSER (delimiter=',', header='false') ", my_file) # buffer_size=65536
    connection.commit()