pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JDBC Blob helper class & streaming uploaded data into PG
3+ messages / 2 participants
[nested] [flat]

* JDBC Blob helper class & streaming uploaded data into PG
@ 2009-02-04 19:18 David Wall <d.wall@computer.org>
  2009-02-05 23:33 ` Re: JDBC Blob helper class & streaming uploaded data into PG Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David Wall @ 2009-02-04 19:18 UTC (permalink / raw)
  To: pgsql-jdbc

Does anybody have a JDBC Blob helper class so we can use the 
setBlob()/getBlob() calls in JDBC for PG 8.3?  It would be a class that 
implements the java.sql.Blob interface.

Does anybody have any code samples of how I'd go about reading a file in 
blocks, compressing the block and then streaming that block into a Blob 
(LO)?  We are starting to process very large files uploaded in a web app 
and so it's no longer possible to keep it all in memory before it's 
stored in PG.

The pseudo-code is something like:

while ( hasMoreHttpPostData )
{
  read chunk from HTTP POST
  compress chunk
  write chunk to DB
}

If possible, we'd like to use a streaming cipher (for encryption) and 
GZIP to write the compressed data and a Blob to store to the database. 

Do these APIs support writing chunks, or must we actually loop through 
each byte (which seems painfully slow).

Thanks,
David



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

* Re: JDBC Blob helper class & streaming uploaded data into PG
  2009-02-04 19:18 JDBC Blob helper class & streaming uploaded data into PG David Wall <d.wall@computer.org>
@ 2009-02-05 23:33 ` Kris Jurka <books@ejurka.com>
  2009-02-05 23:57   ` Re: JDBC Blob helper class & streaming uploaded data into PG David Wall <d.wall@computer.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Kris Jurka @ 2009-02-05 23:33 UTC (permalink / raw)
  To: David Wall <d.wall@computer.org>; +Cc: pgsql-jdbc



On Wed, 4 Feb 2009, David Wall wrote:

> Does anybody have a JDBC Blob helper class so we can use the 
> setBlob()/getBlob() calls in JDBC for PG 8.3?  It would be a class that 
> implements the java.sql.Blob interface.

You really ought to use the driver's Blob implementation.  Right now 
creating a new Blob isn't terribly straightforward.  In theory the JDBC 4 
method Connection.createBlob should be used, but that has not been 
implemented in the postgresql driver yet.

The best way to do this at the moment is to insert a row with an empty 
blob, retrieve that blob and then write data into it.

CREATE TABLE test (id int, bigdata oid);

INSERT INTO test VALUES (1, lo_creat(-1));

ResultSet rs = stmt.executeQuery("SELECT bigdata FROM test WHERE id = 1");
rs.next();
Blob blob = rs.getBlob(1);
OutputStream out = blob.setBinaryStream(1);
// from java.util.zip. to compress the data.
GZIPOutputStream gz = new GZIPOutputStream(out);
while (!done) {
 	gz.write(your data);
}
gz.close();
rs.close();

Kris Jurka



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

* Re: JDBC Blob helper class & streaming uploaded data into PG
  2009-02-04 19:18 JDBC Blob helper class & streaming uploaded data into PG David Wall <d.wall@computer.org>
  2009-02-05 23:33 ` Re: JDBC Blob helper class & streaming uploaded data into PG Kris Jurka <books@ejurka.com>
@ 2009-02-05 23:57   ` David Wall <d.wall@computer.org>
  0 siblings, 0 replies; 3+ messages in thread

From: David Wall @ 2009-02-05 23:57 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc


>
>> Does anybody have a JDBC Blob helper class so we can use the 
>> setBlob()/getBlob() calls in JDBC for PG 8.3?  It would be a class 
>> that implements the java.sql.Blob interface.
>
> You really ought to use the driver's Blob implementation.  Right now 
> creating a new Blob isn't terribly straightforward.  In theory the 
> JDBC 4 method Connection.createBlob should be used, but that has not 
> been implemented in the postgresql driver yet.
>
> The best way to do this at the moment is to insert a row with an empty 
> blob, retrieve that blob and then write data into it.
>
> CREATE TABLE test (id int, bigdata oid);
>
> INSERT INTO test VALUES (1, lo_creat(-1));
>
> ResultSet rs = stmt.executeQuery("SELECT bigdata FROM test WHERE id = 
> 1");
> rs.next();
> Blob blob = rs.getBlob(1);
> OutputStream out = blob.setBinaryStream(1);
> // from java.util.zip. to compress the data.
> GZIPOutputStream gz = new GZIPOutputStream(out);
> while (!done) {
>     gz.write(your data);
> }
> gz.close();
> rs.close();
>
> Kris Jurka

Thanks, Kris.  Interesting the create empty blob and then update is used 
since Oracle seems to require something similar, though we don't do much 
with Oracle any more, I do recall using the EMPTY_BLOB() function and 
then updating it after getting an Oracle specific Blob class.

Yeah, that's why we need a Blob class that implements streaming through 
the java.sql.Blob interface.  We have our own and use simple code like 
the following to INSERT (and it works for UPDATE, too):

            YoByteBlob ybb = new YoByteBlob(encryptedCompressedData);
            stmt.setBlob(1, ybb);

When SELECTING, we can use:
            java.sql.Blob dbBlob = rs.getBlob(1);
            YoByteBlob ybb = new YoByteBlob(dbBlob);

These work for us now, but our version only supports "all in memory" 
byte arrays and no streaming.  I could share YoByteBlob if anybody was 
interested, but we'll need to do more work to make it all work so we can 
stream the data in and out.

David






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


end of thread, other threads:[~2009-02-05 23:57 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-02-04 19:18 JDBC Blob helper class & streaming uploaded data into PG David Wall <d.wall@computer.org>
2009-02-05 23:33 ` Kris Jurka <books@ejurka.com>
2009-02-05 23:57   ` David Wall <d.wall@computer.org>

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