Bloblang Functions
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:
root.doc.id = uuid_v4()
root.doc.received_at = now()
root.doc.host = hostname()
Functions support both named and nameless style arguments:
root.values_one = range(start: 0, stop: this.max, step: 2)
root.values_two = range(0, this.max, 2)
General
cosine_similarity
Calculates the cosine similarity between two vectors a and b. Vectors must be the same length and neither vector can be null (all zeros).
Parameters
a <unknown> Vector of numbers.
b <unknown> Vector of numbers.
Examples
Calculate similarity between vectors
root.similarity = cosine_similarity([1, 2, 3], [2, 4, 6])
# In: {}
# Out: {"similarity":1}
Orthogonal vectors have zero similarity
root.similarity = cosine_similarity([1, 0], [0, 1])
# In: {}
# Out: {"similarity":0}
counter
This function is experimental and therefore breaking changes could be made to it outside of major version releases.
Returns a non-negative integer that increments each time it is resolved, yielding the minimum (1 by default) as the first value. Each instantiation of counter has its own independent count. Once the maximum integer (or max argument) is reached the counter resets back to the minimum.
Parameters
min <query expression, default 1> The minimum value of the counter, this is the first value that will be yielded. If this parameter is dynamic it will be resolved only once during the lifetime of the mapping.
max <query expression, default 9223372036854775807> The maximum value of the counter, once this value is yielded the counter will reset back to the min. If this parameter is dynamic it will be resolved only once during the lifetime of the mapping.
set <(optional) query expression> An optional mapping that when specified will be executed each time the counter is resolved. When this mapping resolves to a non-negative integer value it will cause the counter to reset to this value and yield it. If this mapping is omitted or doesn't resolve to anything then the counter will increment and yield the value as normal. If this mapping resolves to null then the counter is not incremented and the current value is yielded. If this mapping resolves to a deletion then the counter is reset to the min value.
Examples
root.id = counter()
# In: {}
# Out: {"id":1}
# In: {}
# Out: {"id":2}
It's possible to increment a counter multiple times within a single mapping invocation using a map.
map foos {
root = counter()
}
root.meow_id = null.apply("foos")
root.woof_id = null.apply("foos")
# In: {}
# Out: {"meow_id":1,"woof_id":2}
# In: {}
# Out: {"meow_id":3,"woof_id":4}
By specifying an optional set parameter it is possible to dynamically reset the counter based on input data.
root.consecutive_doggos = counter(min: 1, set: if !this.sound.lowercase().contains("woof") { 0 })
# In: {"sound":"woof woof"}
# Out: {"consecutive_doggos":1}
# In: {"sound":"woofer wooooo"}
# Out: {"consecutive_doggos":2}
# In: {"sound":"meow"}
# Out: {"consecutive_doggos":0}
# In: {"sound":"uuuuh uh uh woof uhhhhhh"}
# Out: {"consecutive_doggos":1}
The set parameter can also be utilised to peek at the counter without mutating it by returning null.
root.things = counter(set: if this.id == null { null })
# In: {"id":"a"}
# Out: {"things":1}
# In: {"id":"b"}
# Out: {"things":2}
# In: {"what":"just checking"}
# Out: {"things":2}
# In: {"id":"c"}
# Out: {"things":3}
deleted
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 pipeline. For more information about error handling patterns read here.
Examples
root = this
root.bar = deleted()
# In: {"bar":"bar_value","baz":"baz_value","foo":"foo value"}
# Out: {"baz":"baz_value","foo":"foo value"}
Since the result is a value it can be used to do things like remove elements of an array within map_each.
root.new_nums = this.nums.map_each(num -> if num < 10 { deleted() } else { num - 10 })
# In: {"nums":[3,11,4,17]}
# Out: {"new_nums":[1,7]}
ksuid
Generates a new ksuid each time it is invoked and prints a string representation.
Examples
root.id = ksuid()
nanoid
Generates a new nanoid each time it is invoked and prints a string representation.
Parameters
length <(optional) integer> An optional length.
alphabet <(optional) string> An optional custom alphabet to use for generating IDs. When specified the field length must also be present.
Examples
root.id = nanoid()
It is possible to specify an optional length parameter.
root.id = nanoid(54)
It is also possible to specify an optional custom alphabet after the length parameter.
root.id = nanoid(54, "abcde")
pi
Returns the value of the mathematical constant Pi.
Examples
root.radians = this.degrees * (pi() / 180)
# In: {"degrees":45}
# Out: {"radians":0.7853981633974483}
root.degrees = this.radians * (180 / pi())
# In: {"radians":0.78540}
# Out: {"degrees":45.00010522957486}
random_int
Generates a non-negative pseudo-random 64-bit integer. An optional integer argument can be provided in order to seed the random number generator.
Optional min and max arguments can be provided in order to only generate numbers within a range. Neither of these parameters can be set via a dynamic expression (i.e. from values taken from mapped data). Instead, for dynamic ranges extract a min and max manually using a modulo operator (random_int() % a + b).
Parameters
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.
min <integer, default 0> The minimum value the random generated number will have. The default value is 0.
max <integer, default 9223372036854775806> The maximum value the random generated number will have. The default value is 9223372036854775806 (math.MaxInt64 - 1).
Examples
root.first = random_int()
root.second = random_int(1)
root.third = random_int(max:20)
root.fourth = random_int(min:10, max:20)
root.fifth = random_int(timestamp_unix_nano(), 5, 20)
root.sixth = random_int(seed:timestamp_unix_nano(), max:20)
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.
root.first = random_int(timestamp_unix_nano())
range
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.
Parameters
start <integer> The start value.
stop <integer> The stop value.
step <integer, default 1> The step value.
Examples
root.a = range(0, 10)
root.b = range(start: 0, stop: this.max, step: 2) # Using named params
root.c = range(0, -this.max, -2)
# In: {"max":10}
# Out: {"a":[0,1,2,3,4,5,6,7,8,9],"b":[0,2,4,6,8],"c":[0,-2,-4,-6,-8]}
snowflake_id
Generate a new snowflake ID each time it is invoked and prints a string representation. I.e.: 1559229974454472704
Parameters
node_id <integer, default 1> It is possible to specify the node_id.
Examples
root.id = snowflake_id()
It is possible to specify the node_id.
root.id = snowflake_id(2)
throw
Throws an error similar to a regular mapping error. This is useful for abandoning a mapping entirely given certain conditions.