Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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 0683p000009MACn.png
Thomas
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

15 Replies
Anonymous
Not applicable
Author

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...
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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...
0683p000009MDSB.pngI 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;
Anonymous
Not applicable
Author

Thank you both for your answers. I will try both solutions.
Thomas
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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 0683p000009MACn.png
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
Anonymous
Not applicable
Author

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....

0683p000009MDUo.pngIn 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....
0683p000009MDK7.png
The tFileOutputRaw is configured as below....
0683p000009MDMg.png
This should work 🙂
Anonymous
Not applicable
Author

-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 ! 0683p000009MACn.png
Thomas