switch
The switch output type allows you to route messages to different outputs based on their contents.
- Common
- Advanced
# Common config fields, showing default valuesoutput:label: ""switch:retry_until_success: truecases: []
# All config fields, showing default valuesoutput:label: ""switch:retry_until_success: truestrict_mode: falsemax_in_flight: 1cases: []
Messages must successfully route to one or more outputs, otherwise this is considered an error and the message is reprocessed. In order to explicitly drop messages that do not match your cases add one final case with a drop output.
Examples​
- Basic Multiplexing
- Control Flow
The most common use for a switch output is to multiplex messages across a range of output destinations. The following config checks the contents of the field type
of messages and sends foo
type messages to an amqp_1
output, bar
type messages to a gcp_pubsub
output, and everything else to a redis_streams
output.
Outputs can have their own processors associated with them, and in this example the redis_streams
output has a processor that enforces the presence of a type field before sending it.
output:switch:cases:- check: this.type == "foo"output:amqp_1:url: amqps://guest:guest@localhost:5672/target_address: queue:/the_foos- check: this.type == "bar"output:gcp_pubsub:project: dealing_with_miketopic: mikes_bars- output:redis_streams:url: tcp://localhost:6379stream: everything_elseprocessors:- bloblang: |root = thisroot.type = this.type | "unknown"
The continue
field allows messages that have passed a case to be tested against the next one also. This can be useful when combining non-mutually-exclusive case checks.
In the following example a message that passes both the check of the first case as well as the second will be routed to both.
output:switch:cases:- check: 'this.user.interests.contains("walks").catch(false)'output:amqp_1:url: amqps://guest:guest@localhost:5672/target_address: queue:/people_what_think_goodcontinue: true- check: 'this.user.dislikes.contains("videogames").catch(false)'output:gcp_pubsub:project: peopletopic: that_i_dont_want_to_hang_with
Fields​
retry_until_success
​
If a selected output fails to send a message this field determines whether it is reattempted indefinitely. If set to false the error is instead propagated back to the input level.
If a message can be routed to >1 outputs it is usually best to set this to true in order to avoid duplicate messages being routed to an output.
Type: bool
Default: true
strict_mode
​
This field determines whether an error should be reported if no condition is met. If set to true, an error is propagated back to the input level. The default behavior is false, which will drop the message.
Type: bool
Default: false
max_in_flight
​
The maximum number of parallel message batches to have in flight at any given time. Note that if a child output has a higher max_in_flight
then the switch output will automatically match it, therefore this value is the minimum max_in_flight
to set in cases where the child values can't be inferred (such as when using resource outputs as children).
Type: int
Default: 1
cases
​
A list of switch cases, outlining outputs that can be routed to.
Type: array
Default: []
# Examplescases:- check: this.urls.contains("http://benthos.dev")continue: trueoutput:cache:key: ${!json("id")}target: foo- output:s3:bucket: barpath: ${!json("id")}
cases[].check
​
A Bloblang query that should return a boolean value indicating whether a message should be routed to the case output. If left empty the case always passes.
Type: string
Default: ""
# Examplescheck: this.type == "foo"check: this.contents.urls.contains("https://benthos.dev/")
cases[].output
​
An output for messages that pass the check to be routed to.
Type: output
Default: {}
cases[].continue
​
Indicates whether, if this case passes for a message, the next case should also be tested.
Type: bool
Default: false