How to load a simple CSV?

Moderator: NorbertKrupa

yang
Newbie
Newbie
Posts: 11
Joined: Wed Jul 04, 2012 11:27 am

How to load a simple CSV?

Post by yang » Wed Jul 04, 2012 11:29 am

I have a file in the most common CSV format, which is where double quotes are the enclosing quote characters and are escaped using another double quote. This is the default for Excel, Python, psql’s COPY WITH CSV, and many other tools. E.g., here’s a row with two fields:

Code: Select all

“a”"b”,”c
d”
We have substantial CSV data to load, but we can’t figure out how to make Vertica’s COPY command accept simple CSVs.

We tried things like:

Code: Select all

copy test from ‘/tmp/aoeu.csv’ delimiter ‘,’ enclosed by ‘”‘;
copy test from ‘/tmp/aoeu.csv’ delimiter ‘,’ enclosed by ‘”‘ escape as ‘”‘;
Thanks in advance for any answers.

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: How to load a simple CSV?

Post by id10t » Wed Jul 04, 2012 2:20 pm

Hi yang!

For ESCAPE AS & ENCLOSED BY you can choose any ASCII value in the range E'\001' to E'\177' inclusive (any ASCII character except NULL: E'\000')...
question marks that you use are NON-ASCII and out of range.

jpcavanaugh
Intermediate
Intermediate
Posts: 149
Joined: Mon Apr 30, 2012 10:04 pm
Location: New York
Contact:

Re: How to load a simple CSV?

Post by jpcavanaugh » Wed Jul 04, 2012 5:50 pm

Can you upload/send the table definition and a sample and we can help?

yang
Newbie
Newbie
Posts: 11
Joined: Wed Jul 04, 2012 11:27 am

Re: How to load a simple CSV?

Post by yang » Wed Jul 04, 2012 7:27 pm

sKwa wrote:Hi yang!

For ESCAPE AS & ENCLOSED BY you can choose any ASCII value in the range E'\001' to E'\177' inclusive (any ASCII character except NULL: E'\000')...
question marks that you use are NON-ASCII and out of range.
Question marks? I'm not using question marks. If you're talking about the weird "smart quotes" that this forum is replacing my ordinary double quotes with, then I don't know how to disable that - I already wrapped the thing in code tags.

If you try out using regular quotes, the error I get from the second line is that I can't use the same char for both enclosing and escaping.

yang
Newbie
Newbie
Posts: 11
Joined: Wed Jul 04, 2012 11:27 am

Re: How to load a simple CSV?

Post by yang » Wed Jul 04, 2012 7:29 pm

juniorfoo wrote:Can you upload/send the table definition and a sample and we can help?
Sure, that's literally the sample table I was using in my first post. Here's a sample schema:

Code: Select all

create table test (a varchar(512), b varchar(512));

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: How to load a simple CSV?

Post by JimKnicely » Mon Jul 09, 2012 7:16 pm

Hi Yang,

Did you find a solution to your problem? If not, I can try to help.

But I'm not completely sure I understand your original sample data. What values do you expect to see in the A and B columns of the TEST table once its loaded?

I created a text file in notepad on a windows machine using your original data:

Code: Select all

“a”"b”,”c
d”
Then I transferred the file (as binary) to a Linux test server and then loaded it into the TEST table in Vertica. Like this:

Code: Select all

dbadmin=> copy test from '/usr/home/dbadmin/test.txt' delimiter ',';
 Rows Loaded
-------------
           1
(1 row)

dbadmin=> select * from test;
   a    | b
--------+----
 a"b | c
(1 row)
Is that the data you expect?

Note that if I vi the file on my Linux server, I see this:

Code: Select all

<93>a<94>"b<94>,<94>c
d<94>
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

yang
Newbie
Newbie
Posts: 11
Joined: Wed Jul 04, 2012 11:27 am

Re: How to load a simple CSV?

Post by yang » Mon Jul 09, 2012 8:15 pm

In short I expect the same result as if I loaded it into PostgreSQL - that the first cell contains (in C escaped string literal syntax) "a\"b", and that the second cell contains "c\nd".

Also be careful with the "smart" double quotes - if you're just copying from this forum then you need to replace those characters with normal double quotes.

Post Reply

Return to “Vertica Data Load”