Most efficient solution for reading CLOB to String, and String to CLOB in Java?

前端 未结 11 1250
长发绾君心
长发绾君心 2020-12-01 07:04

I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the \"int length\" constru

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 08:06

    If using Mule, below are the steps.

    Follow below steps.

    Enable streaming in the connector i.e. progressiveStreaming=2

    Typecast DB2 returned CLOB to java.sql.Clob (IBM Supports this type cast)

    Convert that to character stream (ASCII stream sometimes may not support some special characters). So you may to use getCharacterStream()

    That will return a "reader" object which can be converted to "String" using common-io (IOUtils).

    So in short, use groovy component and add below code.

    clobTest = (java.sql.Clob)payload.field1 
    bodyText = clobTest.getCharacterStream() 
    targetString = org.apache.commons.io.IOUtils.toString(bodyText)
    payload.PAYLOADHEADERS=targetString return payload
    

    Note: Here I'm assuming "payload.field1" is holding clob data.

    That's it!

    Regards Naveen

提交回复
热议问题