Skip to content

read-chunked ​

Reads content of any size, from any source, as a sequence of chunks, cut by size or by record. In Anypoint Studio the operation is shown as Read Chunked.

Syntax ​

Both child elements are optional. This example, from the connector's sample application, cuts the content into chunks of 5 records and keeps up to 20 chunks in memory so that they can be read more than once:

xml
<chunking:read-chunked doc:name="Read chunked, in-memory">
    <chunking:chunking-strategy>
        <chunking:by-delimiter recordsPerChunk="5"/>
    </chunking:chunking-strategy>
    <chunking:streaming-strategy>
        <chunking:in-memory maxInMemoryChunks="20"/>
    </chunking:streaming-strategy>
</chunking:read-chunked>

The operation needs no config-ref.

Parameters ​

ParameterStudio nameRequiredDefaultDescription
ContentContentNoThe message payloadThe content to chunk
chunking-strategyChunking StrategyNoBy size, in chunks of 1 MBHow the content is cut into chunks: by-size or by-delimiter. See Chunking Strategies.
streaming-strategyStreaming StrategyNonon-repeatableHow the chunks are held: non-repeatable, sliding-window, in-memory or file-store. See Streaming Strategies.

In Anypoint Studio a strategy left as None, and in Anypoint Code Builder one whose box is left unticked, means the default.

Chunking strategy settings ​

ElementSettingStudio nameDefaultDescription
by-sizechunkSizeChunk Size1048576 (1 MB)Bytes in each chunk but the last
by-delimiterdelimiterDelimitera newlineThe exact sequence that ends a record, as text or as a DataWeave expression
by-delimiterencodingEncodingUTF-8The character encoding that turns the delimiter into bytes
by-delimiterrecordsPerChunkRecords Per ChunkrequiredThe number of records in each chunk
by-delimitermaxChunkSizeMax Chunk Size1048576 (1 MB)The most bytes a chunk may hold; it never cuts a record

Streaming strategy settings ​

ElementSettingStudio nameDefaultDescription
non-repeatablenoneThe chunks can be read once, in order
sliding-windowmaxInMemoryChunksMax In-Memory Chunks3The most chunks held in memory, a positive number; a read that would hold more fails
sliding-windowreadersReadersnot setHow many readers read the chunks. See Sliding Window Readers.
in-memorymaxInMemoryChunksMax In-Memory Chunks10The most chunks held in memory, a positive number; reading one more fails the read
file-storemaxInMemoryChunksMax In-Memory Chunks10The most chunks held in memory, a positive number; older chunks go to a temporary file

Every size is in bytes, and every limit on what is held is counted in chunks. Every setting that is a number must be positive; nothing means "no limit".

Output ​

The operation returns the chunks, for foreach or another reader. Whether they can be read more than once, and by how many readers, depends on the streaming strategy.

Each chunk has these properties:

PropertyTypeDescription
payload.databinary (byte[])Exactly the chunk's bytes
payload.indexnumberThe chunk's position in the sequence, counting from 0
payload.offsetnumberThe position of the chunk's first byte in the content, counting from 0
payload.lengthnumberThe number of bytes in the chunk
payload.firstbooleantrue for the first chunk
payload.lastbooleantrue for the last chunk, including when the content ends exactly at the end of this chunk
payload.recordCountnumberWhen chunking by delimiter, the number of records the chunk holds; empty (null) when chunking by size

Every chunk is an exact slice of the content: the chunks' data, taken in order, reproduce the content byte for byte. Empty content produces no chunks.

Examples ​

Default settings ​

With no settings, the operation cuts the message payload into chunks of 1 MB, and foreach processes them one at a time:

xml
<chunking:read-chunked/>
<foreach>
    <logger level="INFO" message="#['Chunk $(payload.index): $(payload.length) bytes, last: $(payload.last)']"/>
</foreach>

By size ​

xml
<chunking:read-chunked>
    <chunking:chunking-strategy>
        <chunking:by-size chunkSize="65536"/>
    </chunking:chunking-strategy>
</chunking:read-chunked>
<foreach>
    <logger level="INFO" message="#['Chunk $(payload.index): $(payload.length) bytes from byte $(payload.offset)']"/>
</foreach>

By delimiter ​

With a newline delimiter, the default, and 1000 records per chunk, each chunk is 1000 lines, regardless of their size in bytes:

xml
<chunking:read-chunked>
    <chunking:chunking-strategy>
        <chunking:by-delimiter recordsPerChunk="1000"/>
    </chunking:chunking-strategy>
</chunking:read-chunked>
<foreach>
    <logger level="INFO" message="#['Chunk $(payload.index) holds $(payload.recordCount) lines']"/>
</foreach>

More examples are on Chunking Strategies, Streaming Strategies and Sliding Window Readers.

Errors ​

The operation raises these errors itself, before any chunk is read, so a flow can handle them by type:

ErrorWhen
CHUNKING:INVALID_PARAMETERA value is not valid. Parent of the three errors below. Raised itself for an encoding the runtime does not recognise and for a streaming strategy setting that is not positive.
CHUNKING:INVALID_CHUNK_SIZEchunkSize or maxChunkSize is not a positive number of bytes
CHUNKING:INVALID_RECORDS_PER_CHUNKrecordsPerChunk is not positive
CHUNKING:INVALID_DELIMITERThe delimiter is empty
CHUNKING:READ_ERRORThere is no content to read: the content is null

A failure while the chunks are read, after the operation has returned, reaches the flow with Mule's own error types instead. See Errors for both, with examples of handling them.

See also ​

Site content released under the Apache License 2.0. Each asset carries its own license. RSS · llms.txt