github.com/Jeffail/benthos/v3@v3.65.0/website/docs/components/outputs/switch.md (about) 1 --- 2 title: switch 3 type: output 4 status: stable 5 categories: ["Utility"] 6 --- 7 8 <!-- 9 THIS FILE IS AUTOGENERATED! 10 11 To make changes please edit the contents of: 12 lib/output/switch.go 13 --> 14 15 import Tabs from '@theme/Tabs'; 16 import TabItem from '@theme/TabItem'; 17 18 19 The switch output type allows you to route messages to different outputs based on their contents. 20 21 22 <Tabs defaultValue="common" values={[ 23 { label: 'Common', value: 'common', }, 24 { label: 'Advanced', value: 'advanced', }, 25 ]}> 26 27 <TabItem value="common"> 28 29 ```yaml 30 # Common config fields, showing default values 31 output: 32 label: "" 33 switch: 34 retry_until_success: true 35 cases: [] 36 ``` 37 38 </TabItem> 39 <TabItem value="advanced"> 40 41 ```yaml 42 # All config fields, showing default values 43 output: 44 label: "" 45 switch: 46 retry_until_success: true 47 strict_mode: false 48 max_in_flight: 1 49 cases: [] 50 ``` 51 52 </TabItem> 53 </Tabs> 54 55 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](/docs/components/outputs/drop). 56 57 ## Examples 58 59 <Tabs defaultValue="Basic Multiplexing" values={[ 60 { label: 'Basic Multiplexing', value: 'Basic Multiplexing', }, 61 { label: 'Control Flow', value: 'Control Flow', }, 62 ]}> 63 64 <TabItem value="Basic Multiplexing"> 65 66 67 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. 68 69 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. 70 71 ```yaml 72 output: 73 switch: 74 cases: 75 - check: this.type == "foo" 76 output: 77 amqp_1: 78 url: amqps://guest:guest@localhost:5672/ 79 target_address: queue:/the_foos 80 81 - check: this.type == "bar" 82 output: 83 gcp_pubsub: 84 project: dealing_with_mike 85 topic: mikes_bars 86 87 - output: 88 redis_streams: 89 url: tcp://localhost:6379 90 stream: everything_else 91 processors: 92 - bloblang: | 93 root = this 94 root.type = this.type | "unknown" 95 ``` 96 97 </TabItem> 98 <TabItem value="Control Flow"> 99 100 101 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. 102 103 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. 104 105 ```yaml 106 output: 107 switch: 108 cases: 109 - check: 'this.user.interests.contains("walks").catch(false)' 110 output: 111 amqp_1: 112 url: amqps://guest:guest@localhost:5672/ 113 target_address: queue:/people_what_think_good 114 continue: true 115 116 - check: 'this.user.dislikes.contains("videogames").catch(false)' 117 output: 118 gcp_pubsub: 119 project: people 120 topic: that_i_dont_want_to_hang_with 121 ``` 122 123 </TabItem> 124 </Tabs> 125 126 ## Fields 127 128 ### `retry_until_success` 129 130 If a selected output fails to send a message this field determines whether it is 131 reattempted indefinitely. If set to false the error is instead propagated back 132 to the input level. 133 134 If a message can be routed to >1 outputs it is usually best to set this to true 135 in order to avoid duplicate messages being routed to an output. 136 137 138 Type: `bool` 139 Default: `true` 140 141 ### `strict_mode` 142 143 This field determines whether an error should be reported if no condition is met. 144 If set to true, an error is propagated back to the input level. The default 145 behavior is false, which will drop the message. 146 147 148 Type: `bool` 149 Default: `false` 150 151 ### `max_in_flight` 152 153 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). 154 155 156 Type: `int` 157 Default: `1` 158 159 ### `cases` 160 161 A list of switch cases, outlining outputs that can be routed to. 162 163 164 Type: `array` 165 Default: `[]` 166 167 ```yaml 168 # Examples 169 170 cases: 171 - check: this.urls.contains("http://benthos.dev") 172 continue: true 173 output: 174 cache: 175 key: ${!json("id")} 176 target: foo 177 - output: 178 s3: 179 bucket: bar 180 path: ${!json("id")} 181 ``` 182 183 ### `cases[].check` 184 185 A [Bloblang query](/docs/guides/bloblang/about/) that should return a boolean value indicating whether a message should be routed to the case output. If left empty the case always passes. 186 187 188 Type: `string` 189 Default: `""` 190 191 ```yaml 192 # Examples 193 194 check: this.type == "foo" 195 196 check: this.contents.urls.contains("https://benthos.dev/") 197 ``` 198 199 ### `cases[].output` 200 201 An [output](/docs/components/outputs/about/) for messages that pass the check to be routed to. 202 203 204 Type: `output` 205 Default: `{}` 206 207 ### `cases[].continue` 208 209 Indicates whether, if this case passes for a message, the next case should also be tested. 210 211 212 Type: `bool` 213 Default: `false` 214 215