pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
COPY in Java?
5+ messages / 4 participants
[nested] [flat]

* COPY in Java?
@ 2016-07-18 20:34  John R Pierce <pierce@hogranch.com>
  0 siblings, 1 reply; 5+ messages in thread

From: John R Pierce @ 2016-07-18 20:34 UTC (permalink / raw)
  To: pgsql-jdbc

I'm looking at the JDBC documentation at 
https://jdbc.postgresql.org/documentation/head/index.html  and not 
seeing any mention of COPY and any semantics for using COPY FROM STDIN 
or COPY TO STDOUT...  google has a link to a 404 page with some sort of 
CopyManager

we may need to develop a specialized sort of sql loader to replace an 
existing Oracle tool, we need to intercept bad rows and put them in an 
exceptions file, while bulk loading the good data.   I'm thinking we 
batch N rows into COPY, and if a row faults, put it in the exception 
pool then retry the batch without it.   the source data needs to be 
massaged before it can be fed to copy, so we can't use COPY FROM 
filename ...



-- 
john r pierce, recycling bits in santa cruz



-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: COPY in Java?
@ 2016-07-18 21:16  Thomas Kellerer <spam_eater@gmx.net>
  parent: John R Pierce <pierce@hogranch.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Thomas Kellerer @ 2016-07-18 21:16 UTC (permalink / raw)
  To: pgsql-jdbc

John R Pierce schrieb am 18.07.2016 um 22:34:
> I'm looking at the JDBC documentation at
> https://jdbc.postgresql.org/documentation/head/index.html  and not
> seeing any mention of COPY and any semantics for using COPY FROM
> STDIN or COPY TO STDOUT...  google has a link to a 404 page with some
> sort of CopyManager

https://jdbc.postgresql.org/documentation/publicapi/index.html

indeed returns a 404

> we may need to develop a specialized sort of sql loader to replace an
> existing Oracle tool, we need to intercept bad rows and put them in
> an exceptions file, while bulk loading the good data.   I'm thinking
> we batch N rows into COPY, and if a row faults, put it in the
> exception pool then retry the batch without it.   the source data
> needs to be massaged before it can be fed to copy, so we can't use
> COPY FROM filename ...

The CopyManager is actually quite easy to use:

   org.postgresql.core.BaseConnection con = (BaseConnection)DriverManager.getConnection(....);
   CopyManager mgr = new CopyManager(con);

   Reader in = new BuffereReader(new FileReader("..."));
   mgr.copyIn("copy target_table from stdin with (...)", in);

But I don't think it's suitable for what you need to do because:

* The input file needs to be formatted so that COPY can handle it.
* there is no way you can catch the bad rows with that.

I assume the first problem could be solved by implementing your own Reader that "massages" each line while it reads the source file.

But I don't think there is an efficient way to "catch" the bad rows through the Copy API.


But you might be interested in http://pgloader.io/ which is a tool similar to SQL*Loader it supports more formats then COPY does.
I have not worked with it however

Thomas





-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: COPY in Java?
@ 2016-07-19 00:08  Sam Gendler <sgendler@ideasculptor.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 0 replies; 5+ messages in thread

From: Sam Gendler @ 2016-07-19 00:08 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

On Mon, Jul 18, 2016 at 2:16 PM, Thomas Kellerer <spam_eater@gmx.net> wrote:

>
>
> I assume the first problem could be solved by implementing your own Reader
> that "massages" each line while it reads the source file.
>
> But I don't think there is an efficient way to "catch" the bad rows
> through the Copy API.
>

I've done exactly this.  A long time ago, but so long as you don't mind
redoing the entire COPY without the bad rows, it works just fine. If you
get lots of failures in any given batch, you'll be better off doing
individual inserts in large transactions and catching the failures there,
or writing a more sophisticated reader which catches the bad rows and
prevents them from even being attempted.



>
> But you might be interested in http://pgloader.io/ which is a tool
> similar to SQL*Loader it supports more formats then COPY does.
> I have not worked with it however


Is Kettle still around (java-based ETL tool)?  It always had its
difficulties, in my experience, but for basic bulk inserts with redirection
of bad rows to a log, it works very well.  Not the fastest to execute
relative to implementing a custom COPY reader, but if you aren't already
dealing with overloaded infrastructure and don't need to absolutely
maximize efficiency, it will get you up and running really quickly (once
you are familiar with the tool and over the learning curve, of course) and
isn't database-specific, so much more portable. I found it really easy to
write Java/Groovy/jython code which could instantiate the kettle tools and
run them in the context of other tools/scripts, making it quite modular.
It was all a long time ago or I'd provide more detail.  Sorry. And none of
that should discredit the pgloader.io suggestion.  I just have no
experience of it.

--sam

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: COPY in Java?
@ 2016-07-19 02:11  John R Pierce <pierce@hogranch.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 1 reply; 5+ messages in thread

From: John R Pierce @ 2016-07-19 02:11 UTC (permalink / raw)
  To: pgsql-jdbc

On 7/18/2016 2:16 PM, Thomas Kellerer wrote:
> John R Pierce schrieb am 18.07.2016 um 22:34:
>> I'm looking at the JDBC documentation at
>> https://jdbc.postgresql.org/documentation/head/index.html and not
>> seeing any mention of COPY and any semantics for using COPY FROM
>> STDIN or COPY TO STDOUT...  google has a link to a 404 page with some
>> sort of CopyManager
>
> https://jdbc.postgresql.org/documentation/publicapi/index.html
>
> indeed returns a 404 

so where is the documentation on CopyManager ?


-- 
john r pierce, recycling bits in santa cruz

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: COPY in Java?
@ 2016-07-19 07:02  Radoslav Petrov <rado@edno.moe>
  parent: John R Pierce <pierce@hogranch.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Radoslav Petrov @ 2016-07-19 07:02 UTC (permalink / raw)
  To: pgsql-jdbc



On 19.07.2016 05:11, John R Pierce wrote:
> On 7/18/2016 2:16 PM, Thomas Kellerer wrote:
>> John R Pierce schrieb am 18.07.2016 um 22:34:
>>> I'm looking at the JDBC documentation at
>>> https://jdbc.postgresql.org/documentation/head/index.html and not
>>> seeing any mention of COPY and any semantics for using COPY FROM
>>> STDIN or COPY TO STDOUT...  google has a link to a 404 page with some
>>> sort of CopyManager
>>
>> https://jdbc.postgresql.org/documentation/publicapi/index.html
>>
>> indeed returns a 404 
>
> so where is the documentation on CopyManager ?
>
I'm also getting HTTP 404 when I try to access 
https://jdbc.postgresql.org/documentation/publicapi/index.html.
The cited URL should contain the JavaDocs. You can get the archive from 
the Maven Central repository. This is the link for the latest version: 
http://central.maven.org/maven2/org/postgresql/postgresql/9.4.1209/postgresql-9.4.1209-javadoc.jar
Directory browsing is enabled so if this is not the version you are 
using, you can browse to the directory with the appropriate version.

-- 
Поздрави,       | Best regards,
Радослав Петров | Radoslav Petrov



-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2016-07-19 07:02 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-07-18 20:34 COPY in Java? John R Pierce <pierce@hogranch.com>
2016-07-18 21:16 ` Thomas Kellerer <spam_eater@gmx.net>
2016-07-19 00:08   ` Sam Gendler <sgendler@ideasculptor.com>
2016-07-19 02:11   ` John R Pierce <pierce@hogranch.com>
2016-07-19 07:02     ` Radoslav Petrov <rado@edno.moe>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox