[ 
https://issues.apache.org/jira/browse/NIFI-4830?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16357405#comment-16357405
 ] 

Matt Burgess commented on NIFI-4830:
------------------------------------

I'm not sure we can/should use getBytes() at this point, the comment about 
getObject() makes me think this stream-based approach was done on purpose. 
Instead I think we can just fix the logic bug by changing >0 to >=0, which 
aligns with the behavior of the read() method.

I should mention that the unit tests did not catch this because for both BLOB 
and CLOB, alphanumeric characters were being used for test. The unit tests 
should be updated to exhibit this problem, and of course pass once the logic 
errors are fixed.

> Blob not being read properly from Database to Avro
> --------------------------------------------------
>
>                 Key: NIFI-4830
>                 URL: https://issues.apache.org/jira/browse/NIFI-4830
>             Project: Apache NiFi
>          Issue Type: Bug
>            Reporter: Israel Varea
>            Priority: Major
>
> When using QueryDatabaseTable or any other component using JdbcCommon.java, 
> the Blob column of the database is not fetched correctly to the Avro 
> ByteBuffer. Instead of fetching whole file, only first bytes which are 
> different to zero are added. Then, the rest of the bytes remain with zero 
> value.
> Specifically, this code in JdbcCommon.java should be fixed:
> {code:java}
>                     if (javaSqlType == BLOB) {
>                         Blob blob = rs.getBlob(i);
>                         if (blob != null) {
>                             long numChars = blob.length();
>                             byte[] buffer = new byte[(int) numChars];
>                             InputStream is = blob.getBinaryStream();
>                             int index = 0;
>                             int c = is.read();
>                             while (c > 0) {
>                                 buffer[index++] = (byte) c;
>                                 c = is.read();
>                             }
>                             ByteBuffer bb = ByteBuffer.wrap(buffer);
>                             rec.put(i - 1, bb);
>                             blob.free();
>                         } else {
>                             rec.put(i - 1, null);
>                         }
>                         continue;
>                     }
>  {code}
> Notice the while statement.
> The whole block should be replaced for something like
> {code:java}
>                     if (javaSqlType == BLOB) {
>                         Blob blob = rs.getBlob(i);
>                         if (blob != null) {
>                             long blobLength = blob.length();                  
>           
>                             byte[] blobAsBytes = blob.getBytes(1, 
> (int)blobLength);
>                             ByteBuffer bb = ByteBuffer.wrap(blobAsBytes);
>                             rec.put(i - 1, bb);
>                             blob.free();
>                         } else {
>                             rec.put(i - 1, null);
>                         }
>                         continue;
>                     }
>  {code}
> This has been tested locally and it works! :)



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to