Step 4: Configure Batching
Batching collects a number of messages and sends them as a group, reducing the number of output operations.
The Goal
You will modify your pipeline's output to use a batching policy. Instead of writing every single log to a file as it arrives, the pipeline will collect 10 messages (or wait for 5 seconds) and then write them all at once.
The batching Block
This example places the batching policy on a broker wrapping the file output. The two most common fields are:
count: The number of messages to collect before sending.period: The maximum amount of time to wait before sending, even if thecounthas not been reached.
Whichever condition is met first triggers the send.
Implementation
-
Start with the Previous Pipeline: Copy the
restructure-log.yamlfrom Step 3 to a new file namedbatched-output.yaml.cp restructure-log.yaml batched-output.yaml -
Add the Batching Logic: Open
batched-output.yamland replace the entireoutputsection with the batchingbrokerand its file output below.Replace the 'output' section in batched-output.yamloutput:
broker:
pattern: fan_out
batching:
count: 10
period: 5s
outputs:
- file:
path: /tmp/batched_logs.jsonl
codec: lines -
Deploy and Test:
# Create the output file's directory if it doesn't exist
mkdir -p /tmp -
Verify: Watch the output file. With one generated message per second, the 5-second period flushes the batch before it reaches 10 messages.
tail -f /tmp/batched_logs.jsonlIf you were to stop the input, any remaining messages in the batch would be flushed after the 5-second
periodexpires.
You have now implemented a batching policy, which is a critical technique for building high-throughput, cost-effective data pipelines.