github.com/Jeffail/benthos/v3@v3.65.0/website/docs/guides/bloblang/functions.md (about) 1 --- 2 title: Bloblang Functions 3 sidebar_label: Functions 4 description: A list of Bloblang functions 5 --- 6 7 <!-- 8 THIS FILE IS AUTOGENERATED! 9 10 To make changes please edit the contents of: 11 internal/bloblang/query/functions.go 12 internal/docs/bloblang.go 13 --> 14 15 import Tabs from '@theme/Tabs'; 16 import TabItem from '@theme/TabItem'; 17 18 Functions can be placed anywhere and allow you to extract information from your environment, generate values, or access data from the underlying message being mapped: 19 20 ```coffee 21 root.doc.id = uuid_v4() 22 root.doc.received_at = now() 23 root.doc.host = hostname() 24 ``` 25 26 Functions support both named and nameless style arguments: 27 28 ```coffee 29 root.values_one = range(start: 0, stop: this.max, step: 2) 30 root.values_two = range(0, this.max, 2) 31 ``` 32 33 ## General 34 35 ### `count` 36 37 The `count` function is a counter starting at 1 which increments after each time it is called. Count takes an argument which is an identifier for the counter, allowing you to specify multiple unique counters in your configuration. 38 39 #### Parameters 40 41 **`name`** <string> An identifier for the counter. 42 43 #### Examples 44 45 46 ```coffee 47 root = this 48 root.id = count("bloblang_function_example") 49 50 # In: {"message":"foo"} 51 # Out: {"id":1,"message":"foo"} 52 53 # In: {"message":"bar"} 54 # Out: {"id":2,"message":"bar"} 55 ``` 56 57 ### `deleted` 58 59 A function that returns a result indicating that the mapping target should be deleted. Deleting, also known as dropping, messages will result in them being acknowledged as successfully processed to inputs in a Benthos pipeline. For more information about error handling patterns read [here][error_handling]. 60 61 #### Examples 62 63 64 ```coffee 65 root = this 66 root.bar = deleted() 67 68 # In: {"bar":"bar_value","baz":"baz_value","foo":"foo value"} 69 # Out: {"baz":"baz_value","foo":"foo value"} 70 ``` 71 72 Since the result is a value it can be used to do things like remove elements of an array within `map_each`. 73 74 ```coffee 75 root.new_nums = this.nums.map_each(num -> if num < 10 { deleted() } else { num - 10 }) 76 77 # In: {"nums":[3,11,4,17]} 78 # Out: {"new_nums":[1,7]} 79 ``` 80 81 ### `ksuid` 82 83 Generates a new ksuid each time it is invoked and prints a string representation. 84 85 #### Examples 86 87 88 ```coffee 89 root.id = ksuid() 90 ``` 91 92 ### `nanoid` 93 94 Generates a new nanoid each time it is invoked and prints a string representation. 95 96 #### Parameters 97 98 **`length`** <(optional) integer> An optional length. 99 **`alphabet`** <(optional) string> An optional custom alphabet to use for generating IDs. When specified the field `length` must also be present. 100 101 #### Examples 102 103 104 ```coffee 105 root.id = nanoid() 106 ``` 107 108 It is possible to specify an optional length parameter. 109 110 ```coffee 111 root.id = nanoid(54) 112 ``` 113 114 It is also possible to specify an optional custom alphabet after the length parameter. 115 116 ```coffee 117 root.id = nanoid(54, "abcde") 118 ``` 119 120 ### `random_int` 121 122 Generates a non-negative pseudo-random 64-bit integer. An optional integer argument can be provided in order to seed the random number generator. 123 124 #### Parameters 125 126 **`seed`** <query expression, default `{"Value":0}`> A seed to use, if a query is provided it will only be resolved once during the lifetime of the mapping. 127 128 #### Examples 129 130 131 ```coffee 132 root.first = random_int() 133 root.second = random_int(1) 134 ``` 135 136 It is possible to specify a dynamic seed argument, in which case the argument will only be resolved once during the lifetime of the mapping. 137 138 ```coffee 139 root.first = random_int(timestamp_unix_nano()) 140 ``` 141 142 ### `range` 143 144 The `range` function creates an array of integers following a range between a start, stop and optional step integer argument. If the step argument is omitted then it defaults to 1. A negative step can be provided as long as stop < start. 145 146 #### Parameters 147 148 **`start`** <integer> The start value. 149 **`stop`** <integer> The stop value. 150 **`step`** <integer, default `1`> The step value. 151 152 #### Examples 153 154 155 ```coffee 156 root.a = range(0, 10) 157 root.b = range(start: 0, stop: this.max, step: 2) # Using named params 158 root.c = range(0, -this.max, -2) 159 160 # In: {"max":10} 161 # Out: {"a":[0,1,2,3,4,5,6,7,8,9],"b":[0,2,4,6,8],"c":[0,-2,-4,-6,-8]} 162 ``` 163 164 ### `throw` 165 166 Throws an error similar to a regular mapping error. This is useful for abandoning a mapping entirely given certain conditions. 167 168 #### Parameters 169 170 **`why`** <string> A string explanation for why an error was thrown, this will be added to the resulting error message. 171 172 #### Examples 173 174 175 ```coffee 176 root.doc.type = match { 177 this.exists("header.id") => "foo" 178 this.exists("body.data") => "bar" 179 _ => throw("unknown type") 180 } 181 root.doc.contents = (this.body.content | this.thing.body) 182 183 # In: {"header":{"id":"first"},"thing":{"body":"hello world"}} 184 # Out: {"doc":{"contents":"hello world","type":"foo"}} 185 186 # In: {"nothing":"matches"} 187 # Out: Error("failed assignment (line 1): unknown type") 188 ``` 189 190 ### `uuid_v4` 191 192 Generates a new RFC-4122 UUID each time it is invoked and prints a string representation. 193 194 #### Examples 195 196 197 ```coffee 198 root.id = uuid_v4() 199 ``` 200 201 ## Message Info 202 203 ### `batch_index` 204 205 Returns the index of the mapped message within a batch. This is useful for applying maps only on certain messages of a batch. 206 207 #### Examples 208 209 210 ```coffee 211 root = if batch_index() > 0 { deleted() } 212 ``` 213 214 ### `batch_size` 215 216 Returns the size of the message batch. 217 218 #### Examples 219 220 221 ```coffee 222 root.foo = batch_size() 223 ``` 224 225 ### `content` 226 227 Returns the full raw contents of the mapping target message as a byte array. When mapping to a JSON field the value should be encoded using the method [`encode`][methods.encode], or cast to a string directly using the method [`string`][methods.string], otherwise it will be base64 encoded by default. 228 229 #### Examples 230 231 232 ```coffee 233 root.doc = content().string() 234 235 # In: {"foo":"bar"} 236 # Out: {"doc":"{\"foo\":\"bar\"}"} 237 ``` 238 239 ### `error` 240 241 If an error has occurred during the processing of a message this function returns the reported cause of the error. For more information about error handling patterns read [here][error_handling]. 242 243 #### Examples 244 245 246 ```coffee 247 root.doc.error = error() 248 ``` 249 250 ### `errored` 251 252 Returns a boolean value indicating whether an error has occurred during the processing of a message. For more information about error handling patterns read [here][error_handling]. 253 254 #### Examples 255 256 257 ```coffee 258 root.doc.status = if errored() { 400 } else { 200 } 259 ``` 260 261 ### `json` 262 263 Returns the value of a field within a JSON message located by a [dot path][field_paths] argument. This function always targets the entire source JSON document regardless of the mapping context. 264 265 #### Parameters 266 267 **`path`** <string, default `""`> An optional [dot path][field_paths] identifying a field to obtain. 268 269 #### Examples 270 271 272 ```coffee 273 root.mapped = json("foo.bar") 274 275 # In: {"foo":{"bar":"hello world"}} 276 # Out: {"mapped":"hello world"} 277 ``` 278 279 The path argument is optional and if omitted the entire JSON payload is returned. 280 281 ```coffee 282 root.doc = json() 283 284 # In: {"foo":{"bar":"hello world"}} 285 # Out: {"doc":{"foo":{"bar":"hello world"}}} 286 ``` 287 288 ### `meta` 289 290 Returns the value of a metadata key from the input message. Since values are extracted from the read-only input message they do NOT reflect changes made from within the map. In order to query metadata mutations made within a mapping use the [`root_meta` function](#root_meta). This function supports extracting metadata from other messages of a batch with the `from` method. 291 292 #### Parameters 293 294 **`key`** <string, default `""`> An optional key of a metadata value to obtain. 295 296 #### Examples 297 298 299 ```coffee 300 root.topic = meta("kafka_topic") 301 ``` 302 303 If the target key does not exist an error is thrown, allowing you to use coalesce or catch methods to fallback to other queries. 304 305 ```coffee 306 root.topic = meta("nope") | meta("also nope") | "default" 307 ``` 308 309 The parameter is optional and if omitted the entire metadata contents are returned as an object. 310 311 ```coffee 312 root.all_metadata = meta() 313 ``` 314 315 ### `root_meta` 316 317 BETA: This function is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with it is found. 318 319 Returns the value of a metadata key from the new message being created. Changes made to metadata during a mapping will be reflected by this function. 320 321 #### Parameters 322 323 **`key`** <string, default `""`> An optional key of a metadata value to obtain. 324 325 #### Examples 326 327 328 ```coffee 329 root.topic = root_meta("kafka_topic") 330 ``` 331 332 If the target key does not exist an error is thrown, allowing you to use coalesce or catch methods to fallback to other queries. 333 334 ```coffee 335 root.topic = root_meta("nope") | root_meta("also nope") | "default" 336 ``` 337 338 The parameter is optional and if omitted the entire metadata contents are returned as an object. 339 340 ```coffee 341 root.all_metadata = root_meta() 342 ``` 343 344 ## Environment 345 346 ### `env` 347 348 Returns the value of an environment variable, or an empty string if the environment variable does not exist. 349 350 #### Parameters 351 352 **`name`** <string> The name of an environment variable. 353 354 #### Examples 355 356 357 ```coffee 358 root.thing.key = env("key") 359 ``` 360 361 ### `file` 362 363 BETA: This function is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with it is found. 364 365 Reads a file and returns its contents. Relative paths are resolved from the directory of the process executing the mapping. 366 367 #### Parameters 368 369 **`path`** <string> The path of the target file. 370 371 #### Examples 372 373 374 ```coffee 375 root.doc = file(env("BENTHOS_TEST_BLOBLANG_FILE")).parse_json() 376 377 # In: {} 378 # Out: {"doc":{"foo":"bar"}} 379 ``` 380 381 ### `hostname` 382 383 Returns a string matching the hostname of the machine running Benthos. 384 385 #### Examples 386 387 388 ```coffee 389 root.thing.host = hostname() 390 ``` 391 392 ### `now` 393 394 Returns the current timestamp as a string in ISO 8601 format with the local timezone. Use the method `format_timestamp` in order to change the format and timezone. 395 396 #### Examples 397 398 399 ```coffee 400 root.received_at = now() 401 ``` 402 403 ```coffee 404 root.received_at = now().format_timestamp("Mon Jan 2 15:04:05 -0700 MST 2006", "UTC") 405 ``` 406 407 ### `timestamp_unix` 408 409 Returns the current unix timestamp in seconds. 410 411 #### Examples 412 413 414 ```coffee 415 root.received_at = timestamp_unix() 416 ``` 417 418 ### `timestamp_unix_nano` 419 420 Returns the current unix timestamp in nanoseconds. 421 422 #### Examples 423 424 425 ```coffee 426 root.received_at = timestamp_unix_nano() 427 ``` 428 429 ## Deprecated 430 431 ### `timestamp` 432 433 Returns the current time in a custom format specified by the argument. The format is defined by showing how the reference time, defined to be `Mon Jan 2 15:04:05 -0700 MST 2006` would be displayed if it were the value. 434 435 A fractional second is represented by adding a period and zeros to the end of the seconds section of layout string, as in `15:04:05.000` to format a time stamp with millisecond precision. This has been deprecated in favour of the new `now` function. 436 437 #### Parameters 438 439 **`format`** <string, default `"Mon Jan 2 15:04:05 -0700 MST 2006"`> The format to print as. 440 441 #### Examples 442 443 444 ```coffee 445 root.received_at = timestamp("15:04:05") 446 ``` 447 448 ### `timestamp_utc` 449 450 The equivalent of `timestamp` except the time is printed as UTC instead of the local timezone. This has been deprecated in favour of the new `now` function. 451 452 #### Parameters 453 454 **`format`** <string, default `"Mon Jan 2 15:04:05 -0700 MST 2006"`> The format to print as. 455 456 #### Examples 457 458 459 ```coffee 460 root.received_at = timestamp_utc("15:04:05") 461 ``` 462 463 [error_handling]: /docs/configuration/error_handling 464 [field_paths]: /docs/configuration/field_paths 465 [meta_proc]: /docs/components/processors/metadata 466 [methods.encode]: /docs/guides/bloblang/methods#encode 467 [methods.string]: /docs/guides/bloblang/methods#string