Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying data to Vertica using python

Tags:

python

vertica

I use python and vertica-python library to COPY data to Vertica DB

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()

It inserts data, but only 5 rows, although the file contains more. Could this be related to db settings or it's some client issue?

like image 342
andylens Avatar asked Oct 20 '25 10:10

andylens


2 Answers

This code works for me:

For 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()

For 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()
like image 69
Waqas Ali Avatar answered Oct 22 '25 01:10

Waqas Ali


Very likely that you have rows getting rejected. Assuming you are using 7.x, you can add:

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

You can also query this after the copy execution to see the summary of results:

SELECTGET_NUM_ACCEPTED_ROWS(),GET_NUM_REJECTED_ROWS();

like image 42
woot Avatar answered Oct 21 '25 23:10

woot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!