This recipe dispatches the lines of a single file between several processings, according to their content: a typical partner file mixing products, categories and comments.
type;code;label;total
product;P001;Chair;10
category;C01;Furniture;
product;P002;Table;2500
comment;;File of 2026-12-03;
promo;X1;Black Friday;Before the RouterTask, this was done with a cascade of
FilterTask, each rejected line going to the next filter through error_outputs:
is_product:
task: filter
options: { match: { '[type]': product } }
outputs: [write_product]
error_outputs: [is_category] # "else", through the error channel
is_category:
task: filter
options: { match: { '[type]': category } }
outputs: [write_category] # the other lines are lost without trace- The next filters and their tasks are an error branch (shown as such by
cleverage:process:help). - A failure of
is_productwitherror_strategy: skipsends the line tois_category, as if it were not a product. - Each new type adds a filter, and a line of no known type is silently dropped.
With a router, the dispatch is a single task, the lines of no known type are reported, and the errors stay errors:
cleverage_process:
configurations:
app.import_catalog:
description: 'Import the catalog file of the partner'
tasks:
read:
task: csv_reader
options:
file_path: '%kernel.project_dir%/var/data/catalog.csv'
delimiter: ';'
outputs: [route]
route:
task: router
options:
mode: all
conditions:
product: { match: { '[type]': product } }
category: { match: { '[type]': category } }
comment: { match: { '[type]': comment } }
big: { expression: 'data["type"] == "product" and data["total"] > 1000' }
default: unknown # a line of no known type (e.g. promo)
outputs: [count] # every line
branches:
product: [write_product]
category: [write_category]
comment: [] # ignored
big: [log_big]
unknown: [log_unknown]
count:
task: stat_counter
write_product:
task: csv_writer
options:
file_path: '%kernel.project_dir%/var/exports/products.csv'
write_category:
task: csv_writer
options:
file_path: '%kernel.project_dir%/var/exports/categories.csv'
log_big:
task: logger
options:
level: notice
message: 'Big product'
log_unknown:
task: logger
options:
level: warning
message: 'Unknown line type'How it works:
- CsvReaderTask is iterable: each line goes through the router before the next one is read.
- The RouterTask sends every line to its
outputs(the StatCounterTask), and to the tasks of the branches it matches. Withmode: all, the lineP002goes towrite_productandlog_big; with the defaultmode: first, only the first matching branch (in the order ofconditions) would get it. - The
commentbranch has no task: the comments are dropped on purpose, without warning. - The line
promomatches no branch: it goes to thedefaultbranch,log_unknown. Without adefaultbranch, it would go to theerror_outputsof the router, with a warning, mixed with the errors of the router (e.g. a missing key in an expression, witherror_strategy: skip): adefaultbranch keeps them apart. cleverage:process:help app.import_catalogshows the branch leading to each task (write_product [branch route.product]), in the main branch.
Keep the FilterTask to drop the items that do not match (e.g. the lines without
sku): a router is meant to dispatch them. The branches of a router cannot be joined back with an
InputAggregatorTask, see
conditional branches.