[resolved] Create file from a blob input (oracle db)

One Star

[resolved] Create file from a blob input (oracle db)

Hi,
I would like to create a picture (.jpg) from the blob data of an oracle table.
I have an oracle input that connects to the table "Photo" and retrieve the column "data" of type Blob.
How can I create the picture with this Blob data ?
I tried with a tMap and tJava, but the tMap will convert the blob into an "Object", so I don't know how I can work with this...
Thanks in advance for the help Smiley Happy
Thomas

Accepted Solutions
Seventeen Stars

Re: [resolved] Create file from a blob input (oracle db)

There is currently only one way to do this: use the custom component tLOBDownload
https://exchange.talend.com/#marketplaceproductoverview:gallery=marketplace%252F1&pi=marketplace%252...

All Replies
Seventeen Stars

Re: [resolved] Create file from a blob input (oracle db)

There is currently only one way to do this: use the custom component tLOBDownload
https://exchange.talend.com/#marketplaceproductoverview:gallery=marketplace%252F1&pi=marketplace%252...
Employee

Re: [resolved] Create file from a blob input (oracle db)

I've not tried this with jpgs but I have recently been working with MS Docs stored in a db. I simply used the tFileOutputRaw to do this. Just supply it your blob data as an object and it should work.
Employee

Re: [resolved] Create file from a blob input (oracle db)

I actually started to reply to this before jlolling wrote his response. Since reading his post, I have double checked my response and have found that it isn't quite as easy as I made out. But I have a solution which makes it pretty straight forward.
The job I put together looks like this...
I am receiving an image (as a Blob) and an ID from the db input. The code in the tJavaRow converts the blob to a byte array. The byte array is then sent to the tFileOutputRaw and that produces the file successfully. The Java used in the tJavaRow is below....
ByteArrayOutputStream baos = new ByteArrayOutputStream();
java.sql.Blob blob = (java.sql.Blob)input_row.IMAGE;
byte[] buf = new byte;
java.io.InputStream in = blob.getBinaryStream();
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
output_row.IMAGE = bytes;
output_row.ID = input_row.ID;
One Star

Re: [resolved] Create file from a blob input (oracle db)

Thank you both for your answers. I will try both solutions.
Thomas
One Star

Re: [resolved] Create file from a blob input (oracle db)

Hi,
Your method works fine rhall, the only problem (which in my case is quite important) is this:
I have my javarow linked directly to the fileoutputraw, and I defined the name that the file will have in the javarow... How can I get the value of the variable from the javarow in the fileoutputraw ? (I already tried with context, but I will save iteratively many photos, so the name is changing dynamically)?
Thanks in advance for the help!
Thomas
Employee

Re: [resolved] Create file from a blob input (oracle db)

Hi Thomas,
You can use the globalMap hashmap. Calculate the filename in your javarow component and then save it to the globalMap variable like below....
globalMap.put("filename", "myFile.jpg")
.... then when you want to use it in the file component, retrieve the value like below....
((String)globalMap.get("filename"))
Regards
Richard
One Star

Re: [resolved] Create file from a blob input (oracle db)

Hi rhall,
Thank you very much for your time.
Actually, I already tried this solution, but even tough the system.out.println swears that the "filename" path is correct in the javarow, the tfileoutputraw gives me a nullpointerexception ...
With 3 iterations, here is a sample of my code:
output_row.PATH = context.dir_photos+input_row.CODE_LOGIQUE+".jpg"; //comes from a tmap Smiley Happy
globalMap.put("filename", output_row.PATH);
System.out.println((String)globalMap.get("filename")); //works correctly
And the execution is the following:
connecting to socket on port 4057
connected
/Users/talias/Desktop/mssql/photos/ULR_40244.jpg
java.lang.NullPointerException
java.lang.NullPointerException
/Users/talias/Desktop/mssql/photos/ULR_40244.jpg
/Users/talias/Desktop/mssql/photos/ULR_40244.jpg
java.lang.NullPointerException
disconnected
I guess I missed something ?^^
Thomas
Employee

Re: [resolved] Create file from a blob input (oracle db)

Ah yes, I forgot about this. There is a hack to get round this irritating issue with dynamic file names. You need to use a tFlowToIterate followed by a tIterateToFlow. I have changed my job to look like below....

In the tJavaRow I have created an extra String output where I create and output my filename. The tFlowToIterate puts the byte[] content and filename string into the globalMap hashmap. Then the tIterateToFlow takes them one row at a time and creates a flow out of them. This is then sent to the tFileOutRaw and allows it to create a new file for each row.
The tIterateToFlow is configured as below....

The tFileOutputRaw is configured as below....

This should work :-)
One Star

Re: [resolved] Create file from a blob input (oracle db)

-POST EDITED-
I thought I had an error, but it looks like I also had to cast my row.FILEPATH to string (not just the row.IMAGE).
It finally works, you helped me a lot, thank you very much ! Smiley Happy
Thomas
Four Stars

Re: [resolved] Create file from a blob input (oracle db)

Hi ,
I am trying to retrieve text file from oracle database , trying to do it as shown above .
But there is a problem with the schema in tjavarow "cannot cast from byte to blob".
please help!
TIA
Hachi
One Star

Re: [resolved] Create file from a blob input (oracle db)

Hi do you know if this Component is comptible with TOS DI 6.1?
I have an error message "Component not loaded"
Seventeen Stars

Re: [resolved] Create file from a blob input (oracle db)

The best way to download BLOBs is using the custom component tLOBDownload from Talend Exchange:
https://exchange.talend.com/#marketplaceproductoverview:gallery=marketplace%252F1&pi=marketplace%252...
This component is exactly made for this use case.
Four Stars

I am unable to download images from Database

It Showing only the images names .The images are not downloading ..

 

I followed  Your Steps to download images from database. Please guide me how do this Talend job or Could You share your job.

 

Thank you..