how to insert a huge file to BLOB (Oracle) without loading the complete file to memory?

丶灬走出姿态 提交于 2019-12-10 10:12:55

问题


We have a large file stored on our temporary file system, what I would be needing is to read that huge file (probably couple or few gigs) using BufferedInputStream as below

InputStream is = is = new BufferedInputStream(new FileInputStream(file));

this will effectively decrease the time to read the file and wish to save the file to BLOB in DB (Oracle).

Here is the real problem:

I do not want the file to be completely read to my memory as Iam landing into Out of memory and want to read the chunks and push the file content as byte array to my DB BLOB column.

Basically, My idea was to kill the over head of loading the complete file to memory and to achieve saving the file to BLOB (May be appending to existing BLOB content or so)

Is there any ways to get this achieved ?

PS: We use Hibernates to save BLOB files


回答1:


For the ones out there...

Here is the process to get it done:

stmt.execute ("INSERT INTO my_blob_table VALUES ('row1', empty_blob())");
BLOB blob;
cmd = "SELECT * FROM my_blob_table WHERE X='row1' FOR UPDATE";
ResultSet rset = stmt.executeQuery(cmd);
rset.next();
BLOB blob = ((OracleResultSet)rset).getBLOB(2);
File binaryFile = new File("john.gif");
System.out.println("john.gif length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.setBinaryStream(1L);
int size = blob.getBufferSize();
byte[] buffer = new byte[size];
int length = -1;

Source: http://docs.oracle.com/cd/B19306_01/java.102/b14355/oralob.htm#CHDFHHHG




回答2:


Is not using Hibernate an option ?

You can use UTL_FILE to read from a file

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm#i1003849

And DBMS_LOB to write to a LOB

http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lob.htm#i999705

By reading/writing in chunks you can avoid having the whole blob in memory at any one time.

For UTL_FILE to work, the database does need to be able to read the file from the filesystem (ie the filesystem containing the file must be accessible to the database server). You can also read from an HTTP stream or TCP data stream if the file is available for HTTP/FTP etc.



来源:https://stackoverflow.com/questions/24923882/how-to-insert-a-huge-file-to-blob-oracle-without-loading-the-complete-file-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!