Israel Varea created NIFI-4830:
----------------------------------

             Summary: 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


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, blobLength);
                            ByteBuffer bb = ByteBuffer.wrap(blobAsBytes);
                            rec.put(i - 1, bb);
                            blob.free();
                        } else {
                            rec.put(i - 1, null);
                        }
                        continue;
                    }
 {code}









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

Reply via email to