Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - Import CSV file to postgresql

I want to import large csv file its around 1GB of size using copy command. But the condition is if lets see Column A1 datatype is Numeric and the data of the csv file is also of type numeric but some of the values are in alpha numeric. so its not importing . What i want is set the non-numeric value of the colum A1 to null and import the data. All the condition i want in the copy command . Sample of csv file.

<table border=true>
<tr>
<th >A1</th>
<th>A2</th>
<th>A3</th>
</tr>
<tr>
<td>90</td>
<td>91</td>
<td>92</td>
</tr><tr>
<td>A21</td>
<td>B23</td>
<td>C43</td>
</tr><tr>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>12ddwd3</td>
<td>1yhh53</td>
<td>1dfsf754</td>
</tr>
<tr>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
</table>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you cant conditionnaly COPY table from file. what you can do instead:

create table t (a1 text, a2 text, a3 text);
COPY t from file;
update t set a1 = null where a1 !~ '^d{1,}$';
alter table t alter column a1 type int using a1::int;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...